Skip to content

Bound lines array in getLastNVisualLines to prevent unbounded memory growth - #1220

Open
nordicnode wants to merge 1 commit into
CodebuffAI:mainfrom
nordicnode:perf-visual-lines-bounded-buffer
Open

Bound lines array in getLastNVisualLines to prevent unbounded memory growth#1220
nordicnode wants to merge 1 commit into
CodebuffAI:mainfrom
nordicnode:perf-visual-lines-bounded-buffer

Conversation

@nordicnode

Copy link
Copy Markdown

Bound lines array in getLastNVisualLines to prevent unbounded memory growth

Summary

• In cli/src/utils/text-layout.ts, bound the lines accumulator in getLastNVisualLines to at most n lines.
• Previously, getLastNVisualLines accumulated every wrapped line from character 0 to the end of the text in lines: string[], only to slice lines.slice(-n) at the very end and discard the rest. When rendering long reasoning blocks, assistant messages, or terminal logs (e.g. 500+ lines), this held hundreds of unnecessary line strings in memory and required an additional .slice() array allocation.
• Updated pushLine() to evict the oldest line whenever lines.length === n and set hasMore = true, ensuring memory used by lines is strictly bounded by $O(n)$ ($n \le 5$ in preview) rather than $O(\text{total lines})$.
• Added comprehensive unit tests in cli/src/utils/__tests__/text-layout.test.ts covering zero column/line inputs, small line counts, wrapping boundaries, and multi-hundred line inputs.

Test plan

[✓] bun test --config=/dev/null src/utils/__tests__/text-layout.test.ts — 11 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