Optimize extractValueForKey with early guard and streaming line scan - #1215
Optimize extractValueForKey with early guard and streaming line scan#1215nordicnode wants to merge 1 commit into
Conversation
|
Good change. Correctness check: the early guard The streaming rewrite of the pipe-continuation block (multi-line Tests are appropriately scoped (colon-less rejection, no-trailing-newline extraction, 5000-line perf/correctness) and live in the existing Minor nit for future PRs: the benchmark numbers in the description aren't reproducible from the test file itself (no timing assertions), so if a reviewer wants performance regression protection, consider adding a rough timing bound test — not blocking here. |
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