Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
## 2024-05-24 - openpyxl read_only optimization
**Learning:** `openpyxl.load_workbook(..., read_only=True)` is significantly faster (1.75x) for parsing large files but requires explicit `wb.close()` (preferably in `try...finally`) as it keeps file handles open and `Workbook` objects may not support context managers in all versions.
**Action:** Use `read_only=True` for read-heavy Excel tasks and always ensure `close()` is called.

## 2024-05-25 - DOM Traversal Caching
**Learning:** Repeatedly scanning the DOM for IDs (e.g., `getElementsByTagName` in `_get_next_change_id`) causes O(N²) complexity during batch operations. Caching the next ID improves performance to O(N).
**Action:** Cache stateful ID counters in XML editors instead of re-scanning on every insertion.
28 changes: 17 additions & 11 deletions skills/docx/scripts/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,26 @@ def __init__(
self.rsid = rsid
self.author = author
self.initials = initials
self._next_change_id_cache = None

def _get_next_change_id(self):
"""Get the next available change ID by checking all tracked change elements."""
max_id = -1
for tag in ("w:ins", "w:del"):
elements = self.dom.getElementsByTagName(tag)
for elem in elements:
change_id = elem.getAttribute("w:id")
if change_id:
try:
max_id = max(max_id, int(change_id))
except ValueError:
pass
return max_id + 1
if self._next_change_id_cache is None:
max_id = -1
for tag in ("w:ins", "w:del"):
elements = self.dom.getElementsByTagName(tag)
for elem in elements:
change_id = elem.getAttribute("w:id")
if change_id:
try:
max_id = max(max_id, int(change_id))
except ValueError:
pass
self._next_change_id_cache = max_id + 1

next_id = self._next_change_id_cache
self._next_change_id_cache += 1
return next_id

def _ensure_w16du_namespace(self):
"""Ensure w16du namespace is declared on the root element."""
Expand Down