Optimize extractValueForKey with early guard and streaming line scan - #1215
Open
nordicnode wants to merge 1 commit into
Open
Optimize extractValueForKey with early guard and streaming line scan#1215nordicnode 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 extractValueForKey with early guard and streaming line scan
Summary
• In
cli/src/utils/implementor-helpers.ts, optimizeextractValueForKeyto avoid allocating an array of all lines on every invocation.•
extractValueForKeyis called repeatedly for every tool and agent block during message rendering to extractfile,message,unifiedDiff,patch, anderrorMessage.• Previously,
extractValueForKeyunconditionally calledoutput.split('\n'). For multi-thousand line tool results or diffs, this allocated an array of thousands of strings and performed regex checks across all lines even when the searched key was completely absent, churning megabytes of garbage per render.• Added an instant substring guard (
if (!output || !output.includes(key + ':')) return null) and replacedoutput.split('\n')with an index-based line scanner (indexOf('\n')). Keys located early in the output (e.g.message: ...on line 1 or 2) now return immediately without parsing or allocating the remainder of the output.• Benchmark: In a 5,000-line output, lookups for missing keys dropped from 11.02ms down to 0.20ms (55x speedup), and finding a key on line 1 dropped from 17.41ms down to 0.31ms (56x speedup across 100 runs).
• Adds unit tests in
cli/src/utils/__tests__/implementor-helpers.test.tsverifying colon-less key rejection, line termination handling, and performance on large outputs.Test plan
[✓]
bun test --config=/dev/null src/utils/__tests__/implementor-helpers.test.ts— 102 pass, 0 fail[✓]
bun run --cwd cli typecheck— 0 errors[✓] PR hygiene check passed