Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout - #1223
Open
nordicnode wants to merge 1 commit into
Open
Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout#1223nordicnode wants to merge 1 commit into
nordicnode wants to merge 1 commit into
Conversation
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.
Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout
Summary
• In
packages/agent-runtime/src/generate-diffs-prompt.ts, optimizedtryToDoStringReplacementWithExtraIndentationto eliminate redundant string splits and array joins during indentation mismatch recovery.• Previously, the recovery loop evaluated 1..12 space indents followed by 1..6 tab indents (up to 18 iterations). On every iteration, it called
searchContent.split('\n'), mapped over all lines, and joined them back together. For a 50-line block on non-matching text, this produced 18 full string splits and 900 line mappings.• Pre-split
searchLinesonce outside the loops.• Added a fast-bailout check: if
firstNonEmptyLineis present, it tests whetherprefix + firstNonEmptyLineexists inoldFileContent. If the first indented line is not present, the full block cannot possibly match, bypassing string formatting and joining for that indentation level.• Evaluated
replaceContent.split('\n')lazily only when a match is found.• Verified 100% exact parity across spaces, tabs, leading/trailing empty lines, and partial matches.
• Added unit test coverage for first-line matching with subsequent line mismatch, and leading empty lines in
packages/agent-runtime/src/__tests__/generate-diffs-prompt.test.ts.Verification & Benchmark Results
1. Benchmark
2. Test Suite & Hygiene
bun test packages/agent-runtime/src/__tests__/generate-diffs-prompt.test.tspassed 8/8 tests (0 fail).