ZeroAlloc.StateMachine
1.6.5
dotnet add package ZeroAlloc.StateMachine --version 1.6.5
NuGet\Install-Package ZeroAlloc.StateMachine -Version 1.6.5
<PackageReference Include="ZeroAlloc.StateMachine" Version="1.6.5" />
<PackageVersion Include="ZeroAlloc.StateMachine" Version="1.6.5" />
<PackageReference Include="ZeroAlloc.StateMachine" />
paket add ZeroAlloc.StateMachine --version 1.6.5
#r "nuget: ZeroAlloc.StateMachine, 1.6.5"
#:package ZeroAlloc.StateMachine@1.6.5
#addin nuget:?package=ZeroAlloc.StateMachine&version=1.6.5
#tool nuget:?package=ZeroAlloc.StateMachine&version=1.6.5
ZeroAlloc.StateMachine
Source-generated finite state machines for .NET, a compile-time alternative to Stateless: transitions
are a generated switch, 0 B per transition and NativeAOT-ready.
Why ZeroAlloc.StateMachine
- 0 B per transition, asserted in CI.
TryFireallocates nothing on valid, invalid, guarded and concurrent transitions;AllocationTestsfails CI if it does. Stateless allocates 24 B just to reject an invalid trigger. - About 15× faster than Stateless. Rejecting an invalid trigger takes 1.6 ns against 23 ns in the same run, and a three-step cycle with reset is about 300× faster.
- Works under NativeAOT and trimming. No reflection; every PR publishes an AOT sample with
PublishAot=true. - Compile-time diagnostics instead of runtime surprises. An unreachable state, a state with no way
out or a likely trigger typo is reported at build time, from
ZSM0001toZSM0026.
Install
dotnet add package ZeroAlloc.StateMachine
Quick start
using ZeroAlloc.StateMachine;
var order = new OrderMachine();
Console.WriteLine(order.TryFire(Trigger.Submit)); // True: Idle -> Pending
Console.WriteLine(order.Current); // Pending
Console.WriteLine(order.TryFire(Trigger.Pay)); // True: Pending -> Done
Console.WriteLine(order.TryFire(Trigger.Submit)); // False: Done is terminal
public enum State { Idle, Pending, Done }
public enum Trigger { Submit, Pay }
[StateMachine(InitialState = nameof(State.Idle))]
[Transition<State, Trigger>(From = State.Idle, On = Trigger.Submit, To = State.Pending)]
[Transition<State, Trigger>(From = State.Pending, On = Trigger.Pay, To = State.Done)]
[Terminal<State>(State = State.Done)]
public partial class OrderMachine
{
partial void OnEnterDone(State from) => Console.WriteLine($"Order complete, came from {from}");
}
The generator writes TryFire as a switch over (State, Trigger) and calls the OnEnter… and
OnExit… hooks you implement.
What you get
- Guards that block a transition at runtime through a generated
partial boolmethod. - Entry and exit hooks per state.
- Concurrent mode: lock-free, thread-safe transitions with one attribute property.
- Struct machines that keep even the machine itself off the heap.
- Composite and history states, timeout transitions, and concurrent parts that host several machines in one class.
- Mermaid diagram export as a generated constant.
Every attribute and property is in the attribute reference.
Performance
BenchmarkDotNet, same run against Stateless 5.20.1. Allocations are exact.
| Operation | Speed-up vs Stateless | Stateless alloc | ZA alloc |
|---|---|---|---|
| Fire valid, 3-step cycle with reset | ~300× | 7,600 B | 0 B |
| Fire invalid | ~15× | 24 B | 0 B |
| Guard blocked | > 400× | 768 B | 0 B |
The cycle row includes each library's reset: a field write for ZeroAlloc, a fluent reconfiguration for Stateless. The guard row is a lower bound, because ZeroAlloc's time is below what BenchmarkDotNet can measure. Full numbers and methodology: docs/performance.md.
Learn more
- Documentation: statemachine.zeroalloc.net, including getting started, guards, concurrent mode, the generated code and diagnostics.
- Related packages: ZeroAlloc.Saga and ZeroAlloc.Scheduling, both built on it.
- Contributing: issues and pull requests are welcome on GitHub.
- CHANGELOG.
License
MIT. See LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (7)
Showing the top 5 NuGet packages that depend on ZeroAlloc.StateMachine:
| Package | Downloads |
|---|---|
|
ZeroAlloc.Resilience
Source-generated, zero-allocation resilience policies for .NET. Add [Retry], [Timeout], [RateLimit], and [CircuitBreaker] to an interface; the generator emits a proxy composing all policies in declaration order. AOT-safe. |
|
|
ZeroAlloc.Scheduling
Source-generated zero-allocation background job scheduling for .NET. |
|
|
ZeroAlloc.Outbox
Source-generated transactional outbox for .NET. |
|
|
Thalos.NET
Thalos.NET agent runtime on Microsoft Agent Framework: agent factory, tool catalog with authorization, session state machine, in-memory session store. |
|
|
ZeroAlloc.EventSourcing.Aggregates
Aggregate base + generator-emitted Apply dispatch for ZeroAlloc.EventSourcing. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.6.5 | 27 | 10/16/2026 |
| 1.6.4 | 987 | 10/15/2026 |
| 1.6.3 | 6,248 | 10/11/2026 |
| 1.6.2 | 12,035 | 9/30/2026 |
| 1.6.1 | 6,386 | 9/28/2026 |
| 1.6.0 | 2,023 | 9/27/2026 |
| 1.5.4 | 7,455 | 9/20/2026 |
| 1.5.3 | 40,790 | 9/19/2026 |
| 1.5.2 | 1,464 | 8/8/2026 |
| 1.5.1 | 596 | 5/25/2026 |
| 1.5.0 | 143 | 5/23/2026 |
| 1.4.0 | 137 | 5/23/2026 |
| 1.2.0 | 202 | 5/13/2026 |
| 1.1.2 | 168 | 5/12/2026 |
| 1.1.1 | 261 | 5/3/2026 |
| 1.1.0 | 227 | 5/1/2026 |
| 1.0.3 | 176 | 4/29/2026 |
| 1.0.1 | 4,867 | 4/19/2026 |
| 1.0.0 | 31,463 | 4/18/2026 |