-
Notifications
You must be signed in to change notification settings - Fork 742
⚡ Bolt: optimize O(N^2) string manipulations in JSON parsers #7620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ## 2025-03-01 - O(N^2) String operations | ||
|
|
||
| **Learning:** String manipulation inside a loop (like `prefix += s1[i]`) causes O(N^2) behavior due to string immutability in Python, especially problematic since we process JSON objects where string values can be large. | ||
| **Action:** Always prefer string slicing `s1[:i]` or Python builtins like `.startswith()`, `.endswith()`, and `.replace()` instead of manual character-by-character loops. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,14 +35,11 @@ def find_common_prefix(s1: str, s2: str) -> str: | |
| e.g. find_common_prefix('{"fruit": "ap"}', '{"fruit": "apple"}') -> | ||
| '{"fruit": "ap' | ||
| """ | ||
| prefix = "" | ||
| min_length = min(len(s1), len(s2)) | ||
| for i in range(0, min_length): | ||
| if s1[i] == s2[i]: | ||
| prefix += s1[i] | ||
| else: | ||
| break | ||
| return prefix | ||
| for i in range(min_length): | ||
| if s1[i] != s2[i]: | ||
| return s1[:i] | ||
| return s1[:min_length] | ||
|
|
||
|
|
||
| def find_common_suffix(s1: str, s2: str) -> str: | ||
|
|
@@ -53,14 +50,11 @@ def find_common_suffix(s1: str, s2: str) -> str: | |
|
|
||
| e.g. find_common_suffix('{"fruit": "ap"}', '{"fruit": "apple"}') -> '"}' | ||
| """ | ||
| suffix = "" | ||
| min_length = min(len(s1), len(s2)) | ||
| for i in range(1, min_length + 1): | ||
| if s1[-i] == s2[-i] and not s1[-i].isalnum(): | ||
| suffix = s1[-i] + suffix | ||
| else: | ||
| break | ||
| return suffix | ||
| if s1[-i] != s2[-i] or s1[-i].isalnum(): | ||
| return s1[len(s1) - i + 1:] if i > 1 else "" | ||
| return s1[-min_length:] if min_length > 0 and not s1[-min_length].isalnum() else "" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 建议 当程序执行到此 该 return s1[-min_length:] if min_length > 0 else "" |
||
|
|
||
|
|
||
| def extract_intermediate_diff(curr: str, old: str) -> str: | ||
|
|
@@ -83,15 +77,26 @@ def extract_intermediate_diff(curr: str, old: str) -> str: | |
| """ | ||
| suffix = find_common_suffix(curr, old) | ||
|
|
||
| old = old[::-1].replace(suffix[::-1], "", 1)[::-1] | ||
| if suffix: | ||
| if old.endswith(suffix): | ||
| old = old[:-len(suffix)] | ||
| else: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 建议
同样地,第 92 行的 建议删除冗余的 if suffix:
old = old[:-len(suffix)]
prefix = find_common_prefix(curr, old)
diff = curr
if suffix:
diff = diff[:-len(suffix)]
if prefix:
diff = diff[len(prefix):] |
||
| old = old[::-1].replace(suffix[::-1], "", 1)[::-1] | ||
|
|
||
| prefix = find_common_prefix(curr, old) | ||
| diff = curr | ||
| if len(suffix): | ||
| diff = diff[::-1].replace(suffix[::-1], "", 1)[::-1] | ||
|
|
||
| if len(prefix): | ||
| # replace the prefix only once in case it's mirrored | ||
| diff = diff.replace(prefix, "", 1) | ||
| if suffix: | ||
| if diff.endswith(suffix): | ||
| diff = diff[:-len(suffix)] | ||
| else: | ||
| diff = diff[::-1].replace(suffix[::-1], "", 1)[::-1] | ||
|
|
||
| if prefix: | ||
| if diff.startswith(prefix): | ||
| diff = diff[len(prefix):] | ||
| else: | ||
| diff = diff.replace(prefix, "", 1) | ||
|
|
||
| return diff | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ 疑问 此文件是 Jules AI 系统的内部学习日志,不应提交到产品仓库
.jules/bolt.md是 Google Jules AI 任务系统的元数据文件(等同于.claude/目录),记录的是 Jules 实例的学习条目,不属于项目文档的一部分。建议将.jules/加入.gitignore并删除此文件。