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
@@ -0,0 +1,4 @@
## 2025-03-01 - O(N^2) String operations
Copy link
Copy Markdown

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 并删除此文件。


**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.
43 changes: 24 additions & 19 deletions fastdeploy/entrypoints/openai/tool_parsers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 ""
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 建议 not s1[-min_length].isalnum() 守护条件冗余

当程序执行到此 return 语句时,循环内的 if s1[-i] != s2[-i] or s1[-i].isalnum() 条件在整个 range(1, min_length + 1) 内均未触发,说明所有字符已验证为非 alphanumeric,因此 s1[-min_length].isalnum() 在此处恒为 Falsenot s1[-min_length].isalnum() 恒为 True

else "" 分支是死代码,增加了阅读负担。建议简化为:

return s1[-min_length:] if min_length > 0 else ""



def extract_intermediate_diff(curr: str, old: str) -> str:
Expand All @@ -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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 建议 else 分支为不可达代码

suffix = find_common_suffix(curr, old) 按定义返回的是 currold 共同的后缀(函数内部通过从末尾逐字符匹配构造),因此当 suffix 非空时,old.endswith(suffix) 恒为 True,此 else 分支永远不会执行。

同样地,第 92 行的 else 分支(diff.endswith(suffix))和第 98 行的 else 分支(diff.startswith(prefix))也是不可达代码。

建议删除冗余的 else 分支,简化代码:

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

Expand Down
Loading