Skip to content

perf(runtime): allocate less to bracket a feature read - #2388

Merged
HuiJun merged 3 commits into
developfrom
fix/feature-read-allocations
Nov 19, 2026
Merged

HuiJun merged 3 commits into
developfrom
fix/feature-read-allocations

Conversation

@HuiJun

@HuiJun HuiJun commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

Reading a feature value brackets the read in a run of its own, and that bracket allocated more than it needs. Since the instance graph of a gRPC answer is walked with Context.Traverse and then serialized, every feature value of the graph is read twice, so the fixed cost per read shows up directly in BenchmarkGRPCVerifyConstraint: with #2378, #2379 and #2380 applied it still allocates 26% more than v0.12.1.

Three changes, none of which alters what a read does:

  • beginRun returns one closure instead of composing three (nestRun's, syncBoundary's and its own).
  • runState.calcUsageRuns is made when a run first evaluates a calc usage rather than for every run; most runs never write it.
  • resolveBindings spells out the owner feature path only at an owner that declares bindings, the one place the path is read, instead of concatenating at every owner on the way up.

Against v0.12.1, with the three pull requests above merged in, GRPCVerifyConstraint goes from +26.45% to +3.07% allocs/op and from +27.69% to +16.66% B/op; Instantiate from +2.47% to +1.11% allocs/op.

Not addressed here: the time of GRPCVerifyConstraint and Instantiate and the bytes of Instantiate do not move measurably. What remains there is the second read of each feature value that Traverse adds to the instance graph, and per-type work done the first time a type is instantiated (lowering a state machine's expressions into its ExprTable, the reachingSubsetters index).

How it was verified

go build ./..., go vet ./... and gofmt -l . are clean. go test -count=1 ./internal/exec/... ./internal/frontend/... ./tests/grpc/... ./tests/perf/... passes, apart from the two training-corpus tests of internal/exec/solve, which did not find the corpus in the worktree they ran in. TestReleaseInsideAnotherRunEndsThePausedRunsOwn now makes the table it writes to, since a fresh run no longer carries one.

Benchmarks: tests/perf, six interleaved rounds, on develop with #2378, #2379 and #2380 merged, before and after this change:

                       │    before     │               after                │
                       │    sec/op     │    sec/op     vs base              │
Instantiate-8              603.6m ± 5%   609.1m ± 16%       ~ (p=0.589 n=6)
GRPCEvaluate-8             9.610µ ± 9%   9.654µ ± 12%       ~ (p=1.000 n=6)
GRPCVerifyConstraint-8     2.344m ± 5%   2.247m ± 13%       ~ (p=0.310 n=6)
                       │     B/op      │     B/op      vs base                │
Instantiate-8             179.3Mi ± 0%   178.9Mi ± 0%  -0.19% (p=0.002 n=6)
GRPCEvaluate-8            12.12Ki ± 0%   12.12Ki ± 0%       ~ (p=1.000 n=6)
GRPCVerifyConstraint-8    983.1Ki ± 0%   898.2Ki ± 0%  -8.64% (p=0.002 n=6)
                       │   allocs/op   │  allocs/op   vs base                 │
Instantiate-8              1.361M ± 0%   1.343M ± 0%   -1.32% (p=0.002 n=6)
GRPCEvaluate-8              149.0 ± 0%    149.0 ± 0%        ~ (p=1.000 n=6)
GRPCVerifyConstraint-8     18.84k ± 0%   15.36k ± 0%  -18.49% (p=0.002 n=6)

The same, v0.12.1 against this change:

                       │   v0.12.1    │               after                 │
                       │    sec/op    │    sec/op     vs base               │
Instantiate-8             515.9m ± 8%   609.1m ± 16%  +18.09% (p=0.002 n=6)
GRPCEvaluate-8           10.797µ ± 8%   9.654µ ± 12%  -10.59% (p=0.015 n=6)
GRPCVerifyConstraint-8    1.799m ± 4%   2.247m ± 13%  +24.90% (p=0.002 n=6)
                       │     B/op     │     B/op      vs base               │
Instantiate-8            153.1Mi ± 0%   178.9Mi ± 0%  +16.90% (p=0.002 n=6)
GRPCEvaluate-8           10.20Ki ± 1%   12.12Ki ± 0%  +18.82% (p=0.002 n=6)
GRPCVerifyConstraint-8   769.9Ki ± 0%   898.2Ki ± 0%  +16.66% (p=0.002 n=6)
                       │  allocs/op  │  allocs/op   vs base               │
Instantiate-8            1.328M ± 0%   1.343M ± 0%   +1.11% (p=0.002 n=6)
GRPCEvaluate-8            129.0 ± 0%    149.0 ± 0%  +15.50% (p=0.002 n=6)
GRPCVerifyConstraint-8   14.90k ± 0%   15.36k ± 0%   +3.07% (p=0.002 n=6)

Checklist

  • make test and make lint pass locally — not run in full; the packages above were run instead
  • Tests added or updated for the change
  • Documentation extended where it already covers the surface (none does)
  • Changelog entry added as changes/unreleased/<slug>.<section>.md — none: no behavior a user reads about changes
  • baselines regenerated and make docs-counts run if a gate count moved (none moved)
  • No internal work-item labels (waves, slices, F4, K5) in the body, docs, or changelog

🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review

Reading a feature value brackets the read in a run. The bracket allocated
three closures where one does, a calc-usage table most runs never write,
and spelled out the owner path at every owner on the way up although only
an owner declaring bindings reads it.

Co-Authored-By: jason.han <hanhuijun@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

Co-Authored-By: jason.han <hanhuijun@gmail.com>

@devin-ai-integration devin-ai-integration 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.

Devin Review found 1 finding. Devin automatically fixed it.

Fixed by Devin (1)
  • Run-state comment exceeds two-line limit — Comment shortened to two lines.

View findings in Devin Review · Follow Devin's work

Devin Review

@HuiJun
HuiJun added this pull request to the merge queue Nov 19, 2026
Merged via the queue into develop with commit 6ceafd6 Nov 19, 2026
32 checks passed
@HuiJun
HuiJun deleted the fix/feature-read-allocations branch November 19, 2026 12:52
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