fix(edit-tool): eliminate token amplification and hang on large files - #2144
Open
xielixing wants to merge 1 commit into
Open
fix(edit-tool): eliminate token amplification and hang on large files#2144xielixing wants to merge 1 commit into
xielixing wants to merge 1 commit into
Conversation
Remove redundant dry-run from validate_input that duplicated the full apply_edit_to_content call (file read + candidate generation) which call_impl already performs. Add a fast path in apply_edit_to_content that tries the exact old_string match before generating whitespace-normalization candidates, avoiding expensive find_actual_string character-by-character scanning when the exact match succeeds. Fixes GCWing#1650
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1650
The Edit tool caused extreme token amplification (957K input tokens) and a 1m23s hang on a 1185-line file. Two root causes:
Fix 1: Remove redundant dry-run from
validate_inputvalidate_input()infile_edit_tool.rsran a full dry-runapply_edit_to_content()— reading the file and generating all whitespace-normalization candidates — and thencall_impldid the exact same work again when actually applying the edit. This doubled the file reads and candidate generation on every edit.Fix: Removed the dry-run block from
validate_input. The edit is already validated duringcall_implviaapply_edit_to_content, so the dry-run was purely redundant work.Fix 2: Add fast path before candidate generation in
apply_edit_to_contentedit_string_candidates()generates whitespace-normalization candidates (tabs↔spaces at width 2 and 4), each callingfind_actual_string()which does O(n*m) char-by-char scanning. ALL candidates were generated upfront before any matching, even when the exactold_stringalready matched the file content.Fix: Added a fast path that tries the exact
old_string/new_stringmatch viaapply_match_and_replace()before callingedit_string_candidates(). If the exact match succeeds (the common case), it returns immediately — skipping all candidate generation and expensive scanning. If the exact match fails with "not found", it falls through to the existing candidate loop (slow path unchanged for edge cases).Validation
cargo check -p tool-runtime -p bitfun-core— passedcargo test -p tool-runtime -- fs::edit_file— 21/21 passedcargo test -p bitfun-core -- file_edit_tool— 3/3 passedImpact
For the reported 1185-line file edit, the common case (exact match) now completes in a single
apply_match_and_replacecall instead of generating and scanning multiple whitespace-normalization candidates throughfind_actual_string. Combined with removing the redundant dry-run, this eliminates the token amplification and hang.