Skip to content

perf: amortize ReaderParser retained-buffer growth - #294

Open
anishpentyala wants to merge 2 commits into
buger:masterfrom
anishpentyala:fix/reader-buffer-growth
Open

anishpentyala wants to merge 2 commits into
buger:masterfrom
anishpentyala:fix/reader-buffer-growth

Conversation

@anishpentyala

Copy link
Copy Markdown

Description

When a retained value spans many read chunks, readMore currently allocates exactly enough room for one more chunk and copies the entire retained prefix each time. This makes copying quadratic in the number of chunks.

Grow capacity geometrically instead, using the required capacity when that is larger. The requested read size and the existing discard behavior are unchanged. The tradeoff is spare retained-buffer capacity, up to roughly 2x the capacity immediately needed in the worst case.

Added a deterministic growth regression, compatibility controls for long strings, escaped values, composite values, long keys, ArrayEach callbacks and reader errors, plus a retained-value benchmark. The growth test fails on the baseline with 128 reallocations for 128 reads at both tested chunk sizes, and passes after the change.

Benchmark before / after

Local exploratory medians from five runs of three iterations each, Go 1.26.6 linux/amd64, GOMEMLIMIT=256MiB GOMAXPROCS=2, benchmark BenchmarkReaderParserRetainedValue with -benchmem -benchtime=3x -count=5:

Retained string / chunk ns/op before ns/op after B/op before B/op after allocs/op before allocs/op after
1 MiB / 1 KiB 132,047,804 2,584,447 542,026,128 4,185,120 1,027 13
1 MiB / 64 KiB 4,318,673 2,388,069 10,027,040 4,128,800 18 7
8 MiB / 64 KiB 232,058,952 22,448,698 549,519,392 33,488,928 130 10

Validation

  • Focused ReaderParser tests and focused ReaderParser race tests pass.
  • The existing 100 MiB skipped-prefix bounded-window test passes.
  • Baseline and candidate have the same existing TestOracleSetPr286Regression failures, tracked separately in Test failures for TestOracleSetPr286Regression #288 and test: assert OOB Set is retrieved at the last index #290. The full ordinary suite passes with that known failing test excluded. Baseline also excludes the new growth regression that demonstrates this fix.
  • Full race testing was killed by the local environment, so no full-race pass is claimed.
  • go vet has the same existing unreachable-code and reflect.StringHeader warnings on baseline and candidate. A 386 build is blocked on both by existing integer-overflow constants in obligation_property_test.go.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T23:16:11.120541Z 1dd5f41 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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