Skip to content

perf(kaos): stream readLines to avoid loading whole file into memory - #962

Closed
7Sageer wants to merge 1 commit into
MoonshotAI:mainfrom
7Sageer:perf/read-tail-streaming
Closed

perf(kaos): stream readLines to avoid loading whole file into memory#962
7Sageer wants to merge 1 commit into
MoonshotAI:mainfrom
7Sageer:perf/read-tail-streaming

Conversation

@7Sageer

@7Sageer 7Sageer commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No related issue. The problem is explained below.

Problem

Reading a large text file with the Read tool (including tail mode via a negative line_offset) loads the entire file into memory. LocalKaos.readLines used readFile + split('\n'), which holds the whole file as a Buffer, a decoded string, and a split array at the same time. On a 100 MB file this peaks at ~330 MB of resident memory even when only the last 100 lines are needed, and grows linearly with file size.

What changed

LocalKaos.readLines now reads the file in 64 KiB chunks and splits on the LF byte, decoding one line at a time. Peak memory is bounded by the chunk size plus the longest line rather than the whole file (~57 MB for the same 100 MB file, most of which is the Node.js baseline).

Behavior is unchanged: readTail / readForward keep their ring buffer, the Total lines in file report, and the MAX_LINES / MAX_BYTES caps. CRLF, lone CR, empty files, and the strict / replace / ignore decode modes are preserved. Added tests for line-ending fidelity, multi-chunk files, multibyte characters across the chunk boundary, and the three decode modes.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Read files in 64KiB chunks and split on the LF byte so peak memory stays bounded by the longest line instead of the whole file. readTail/readForward keep the same behavior and Total lines reporting.
@changeset-bot

changeset-bot Bot commented Jun 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f6d584e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jun 22, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@f6d584e
npx https://pkg.pr.new/@moonshot-ai/kimi-code@f6d584e

commit: f6d584e

@itxaiohanglover itxaiohanglover left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good performance fix — streaming file reads in 64KB chunks instead of loading the entire file into memory is the right approach for large files. The pending buffer handling for partial lines is correct. Changeset included.

@7Sageer 7Sageer mentioned this pull request Jun 22, 2026
5 tasks
@7Sageer 7Sageer closed this Jun 22, 2026
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.

2 participants