Repository navigation
perf(runtime): take a state machine's transition without allocating - #1356
Merged
Merged
Conversation
Co-Authored-By: jason.han <hanhuijun@gmail.com>
Contributor
Author
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
This was referenced Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
The 0.11.0 performance record against v0.10.1 (#1337) puts the
StateLoopandExecuteStaterows +12 % in allocations, bisected by exact allocation counts to #1136 (transitive order dependence). Half of that is the statement order rebuilt per entry, fixed in #1355; the other half is here: taking a transition made two closures on every transition taken.StateExecutor.takingput the executor under the choice points the guard stood on throughContext.underControl, which returned a closure releasing the control, and itself returned a closure calling that and putting the firing transition and notes back — two heap allocations per transition taken, on every route resolved, every firing, every resumed cascade; a machine looping on a transition ten thousand times made twenty thousand of them.underControlnow returns the position to release (releaseControl(n)), andtakingreturns atakenvalue whoserestoremethod puts the executor back, so taking a transition allocates nothing of its own. The sevenunderControland fivetakingcall sites change form only (defer ctx.releaseControl(ctx.underControl(by)),defer e.taking(trans, notes).restore()).No behaviour changes: the same control is held for the same extent and the same state restored.
How it was verified
Failing-before test
TestTakingATransitionAllocatesNothing(internal/exec/runtime/taking_allocs_test.go): taking and restoring each of a machine's three transitions allocates 6.00 objects on develop (two per transition, asexec.taking(tr, nil)()), 0 on this branch. The per-event differential test of #1355 (one more event of a counting state machine) goes 133.50 → 127.50 allocations on this branch alone.A/B over six interleaved rounds (
go test -bench -count 1per round,nice; pre-#1136 d5e2317 | develop a662c2b | this branch | this branch + #1355). Allocations are exact; the machine was loaded, so only the allocation column is stated:ExecuteStateStateLoop/count50StateLoop/count500StateLoop/count5000ActionLoop/for1000ActionChain/chain1000RunStateMachine/elements=*This branch takes the state rows from +12 % to +7 %; with #1355 they are within +0.9 % of pre-#1136 (the last six objects per thousand events are #1276's declared-types lookup). The action rows are #1355's and untouched here; the REPL's
RunStateMachine(+2 objects, #1315's) is not this cost.Gates:
gofmt -l .empty,go vet ./...,go build ./...,go test ./internal/exec/runtime,python3 scripts/changelog.py check.Checklist
make testandmake lintpass locally (the package above; the full suite runs in CI)changes/unreleased/take-transition-without-closures.fixed.md, not as an edit toCHANGELOG.mdmake docs-countsrun if a gate count moved (none moved)F4,K5) in the body, docs, or changelogLink to Devin session: https://nasa-jpl-demo.devinenterprise.com/sessions/cf22d7a746814d20b2f0eaf11682fef9
Open in Devin Desktop: https://nasa-jpl-demo.devinenterprise.com/desktop/session/cf22d7a746814d20b2f0eaf11682fef9?variant=devin
Requested by: @HuiJun