Skip to content

perf(session): reuse JSONL buffers while loading saved reviews - #2063

Open
Afloat16 wants to merge 2 commits into
alibaba:mainfrom
Afloat16:perf/session-reuse-jsonl-buffer-20261123
Open

Afloat16 wants to merge 2 commits into
alibaba:mainfrom
Afloat16:perf/session-reuse-jsonl-buffer-20261123

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Inspecting or resuming a saved review reads every JSONL record, including large LLM responses that the summary and checkpoint decoders do not retain. Both session readers currently use ReadBytes, which allocates a new complete line and copies the buffered fragments for each response.

Use one private record reader for these two immediate-decoding paths. Short records borrow the input buffer; long records use an overflow buffer reused within that file traversal. JSON decoding still produces independently owned metadata and checkpoints. A traversal retains its largest overflow buffer until it finishes; there is no pool or cache shared between sessions.

The existing parsing decisions remain at the call sites: display accepts a valid unterminated final record, resume drops that tail as a torn write, strict resume rejects malformed complete records, and review resume skips them. The viewer's retained-record reader is unchanged.

Type of Change

  • Performance improvement

How Has This Been Tested?

The functional test writes a complete review through SessionHistory, including long checkpoints on either side of 32 large response records and a final coverage manifest. It checks detail output, retained comment contents, both resume modes, reusable checkpoints, CRLF, a long unterminated tail, and a long malformed complete line. These public-path checks pass before and after the change. A reader contract test compares records and errors against ReadBytes with short reads, buffer boundaries, invalid bytes, EOF returned with data, and injected read errors.

The benchmark uses actual saved sessions containing 64 response records and verifies complete summary or resume metadata on every iteration. Median of five runs with Go 1.26.10, linux/amd64, GOMAXPROCS=1, and a 150 ms benchmark duration:

Response content per record Loader Before B/op After B/op Before allocs/op After allocs/op
1 KiB Summary 326,057 201,057 1,657 1,523
1 KiB Resume 272,249 147,249 1,516 1,382
64 KiB Summary 9,186,751 423,589 2,937 1,528
64 KiB Resume 9,132,946 369,780 2,796 1,387
256 KiB Summary 34,524,728 1,324,800 6,138 1,533
256 KiB Resume 34,470,898 1,270,997 5,996 1,392

A separate Linux probe generates its fixtures in another process and compares five fresh reader processes per implementation, checking identical SHA256 hashes of the complete decoded results. With 64 responses of 256 KiB, median peak RSS was 17,224 to 13,512 KiB for summary and 16,968 to 13,384 KiB for resume. With 1 MiB responses, allocated bytes fell from about 136 MB to 5.4 MB, but peak RSS changed only slightly. The performance claim is reduced allocation volume; timing and peak RSS depend on the workload and runtime.

  • make license-add check build — passed.
  • Before/after public-path tests and allocation benchmarks — passed; race detection disabled only for benchmark measurement.
  • Full make test with the race detector — passed across all 28 packages.
  • make coverage — passed, 91.5% overall coverage (90% required); internal/session is 90.7%.
  • The inherited telemetry sampler is set to always_on for the test process.

The required ocr review --audience agent --background "..." command was attempted, but could not start because no valid LLM endpoint is configured.

Core contribution owner: @Afloat16.

AI/LLM disclosure required by this repository: ChatGPT (OpenAI GPT-6 Astra Pro).

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 3 selected item(s).

@Afloat16

Afloat16 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated the branch to 591bff959c6667b739acfea79e4756b5d2f8b781, integrating current main c445fb4e377d and resolving the list.go conflict. The resolution keeps main's readSessionRecords(io.Reader, ...) entry point and uses the reusable JSONL reader inside it; the original parsing and retained-record behavior remain covered by the existing tests. GitHub now reports no merge conflicts.

Validation ran on this exact commit with Go 1.26.11 on Linux before updating the PR:

  • make check
  • Focused race tests for internal/session, internal/viewer and cmd/opencodereview
  • Full make test with the race detector
  • make coverage: 91.6% overall against the 90% threshold; internal/session: 91.0%
  • make build

All passed. Workflow, source identity and complete logs. The allocation/RSS measurements in the description remain the original before/after measurements; they were not remeasured for this integration commit.

All upstream checks on 591bff959c6667b739acfea79e4756b5d2f8b781 have now passed: CI, including native Windows and all cross-compilation targets, and CodeQL for Actions, Go and JavaScript/TypeScript. GitHub's merge panel shows all checks passed; the remaining gate is an approving review from someone with write access. Could a maintainer review this for merge?

This branch has not been deployed

No deployments
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