Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-06-21 - Python JSON Decoding Optimization
**Learning:** In Python, string slicing `text[index:]` inside a loop can cause O(N^2) complexity and severe memory copying overhead. When decoding JSON incrementally from a large text blob, `json.JSONDecoder().raw_decode(text, index)` can parse from a given index without slicing. Combining this with `text.find("{", index)` to skip irrelevant characters is significantly faster than `enumerate(text)`.
**Action:** Always prefer `raw_decode(text, index)` and `string.find()` over string slicing and character-by-character iteration when scanning large files for JSON objects.
18 changes: 12 additions & 6 deletions scripts/ci/opencode_review_normalize_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,20 @@ def iter_json_objects(text: str) -> list[Any]:
# OpenCode exports may contain prose around the JSON control object.
pass

for index, character in enumerate(text):
if character != "{":
continue
# Bolt: Use text.find and pass index directly to raw_decode to skip
# non-brace chars efficiently and prevent O(N^2) string copying.
index = 0
length = len(text)
while index < length:
index = text.find("{", index)
if index == -1:
break
try:
value, _ = decoder.raw_decode(text[index:])
value, _ = decoder.raw_decode(text, index)
values.append(value)
except json.JSONDecodeError:
continue
values.append(value)
pass
index += 1

return values

Expand Down
Loading