Skip to content

Compact machine worker loops - #46

Merged
SandroMaglione merged 1 commit into
mainfrom
codex/compact-idle-statechart
Aug 8, 2026
Merged

Compact machine worker loops#46
SandroMaglione merged 1 commit into
mainfrom
codex/compact-idle-statechart

Conversation

@SandroMaglione

@SandroMaglione SandroMaglione commented Aug 8, 2026

Copy link
Copy Markdown
Member

Summary

  • keep each running machine in one generator loop instead of retaining a nested Effect.whileLoop body generator and its closures
  • preserve all mailbox, state publication, invoke lifecycle, command, and emission ordering boundaries
  • allocate and cache the live emitted-event runtime only after a machine actually emits
  • continue relying on Effect's scheduler operation budget for cooperative yielding

Heap-retainer analysis attributed approximately 1.5 KiB per idle statechart to the nested loop and eager emission closures. Four order-balanced local process pairs reduced idle heap by 7.2% and parent-family heap by 6.0%, while improving the measured runtime, observed, and child-send hot paths by 17–26%.

Changeset

  • Added or updated for a library or package-metadata change
  • Not required because this PR does not change src/ or package.json

Validation

  • pnpm check
  • Relevant example checks, when examples changed (not applicable)
  • Reviewed the automated type-performance report, when the public TypeScript API or inference changed; local budgets are unchanged and no public API changed
  • Reviewed the automated runtime-performance report, when runtime behavior changed

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Type performance

Measured with TypeScript 6.0.3 and skipLibCheck=true.

Scenario Base PR Difference
Effect only 55 55 0 (0.0%)
Import effect-machine 55 55 0 (0.0%)
Machine.defineStates (3 states) 2,965 2,965 0 (0.0%)
Machine.make (3 states, 2 events) 8,715 8,715 0 (0.0%)
machine.handle (3 states, 2 transitions) 23,492 23,492 0 (0.0%)
machine.handle (depth 8) 117,075 117,075 0 (0.0%)
machine.handle (depth 12) 129,713 129,713 0 (0.0%)
machine.handle (depth 16) 145,055 145,055 0 (0.0%)
machine.handle (depth 24) 183,851 183,851 0 (0.0%)
machine.handle (wide depth 16) 214,189 214,189 0 (0.0%)
machine.handle (parallel/history/choice) 132,667 132,667 0 (0.0%)
machine.handle (4 successive calls) 128,674 128,674 0 (0.0%)
machine exact input/output/error/services 110,584 110,584 0 (0.0%)
execution adapter readiness 127,854 127,854 0 (0.0%)

Marginal instantiations are measured against the matching setup without that API call:

Scenario Base PR Difference
Import effect-machine 0 0 0
Machine.defineStates (3 states) 2,910 2,910 0 (0.0%)
Machine.make (3 states, 2 events) 5,742 5,742 0 (0.0%)
machine.handle (3 states, 2 transitions) 14,777 14,777 0 (0.0%)
machine.handle (depth 8) 109,601 109,601 0 (0.0%)
machine.handle (depth 12) 121,039 121,039 0 (0.0%)
machine.handle (depth 16) 135,181 135,181 0 (0.0%)
machine.handle (depth 24) 171,577 171,577 0 (0.0%)
machine.handle (wide depth 16) 202,224 202,224 0 (0.0%)
machine.handle (parallel/history/choice) 114,331 114,331 0 (0.0%)
machine.handle (4 successive calls) 113,703 113,703 0 (0.0%)
machine exact input/output/error/services 100,670 100,670 0 (0.0%)
execution adapter readiness 102,092 102,092 0 (0.0%)
Check times (informational)
Scenario Base PR
Effect only 0.03s 0.03s
Import effect-machine 0.03s 0.03s
Machine.defineStates (3 states) 0.10s 0.09s
Machine.make (3 states, 2 events) 0.15s 0.15s
machine.handle (3 states, 2 transitions) 0.21s 0.20s
machine.handle (depth 8) 0.49s 0.48s
machine.handle (depth 12) 0.53s 0.51s
machine.handle (depth 16) 0.57s 0.57s
machine.handle (depth 24) 0.66s 0.64s
machine.handle (wide depth 16) 0.72s 0.70s
machine.handle (parallel/history/choice) 0.58s 0.56s
machine.handle (4 successive calls) 0.54s 0.54s
machine exact input/output/error/services 0.48s 0.47s
execution adapter readiness 0.57s 0.52s

Type instantiations are the comparison metric. Check time varies with runner load and is informational only.

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Runtime performance

Median of 5 independent benchmark processes on INTEL(R) XEON(R) PLATINUM 8573C with Node v24.18.0.

Pull request baseline

Scenario Effect Machine XState 5 XState 6 alpha
Plan counter transitions 161,833 transitions/s 194,540 transitions/s 158,528 transitions/s
Drain burst with terminal fence 121,720 increments/s 582,229 increments/s 149,906 increments/s
Drain burst with a change observer 100,090 increments/s 581,575 increments/s 149,573 increments/s
Lookup and send to one child 108,143 increments/s 577,951 increments/s 143,283 increments/s
Start and stop a machine 31,093 machines/s 263,505 machines/s 107,089 machines/s
Start and stop a parent with one child 9,531 families/s 100,857 families/s 43,375 families/s
Memory profile Effect Machine XState 5 XState 6 alpha Effect runtime primitives
Idle machine 16.6 KiB 3.8 KiB 6.1 KiB
Raw managed process 13.1 KiB
Two independent idle machines 32.3 KiB 7.3 KiB 11.8 KiB
Idle parent with one child 38.5 KiB 5.5 KiB 8.8 KiB
Parent with observed child registry 49.9 KiB
Parent with observed invoked child snapshots 38.7 KiB
Suspended Effect fiber 0.7 KiB
Effect queue with waiting fiber 2.8 KiB
Effect mailbox actor shell 3.2 KiB

Effect Machine change from base

Metric Base Base variability PR PR variability Difference
Plan counter transitions 165,896 transitions/s 0.6% MAD 161,833 transitions/s 2.7% MAD -2.4%
Drain burst with terminal fence 100,165 increments/s 1.1% MAD 121,720 increments/s 0.8% MAD +21.5%
Drain burst with a change observer 84,781 increments/s 1.5% MAD 100,090 increments/s 0.6% MAD +18.1%
Lookup and send to one child 90,740 increments/s 1.9% MAD 108,143 increments/s 0.9% MAD +19.2%
Start and stop a machine 29,024 machines/s 0.2% MAD 31,093 machines/s 1.4% MAD +7.1%
Start and stop a parent with one child 9,160 families/s 0.2% MAD 9,531 families/s 1.0% MAD +4.1%
Idle machine heap per unit 17.8 KiB 0.0% MAD 16.6 KiB 0.0% MAD -7.2%
Raw managed process heap per unit 13.1 KiB 0.0% MAD 13.1 KiB 0.0% MAD -0.0%
Two independent idle machines heap per unit 34.9 KiB 0.0% MAD 32.3 KiB 0.0% MAD -7.3%
Idle parent with one child heap per unit 41.0 KiB 0.0% MAD 38.5 KiB 0.0% MAD -6.0%
Parent with observed child registry heap per unit 52.4 KiB 0.0% MAD 49.9 KiB 0.0% MAD -4.7%
Parent with observed invoked child snapshots heap per unit 41.2 KiB 0.0% MAD 38.7 KiB 0.0% MAD -6.0%
Versions and interpretation
  • Effect Machine: 0.3.0
  • XState 5: 5.32.5
  • XState 6 alpha: 6.0.0-alpha.27
  • Effect runtime primitives: 4.0.0-beta.105

Higher throughput is better; lower heap is better. Variability is the median absolute deviation across independent processes, relative to their median. Runtime measurements on shared GitHub-hosted hardware remain informational, so small differences should be confirmed across multiple workflow runs.

@SandroMaglione
SandroMaglione merged commit d602202 into main Aug 8, 2026
9 checks passed
@SandroMaglione
SandroMaglione deleted the codex/compact-idle-statechart branch August 8, 2026 15:12
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