Skip to content

perf(interp): pool small structs - #183

Merged
siyul-park merged 11 commits into
mainfrom
perf/175-struct-pool
Aug 10, 2026
Merged

siyul-park merged 11 commits into
mainfrom
perf/175-struct-pool

Conversation

@siyul-park

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

Copy link
Copy Markdown
Owner

Summary

  • Reuse small types.Struct values through an interpreter-owned pool
  • Remove per-release worklist allocation by reusing releaseWork
  • Simplify struct reuse by eliminating the pending pool path
  • Regenerate threaded opcode lowering and add coverage for types.Struct.Reset

Results

  • StructTreeWalk and BinaryTrees now run with 0 B/op and 0 allocs/op
  • BinaryTrees improved from ~1.35x CPython to ~1.22x CPython on the compare benchmark set
  • AllocationGraph and PermutationFlips remain stable or improved after the refactor

Validation

  • go test ./...
  • go test -tags=compare -run '^$' -bench='^(BenchmarkMemory_(StructTreeWalk|BinaryTrees)|BenchmarkControl_(IterativeFib|Sieve)|BenchmarkMemory_(AllocationGraph|PermutationFlips))$' -benchmem -benchtime=300ms -count=3 ./benchmarks

Summary by CodeRabbit

  • Performance

    • Improved memory reuse for frequently created small structures and arrays.
    • Reduced temporary allocations during structure creation and reference cleanup.
    • Added tracking for live and peak allocation usage.
  • Bug Fixes

    • Ensured reused structures are fully reset, including stored field values and storage.
  • Documentation

    • Refreshed benchmark results, methodology, performance comparisons, and correctness notes.
  • Tests

    • Added coverage verifying that reset structures retain their type while clearing previous values.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@siyul-park, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 15 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b6f4ae8d-815a-40e0-9322-fc6a9217ba11

📥 Commits

Reviewing files that changed from the base of the PR and between e8727ca and 936db2a.

📒 Files selected for processing (5)
  • docs/benchmarks.md
  • interp/interp.go
  • interp/interp_test.go
  • interp/reuse.go
  • types/struct_test.go
📝 Walkthrough

Walkthrough

The change adds resettable struct storage and interpreter-owned pooling for small structs. Struct construction paths use the pool-aware helper. Allocation tracking and GC worklist reuse support the lifecycle changes. Benchmark documentation contains refreshed August 2026 measurements.

Changes

Struct reuse

Layer / File(s) Summary
Resettable struct storage
types/struct.go, types/struct_test.go
Struct.Reset assigns the type, clears inline storage, and allocates storage for larger structs. Tests verify that resetting a small struct clears its fields.
Interpreter struct pooling
interp/interp.go
The interpreter tracks allocations, reuses small structs, trims pools during reset, returns reclaimed structs to the pool, and clears the pool on close. GC traversal uses interpreter-owned worklist storage.
Interpreter construction wiring
internal/cmd/geninterp/lower.go, interp/threaded.go
Generated and threaded struct construction paths use i.newStruct instead of types.NewStruct.
Benchmark documentation refresh
docs/benchmarks.md
Benchmark methodology, measurements, comparison tables, threaded results, and reference traversal data now reflect August 2026 runs.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Construction
  participant Interpreter
  participant Struct
  participant GC
  Construction->>Interpreter: newStruct(type)
  Interpreter->>Struct: Reset(type)
  Struct-->>Interpreter: Initialized struct
  Interpreter->>GC: Track allocation
  GC->>Interpreter: Reclaim small struct
  Interpreter->>Interpreter: Return struct to pool
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the primary change: pooling small interpreter structs.
Description check ✅ Passed The description clearly covers the changes, results, and validation, but it omits the template's Related Issues and Additional Information sections.
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/175-struct-pool

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
types/struct_test.go (3)

43-43: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Do not assert the types.Struct.Data representation.

require.Len(t, s.Data, 2) checks storage layout, not public behavior. Remove it unless the length is a documented public contract.

As per coding guidelines: Tests must not access private symbols or representations; assert internal invariants through public observable behavior, generated output, or executable boundaries.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@types/struct_test.go` at line 43, Remove the require.Len assertion on s.Data
from the test, and rely on public behavior or generated output assertions
instead of checking the internal types.Struct.Data representation.

Source: Coding guidelines


36-44: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Give types.Struct.Reset its own top-level test.

Move this sub-case to TestStruct_Reset. Keep its scenarios under t.Run.

As per coding guidelines: **/*_test.go: Define one top-level test per public symbol using Test<Func> or Test<Type>_<Method>, and put sub-cases under t.Run rather than separate parallel top-level tests.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@types/struct_test.go` around lines 36 - 44, Move the “reset clears small
storage” scenario from its current parent test into a new top-level
TestStruct_Reset function, keeping the scenario wrapped in t.Run. Preserve the
existing assertions and setup for types.Struct.Reset unchanged.

Source: Coding guidelines


36-44: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover the other types.Struct.Reset behaviors.

The test covers only the small two-field I32 path. Add sub-cases for a struct with more than four fields and for a reference field. Assert the reset values through Field or Raw.

As per coding guidelines: Tests must follow docs/testing.md, cover public specifications and opcode behavior as required, and assert only public observable behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@types/struct_test.go` around lines 36 - 44, Add sub-cases to the existing
“reset clears small storage” test for a struct with more than four fields and
for a struct containing a reference field, exercising types.Struct.Reset in each
case. Assert the post-reset values only through the public Field or Raw APIs,
following docs/testing.md and preserving the existing small-storage assertions.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@interp/interp.go`:
- Around line 2352-2362: Update the release traversal around the release
function to use a local worklist for each invocation instead of resetting shared
i.releaseWork, preserving pending outer-sweep entries while recursively
releasing child references. Keep shared worklist manipulation limited to callers
or reclaim, or save and restore it across nested release calls.

---

Nitpick comments:
In `@types/struct_test.go`:
- Line 43: Remove the require.Len assertion on s.Data from the test, and rely on
public behavior or generated output assertions instead of checking the internal
types.Struct.Data representation.
- Around line 36-44: Move the “reset clears small storage” scenario from its
current parent test into a new top-level TestStruct_Reset function, keeping the
scenario wrapped in t.Run. Preserve the existing assertions and setup for
types.Struct.Reset unchanged.
- Around line 36-44: Add sub-cases to the existing “reset clears small storage”
test for a struct with more than four fields and for a struct containing a
reference field, exercising types.Struct.Reset in each case. Assert the
post-reset values only through the public Field or Raw APIs, following
docs/testing.md and preserving the existing small-storage assertions.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: fd6d1391-d094-42c7-9478-bc74aceb455a

📥 Commits

Reviewing files that changed from the base of the PR and between 4d3ba6c and 89543ec.

📒 Files selected for processing (5)
  • internal/cmd/geninterp/lower.go
  • interp/interp.go
  • interp/threaded.go
  • types/struct.go
  • types/struct_test.go

Comment thread interp/interp.go Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
interp/interp.go (1)

63-63: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Keep a reusable worklist for GC marking.

At Line 2337, work starts as a nil local slice. When push appends the first marked address, the traversal must grow a backing array. This adds allocation pressure on every non-empty mark traversal and removes reusable interpreter worklist storage. Keep a separate markWork field, reset its length before each traversal, and retain its capacity afterward.

Proposed fix
 type Interpreter struct {
   ...
   work []int
+  markWork []int
 }

 func (i *Interpreter) mark() {
-  var work []int
+  work := i.markWork[:0]
   ...
+  i.markWork = work[:0]
 }

Also applies to: 2337-2343

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@interp/interp.go` at line 63, Replace the interpreter’s local GC marking
worklist with a reusable markWork field. In the marking traversal around push at
line 2337, reset markWork to length zero before each traversal, use it for
queued addresses, and retain its backing capacity for subsequent traversals; add
the field alongside the existing work storage.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/benchmarks.md`:
- Line 74: Update the benchmark summary around the CPython comparison to remove
SortStress from the list of workloads where threaded minivm is faster, and state
that its 1.06 ratio makes it slightly slower according to the ratio definition
on line 215.
- Around line 290-292: Update the threaded-table scope explanation immediately
following the benchmark table to describe the currently listed zero-allocation
array and struct cases, removing references to allocation-bearing array, struct,
and map cases. Keep the explanation aligned with the benchmark rows shown in
docs/benchmarks.md.

---

Nitpick comments:
In `@interp/interp.go`:
- Line 63: Replace the interpreter’s local GC marking worklist with a reusable
markWork field. In the marking traversal around push at line 2337, reset
markWork to length zero before each traversal, use it for queued addresses, and
retain its backing capacity for subsequent traversals; add the field alongside
the existing work storage.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a3ede9bb-e5a7-4510-8bf1-92f40487f79c

📥 Commits

Reviewing files that changed from the base of the PR and between 89543ec and e8727ca.

📒 Files selected for processing (2)
  • docs/benchmarks.md
  • interp/interp.go

Comment thread docs/benchmarks.md Resolved
Comment thread docs/benchmarks.md Resolved
@siyul-park
siyul-park merged commit 8d3dc21 into main Aug 10, 2026
3 checks passed
@siyul-park
siyul-park deleted the perf/175-struct-pool branch August 10, 2026 13:08
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