byteflow-actors
Run thousands of small, isolated, crash-proof flows inside your Rust app.
Byteflow is an embeddable runtime for flows: lightweight processes in the Erlang mould. Every flow has its own registers and mailbox, is preempted after a fixed number of instructions, and can crash without taking anything else down. Supervisors restart what dies. A sandbox caps CPU, memory, spawns and messages per flow, and capabilities decide which flow may talk to which.
It is one crate with one dependency (getrandom) and #![deny(unsafe_code)].
You describe flows in Rust with a small builder, and they compile to verified
bytecode:
use ;
const PING: i32 = 10;
const PONG: i32 = 11;
[]
= "0.9" # imported as `byteflow`
Why you might want it
- Plugins and mods your users write. Load them at runtime as
.bfmodules, which go through a static verifier before anything runs. A plugin stuck in an infinite loop ends asFailed("flow CPU budget exhausted"). Your server keeps running. - Rules, automations and scripts per customer. Each tenant gets flows with their own quotas and only the host functions you grant them.
- Simulations and game entities. Give each entity its own flow and let entities talk through messages instead of shared state.
- Concurrent protocol code where "let it crash and restart it" is simpler than defending every error path.
Three guarantees, in code
A runaway guest is stopped, not waited on. The guest below never returns.
Under RuntimeConfig::sandbox() it gets a CPU budget, no native functions
and minimum authority:
use Duration;
use ;
What crashes gets restarted. A supervisor restarts a failing child until it fails too often (OTP's "intensity"). Then it gives up and tells you:
use ;
use ;
Nothing reaches the VM unchecked. .bf modules are decoded, then
verified (registers, jump targets, constants, function table) before a
runtime will start them. Bad bytes come back as an error value, not a panic:
use ;
From the command line
$ cargo install byteflow-actors
$ byteflow demo ping-pong
2
spawned=2 completed=2 failed=0 messages=2 steals=0 reschedules=0
$ byteflow pack ping-pong ping_pong.bf
wrote ping_pong.bf (237 bytes, chunk "ping-pong")
$ byteflow verify ping_pong.bf
ok ping-pong 2 functions 22 instructions
$ byteflow disasm ping_pong.bf
.chunk "ping-pong"
.abi 5
.fn "pong" entry=0 arity=0 regs=7
.fn "main" entry=11 arity=0 regs=8
0000 ReceiveMatchImm r0, tag=10 ; pong
0001 CallNativeFrom r1, r0, native[6]
0002 AddImm r1, r1, 1
...
byteflow asm turns a listing back into a module, and byteflow run runs one.
How it compares
A rough guide. Each of these projects is excellent at what it was built for.
| Byteflow | Tokio tasks | Actix actors | Wasmtime | Erlang / BEAM | |
|---|---|---|---|---|---|
| Embeds as a Rust library | ✓ | ✓ | ✓ | ✓ | – |
| A busy loop cannot starve its neighbours | ✓ preempted by instruction count | – cooperative | – cooperative | ✓ fuel / epochs | ✓ |
| Supervisors and restart strategies | ✓ OTP-style | – | ✓ | – | ✓ |
| Load untrusted code at runtime | ✓ verified bytecode | – | – | ✓ | not sandboxed |
| Per-process CPU / memory / message quotas | ✓ | – | – | partly | partly |
| Runs native-speed Rust inside the process | via host functions | ✓ | ✓ | – | via NIFs |
Byteflow does not do networking or async I/O. Your host owns sockets and files, and flows talk to the host through HostAwait and native functions. It is not a cluster runtime either. Remote hops are frames that the host carries over its own transport (remote-transport).
Numbers
Intel i5-9400F (6 cores), Windows 10, release build, default configuration.
| Workload | Result | Reproduce |
|---|---|---|
| spawn + join 50 000 trivial flows | ~225 000 flows/s | cargo run --release --example throughput |
| one bytecode add, interpreter vs JIT | cycles per run | cargo run --release --example cycles [--features jit] |
| tight counting loop, interpreter vs JIT | wall time | cargo run --release --example jit_loop --features jit |
Criterion benches live in benches/.
What's inside
| Layer | Role |
|---|---|
| Bytecode | Register ISA, Program/Fn builder, .bf format, text assembler, static verifier |
| VM | One flow: registers, call stack, preemption slice, gated native calls |
| Scheduler | M:N work-stealing workers, bounded mailboxes, timer wheel, links, monitors, registry |
| Supervision | OTP strategies OneForOne / OneForAll / RestForOne, restart intensity |
| Security | Capabilities, quotas, trust levels, attestation, audit counters |
| JIT (optional) | Cranelift trace compiler for hot loops |
| CLI | byteflow run / verify / disasm / asm / pack / demo |
Runnable examples are in examples/. Guides
are rendered on docs.rs and tested where they contain code:
atomic-hop ·
beam-mapping ·
lifecycle ·
mailbox ·
vm-safety ·
error-model ·
host-await ·
untrusted ·
security ·
sandbox ·
remote-transport ·
production-embedding
API contracts
The package on crates.io is byteflow-actors. The library is imported as
byteflow (use byteflow::...).
#![deny(unsafe_code)]. Thejitfeature allowsunsafeonly inside the Cranelift backend.- Default dependency:
getrandom, forCapId. Cranelift is optional, behindjit. - A panic inside a flow stays on the worker boundary. Malformed bytecode
produces a
Faulton that flow. - Natives must not block, because they run on a worker thread.
- Host APIs return
Result. Seedocs/error-model.md. - Values:
Unit | Bool | Int | Float | Pid | Message | Cap | Str | Bytes | List | Map | Record. Sendaccepts onlyValue::Message(an Atomic Hop). The scheduler stamps the sender and attaches a reply capability.- FlowCap: a 128-bit CSPRNG
CapId. A grant happens only throughCap::attenuate,Fn::delegateor a confined spawn. CALL_NATIVEchecksCapRights::NATIVEand theNativeMaskbefore the table index.- Quotas:
QuotaConfig::permissive(the default) orsandbox. Process heap is capped withmax_runtime_bytes/MemoryBudget. - Host
Runtime::spawndefaults toRootAuthorityPolicy::TrustedHost(ROOT+ full natives).RuntimeConfig::sandbox()does not change that. Usespawn_untrustedorroot_authority. - Mailboxes are bounded (default 256 hops / 4 MiB / Reject). A bytecode
sender to a full mailbox parks until a slot frees, in FIFO order; the
waiters are bounded by
max_flows(docs/mailbox.md). - Registry
whereisreturns a SEND Cap, never a FlowId. .bfABI:encodepicks the oldest header that fits the constant pool (v5 scalars, v6 lists, v7 maps, v8 records).decodeaccepts all four.- Remote:
encode_frame/decode_frame/deliver_frame, an optional transactionalremote_dedup_window, and a bridge viaDistributionConfig.
Collecting a result without blocking forever:
| Call | Behavior |
|---|---|
try_join() |
Non-blocking; None if still running |
join_timeout(d) / join_deadline(t) |
Bounded wait; None on timeout |
join() |
Blocks until the outcome |
Std native indices: print = 0, now_ms = 1, make_msg = 2, msg_* = 3–6,
msg_reply_cap = 7. Use Runtime::with_natives(..., std_native_table()).
Security detail: docs/security.md,
docs/untrusted.md, docs/sandbox/.
Status
Pre-1.0. The bytecode format is versioned and the decoder accepts every published ABI, but the Rust API still changes between minor versions. The CHANGELOG marks breaking changes. Default features build on Rust 1.88 or newer, and CI tests Linux, Windows and macOS.
Contributing
Issues and pull requests are welcome. See CONTRIBUTING.md, which includes the 60-second setup and the checks CI runs.
License
MIT OR Apache-2.0, at your option.