Skip to content

perf(interp): reuse generic array backing storage across resets - #192

Merged
siyul-park merged 1 commit into
mainfrom
perf/interp-array-backing-reuse
Aug 16, 2026
Merged

siyul-park merged 1 commit into
mainfrom
perf/interp-array-backing-reuse

Conversation

@siyul-park

@siyul-park siyul-park commented Aug 16, 2026 •

Copy link
Copy Markdown
Owner

Split out of #191, which had accumulated several unrelated changes. This one is independent of the JIT work in that stack and can land on its own.

Problem

Every Run that built a generic array allocated a fresh backing slice. A reused Interpreter paid that allocation again after each Reset, even though the array header it was attached to was already being recycled.

Change

Reset now clears a reclaimed generic array's backing storage instead of dropping it, and ARRAY_NEW/ARRAY_NEW_DEFAULT reuse that storage when the requested length fits.

Semantics are unchanged:

  • a reused backing is cleared to the element type's zero
  • an array that escaped the run — detached by pop, or reclaimed before the reset — keeps its own storage and is never handed to a later run

The resets generic array contents Reset subtest went with it. The two subtests beside it, preserves arrays detached by pop and preserves arrays reclaimed before reset, already assert both halves of that contract: the cleared elements and the non-reuse of an escaped header.

Results

BenchmarkMemory_AllocationGraph, Apple M4 Pro, darwin/arm64, Go 1.26.2, go test -run='^$' -bench=. -benchtime=300ms -count=3, median of three:

mode before after
default 5.64 µs, 1024 B/op, 128 allocs/op 5.03 µs, 0 B/op, 0 allocs/op
threaded 4.99 µs, 1024 B/op, 128 allocs/op 4.31 µs, 0 B/op, 0 allocs/op
jit 5.63 µs, 1024 B/op, 128 allocs/op 5.00 µs, 0 B/op, 0 allocs/op

The zero-allocation result was re-confirmed in a fresh run on this branch.

Test plan

  • go test -race ./...
  • make check-generated
  • go vet ./...
  • BenchmarkMemory_AllocationGraph re-measured on branch

Refs #164

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance Improvements
    • Improved generic array creation and reuse to reduce unnecessary allocations.
    • Reuses available array storage when creating arrays of suitable sizes.
    • Clears retained array storage more thoroughly during interpreter resets, helping prevent stale data from being retained.
  • Reliability
    • Preserved existing behavior for detached arrays and other pooling exclusions while improving array lifecycle management.

Every Run that built a generic array allocated a fresh backing slice, so a
reused Interpreter paid the allocation again on each Reset even though the
array header it was attached to was already being recycled.

Reset now clears a reclaimed generic array's backing storage instead of
dropping it, and ARRAY_NEW/ARRAY_NEW_DEFAULT reuse that storage when the
requested length fits. Semantics are unchanged: a reused backing is cleared
to the element type's zero, and an array that escaped the run - detached by
pop, or reclaimed before the reset - keeps its own storage and is never
handed to a later run.

The "resets generic array contents" Reset subtest went with it: the two
subtests beside it, "preserves arrays detached by pop" and "preserves arrays
reclaimed before reset", already assert both halves of that contract - the
cleared elements and the non-reuse of an escaped header.

BenchmarkMemory_AllocationGraph, Apple M4 Pro, darwin/arm64, Go 1.26.2,
go test -run='^$' -bench=. -benchtime=300ms -count=3, median of three:

  default   5.64 us, 1024 B/op, 128 allocs/op -> 5.03 us, 0 B/op, 0 allocs/op
  threaded  4.99 us, 1024 B/op, 128 allocs/op -> 4.31 us, 0 B/op, 0 allocs/op
  jit       5.63 us, 1024 B/op, 128 allocs/op -> 5.00 us, 0 B/op, 0 allocs/op

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 726f1bf3-b707-4b1d-aa83-924bc4153999

📥 Commits

Reviewing files that changed from the base of the PR and between 44dab81 and 5bcb356.

📒 Files selected for processing (5)
  • docs/memory-model.md
  • internal/cmd/geninterp/lower.go
  • interp/interp.go
  • interp/interp_test.go
  • interp/threaded.go
💤 Files with no reviewable changes (1)
  • interp/interp_test.go

📝 Walkthrough

Walkthrough

Generic array pooling now retains and clears backing storage during reset. A new sized constructor reuses that storage when possible. Generated and threaded allocation paths use the constructor directly.

Changes

Generic array pooling

Layer / File(s) Summary
Reset pooled array state
interp/interp.go, docs/memory-model.md
Reset clears retained backing capacity, removes the array type, and preserves the backing allocation for pooling. The memory model documents the updated behavior.
Sized array construction
interp/interp.go
newArraySized reuses pooled array headers and backing storage when capacity is sufficient, and allocates fresh storage when necessary.
Allocation call sites and validation
internal/cmd/geninterp/lower.go, interp/threaded.go, interp/interp_test.go
Generic array creation paths call newArraySized directly. The reset-reuse test for generic array headers was removed.

Estimated code review effort: 2 (Simple) | ~15 minutes

Merge Risk: ⚪ Minimal · up to 5bcb3

The change reuses cleared generic-array backing storage while preserving independent storage for escaped arrays; no actionable merge-blocking risk remains after normal checks and review.

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: reusing generic array backing storage across interpreter resets.
Description check ✅ Passed The description explains the problem, implementation, semantics, benchmark results, related issues, and test plan in sufficient detail.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/interp-array-backing-reuse

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.45.1)
interp/threaded.go

ast-grep timed out on this file


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@siyul-park
siyul-park merged commit 159b6c0 into main Aug 16, 2026
4 of 5 checks passed
@siyul-park
siyul-park deleted the perf/interp-array-backing-reuse branch August 16, 2026 00:57
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