Skip to content

Optimize extractValueForKey with early guard and streaming line scan - #1215

Open
nordicnode wants to merge 1 commit into
CodebuffAI:mainfrom
nordicnode:perf-extract-value-for-key
Open

Optimize extractValueForKey with early guard and streaming line scan#1215
nordicnode wants to merge 1 commit into
CodebuffAI:mainfrom
nordicnode:perf-extract-value-for-key

Conversation

@nordicnode

Copy link
Copy Markdown

Optimize extractValueForKey with early guard and streaming line scan

Summary

• In cli/src/utils/implementor-helpers.ts, optimize extractValueForKey to avoid allocating an array of all lines on every invocation.
extractValueForKey is called repeatedly for every tool and agent block during message rendering to extract file, message, unifiedDiff, patch, and errorMessage.
• Previously, extractValueForKey unconditionally called output.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 replaced output.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.ts verifying 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

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