Skip to content

Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout - #1223

Open
nordicnode wants to merge 1 commit into
CodebuffAI:mainfrom
nordicnode:perf-indentation-recovery-fast-bail
Open

Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout#1223
nordicnode wants to merge 1 commit into
CodebuffAI:mainfrom
nordicnode:perf-indentation-recovery-fast-bail

Conversation

@nordicnode

Copy link
Copy Markdown

Optimize tryToDoStringReplacementWithExtraIndentation with first-line fast bailout

Summary

• In packages/agent-runtime/src/generate-diffs-prompt.ts, optimized tryToDoStringReplacementWithExtraIndentation to 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 searchLines once outside the loops.
• Added a fast-bailout check: if firstNonEmptyLine is present, it tests whether prefix + firstNonEmptyLine exists in oldFileContent. 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

  • Failed Indentation Recovery (50 lines, 1,000 runs):
    • Before: 96.10 ms
    • After: 6.49 ms (14.8x faster)
  • Successful Matches: Identical output and smallest-indentation preference preserved.

2. Test Suite & Hygiene

  • bun test packages/agent-runtime/src/__tests__/generate-diffs-prompt.test.ts passed 8/8 tests (0 fail).
  • PR hygiene checks passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant