From 5f3756aa134b5618d366c882ec65bb30def8e57f Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Sun, 21 Jun 2026 17:09:48 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20JSON=20decoding?= =?UTF-8?q?=20in=20normalize=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace O(N^2) string slicing and character-by-character iteration with `str.find` and index-based `json.JSONDecoder().raw_decode`. This prevents severe memory copying overhead when incrementally parsing large OpenCode JSON output files. --- .jules/bolt.md | 3 +++ scripts/ci/opencode_review_normalize_output.py | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 000000000..36414642e --- /dev/null +++ b/.jules/bolt.md @@ -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. diff --git a/scripts/ci/opencode_review_normalize_output.py b/scripts/ci/opencode_review_normalize_output.py index 711fd1ef3..d972ad63d 100755 --- a/scripts/ci/opencode_review_normalize_output.py +++ b/scripts/ci/opencode_review_normalize_output.py @@ -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