Compiling to native code
nibc build --target native turns a wire into an executable through LLVM — the value model, the fibre host that serves sockets, and how it stays byte-for-byte equal to the walker.
nibc build --target native [--wire W] [-o OUT] [--emit-llvm] [--check] FILE
lowers the wire's entry, writes it as textual LLVM IR, and links it with clang and nibrt into one executable. It prints FILE: ENTRY: compiled natively (LTO) and how to run it: OUT ARG, or OUT --arg name=value as nibc --run takes it. The answer is printed by the entry's declared type. Native is the deployment target nib is built around: Linux in production, macOS for development, 64-bit everywhere.
The value model
Every value is one 64-bit word. An Int is the integer, kept to 63 bits so it wraps exactly as the walker's OCaml ints do; a Bool is 0 or 1; () is 0. Everything else is a block: a 16-byte header — kind, length, an auxiliary word (a sum's constructor, a vector's capacity), an owner count — then 8-byte slots or bytes. A word that points at a counted block has bit 63 set; since user addresses sit below 2^47, no Int can be mistaken for one. A literal or an empty block is a constant in the executable: untagged, never counted, never freed.
Blocks are reference counted. The compiler inserts each dup and drop by liveness, so a block is freed the moment its last owner lets go, into free lists of 768 size classes; a vector or map with one owner is updated in place. A record's fields sit in name order; a list is cons blocks ending in 0; a set is a sorted vector; a small map is a sorted flat block, and past 64 entries a hash trie (CHAMP) whose keys are still walked in order. A Float local that only Float arithmetic reads and writes is kept unboxed inside its function, as raw IEEE bits, and so is a Vec<Float>'s storage: its slots are the doubles themselves. A Float is boxed only where generic code or a call sees it. Raises travel in two thread-local words, a tag and a payload, and a raise is a branch to the handler.
Parallel maps
A Vec.map whose callback performs no effect and raises nothing, over and into Ints, Floats or Bools, runs on every core. The checker's types say which maps qualify; nothing in the source asks for it. The callback can call functions, recurse, allocate and capture any value, because each worker thread runs it on a heap of its own with its own copy of the closure, so no counted block is ever shared between threads. The vector is split into chunks that idle threads take from a shared counter, and every result lands in its own slot of the answer.
- Threads. One per core the process may run on (on Linux, its CPU affinity);
NIB_CORES=Nsets the count andNIB_CORES=1keeps every map serial. - What stays serial. A callback that may raise or perform an effect, one whose effects the checker leaves open, a map inside another map's callback, and every map in a fibre program.
Fibres and the host
An entry that waits — a socket read, a sleep, Server.run — is a fibre program. Its body becomes a state machine: each wait parks the fibre, saving its locals to a frame, and the runtime's host resumes it when the answer is in. fork(sw) { … } starts another fibre; Switch.run waits for its children. The host is nibrt itself, not a library:
- I/O. On Linux it submits each
recvandsendto io_uring as the park itself, receive buffers shared from a ring; on macOS it waits with kqueue;poll()where neither exists. - Memory. Each fibre gets a region (64 MiB reserved, touched lazily;
NIB_REGION_MIBsets it) with its own free lists, freed whole when the fibre ends. A value crossing to a child fibre is deep-copied into the child's region, so no block is ever shared between regions. - Cores.
NIB_CORES=Nruns fibres on N threads, each with its own scheduler and runtime state; four cores serve about 2.2–3× one. - Isolation. A fibre that traps, overflows its stack or exhausts its region is retired alone, by name on stderr, and the server keeps serving. A raise that no handler catches ends the program, as it does under
nibc --run. - Sandbox. On Linux the server installs a seccomp allowlist derived from what the program does — socket calls only if it uses the network,
openatonly if it reads files, nothing executable — and restricts its io_uring rings to the operations it uses.NIB_SANDBOX=0turns it off.
Effectors declared in the program are compiled too: the build resolves the wire's instance graph once (an alias is one instance; a pipe x |> Wrapper fills the wrapper's constructor parameter) and turns each method into a function. An effector's var cells are the program's, whatever the core count: each slot (a key of a keyed cell, or an unkeyed cell's one value) lives in one of 16 stripes chosen by its key's hash, each stripe a region of its own outside every fibre, and values cross in and out by deep copy. A method claims the stripes of the keys it touches before its body runs (a key is a function of the call, so it is known at entry) and gives them back when the body returns or raises. Calls on different keys run on different cores together; calls on one key take turns. A per-fibre instance (I = Name per fibre) keeps its cells in its own fibre, unclaimed.
Speed
Measured 2026-11-22 on one machine: an AMD Ryzen 7 5800H (8 cores, 16 threads) running Linux, in a container shared with other services. Servers ran on cores 0–7 and the load generator, wrk with HTTP/1.1 keep-alive, on cores 8–15.
A representative web app
A small notes site: a router as data, an HTML page and a JSON API, with its responder written as an effector. It re-reads and re-parses its JSON data file on every request, so each answer does real work. wrk -t4 -c64, 15 s per point:
Native code is 30–55× the walker on one core, and four cores serve about 3.3× one. On eight, the JSON route reaches 190 k req/s.
LTO matters: when the clang is built on the LLVM rustc used for nibrt (21, as NIB_CLANG in the devenv), nibrt is linked into the program as bitcode before it is optimised, and the runtime's slow paths inline into the program; an older clang links nibrt as an object, and the build says which it did.
Against Go and Rust
The Computer Language Benchmarks Game's eight programs, written the same way in all three languages (single-threaded, no SIMD, no unsafe, no arenas, no third-party libraries), every output checked byte-for-byte against Rust's; median of five runs. The programs, the servers, the harness and the full report are shared here.
Over all eight, nib takes 1.65× Rust's time and 1.34× Go's (Go takes 1.24× Rust's). Allocation-heavy code is nib's strength — reference-counted regions free a tree the moment it is dropped — and floating-point loops are at or near parity. What costs most is per-element work: every vector and string access is bounds-checked, and String and Bytes accesses go through the runtime (reverse-complement, fannkuch). A Map is a persistent hash trie, so with millions of keys a lookup follows four or five nodes where Rust's hash table touches about two cache lines (k-nucleotide). n-body keeps its bodies in a heap vector in both languages, but Rust's compiler sees there are five and unrolls every loop, where nib's pair loop stays a loop with a bounds check on each access.
The same mandelbrot with its rows spread over eight cores: Rust starts a scoped thread per core and Go a goroutine per GOMAXPROCS, each taking the next row from a shared atomic counter. nib's program is the serial one with its row loop written as a Vec.map, and has no threads in it. Every output is checked against the serial program's; median of five runs:
All three run about 6× faster on eight cores, within run-to-run spread of each other.
Serving HTTP, each language's idiomatic server — nib's Server.run, Go's net/http, Rust's axum on tokio — on eight cores, 15 s per point:
nib serves as many requests as axum and about a quarter more than net/http: per-core io_uring fibres, no shared scheduler.
Shared state under load
An accounts server: 1024 accounts, each keeping its most recent deposits (starting with a full window of zeros, so every request does the same work), and GET /a/<id> deposits into one and answers the sum of what it keeps. nib keeps the ledgers in a keyed effector cell and the program has no lock; Rust and Go hold them behind one lock, or 16 chosen by hash. Same frameworks and cores as above, 64 connections:
While each account's state is small, no server is held back by its lock — Rust and Go serve the same with one lock as with sixteen — and nib keeps pace with Rust, ahead of Go. With 32 KB per account a request holds its account for a sum of 4096 numbers, and Go's single lock becomes the bottleneck until the map is split by hand (212 k with 16 locks). nib's program names no lock and serves within 5 % of Rust: a method of one key runs in that key's storage, and one that reads its slot once and then writes it moves the value out and back, so the push and the drop work on the vector in place. The full table has both connection counts and the 16-lock variants.
The same programs in Rust and Go
The parallel and shared-state benchmarks above are the same work in three languages, and the code is where they differ most. Below is the part of each program that spreads work over cores or shares state between them; the rest (the per-row arithmetic, the HTTP routing) is ordinary serial code in all three. The full programs are in the shared source.
A loop over every core
nib's mandelbrot computes each row with a plain recursive function and maps it over the rows. That is the whole of its parallel code; the compiler sees the callback is pure and runs the map on its pool:
let rows = Vec.map(Iter.to_vec(Iter.range(0, size())), { y -> row(y, 0, 0, 0) });
let total = Vec.fold(rows, 0, { s, r -> s + r });
Rust with the standard library starts a thread per core and shares a row counter between them, then joins the threads and adds up what each one counted:
let threads = thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
let next = AtomicUsize::new(0);
let (byte_sum, count) = thread::scope(|s| {
let handles: Vec<_> = (0..threads)
.map(|_| {
s.spawn(|| {
let mut acc = (0u64, 0u64);
loop {
let y = next.fetch_add(1, Ordering::Relaxed);
if y >= size {
break acc;
}
let (b, c) = row(y, size, iters);
acc.0 += b;
acc.1 += c;
}
})
})
.collect();
handles.into_iter().map(|h| h.join().unwrap()).fold((0, 0), |a, b| (a.0 + b.0, a.1 + b.1))
});
Go is the same shape with goroutines, a WaitGroup, an atomic counter and one result slot per goroutine:
threads := runtime.GOMAXPROCS(0)
var next int64 = -1
sums := make([]int64, threads)
counts := make([]int64, threads)
var wg sync.WaitGroup
for t := 0; t < threads; t++ {
wg.Add(1)
go func(t int) {
defer wg.Done()
for {
y := int(atomic.AddInt64(&next, 1))
if y >= size {
return
}
s, c := row(y, size, iters)
sums[t] += s
counts[t] += c
}
}(t)
}
wg.Wait()
All three run in about 0.15 s on eight cores. In fairness to Rust: its usual answer is the rayon crate, where (0..size).into_par_iter().map(|y| row(y, size, iters)) is one line too; the benchmarks here use no third-party crates. Rust's compiler also checks that the threads share nothing unsafely, as nib's does; Go's does not. What nib adds is that the decision is the compiler's: a callback that performs an effect, or may raise, stays serial, so a map cannot be made parallel by mistake.
Per-key state without locks
The accounts server keeps a ledger per account. In nib it is a keyed cell; there is no lock in the program:
effector Ledger is Accounts where
config window :: Int = 32;
var recent :: String => Vec<Int> = {};
deposit(id, amount) =
let kept = Vec.push(recent[id], amount);
let latest = if Vec.size(kept) > window then Vec.drop(kept, 1) else kept;
recent[id] := latest;
Vec.fold(latest, 0, { s, x -> s + x })
A call to deposit claims account id for as long as it runs: two calls on one account take turns, and calls on different accounts run on different cores together (unless their keys hash to the same one of the 16 stripes, where they take turns too). Rust and Go hold the same state behind locks the program declares and takes. With one lock for the whole map, every request waits for every other; with a lock per shard, the program also picks the shard:
type Shard = Mutex<HashMap<String, VecDeque<i64>>>;
fn shard_of(shards: &[Shard], id: &str) -> usize {
let mut h = DefaultHasher::new();
id.hash(&mut h);
(h.finish() as usize) % shards.len()
}
let mut m = shards[shard_of(&shards, &id)].lock().unwrap();
match m.get_mut(&id) {
Some(recent) => {
recent.push_back(amount);
if recent.len() > window() {
recent.pop_front();
}
(StatusCode::OK, recent.iter().sum::<i64>().to_string())
}
None => (StatusCode::BAD_REQUEST, "bad account".to_string()),
}
s := shardOf(id)
s.mu.Lock()
recent, ok := s.recent[id]
if !ok {
s.mu.Unlock()
http.Error(w, "bad account", http.StatusBadRequest)
return
}
recent = append(recent, int64(1+len(id)))
if len(recent) > window {
recent = append(recent[:0], recent[1:]...)
}
s.recent[id] = recent
var sum int64
for _, x := range recent {
sum += x
}
s.mu.Unlock()
The difference grows with the program. A method that touches two accounts claims both, in an order the runtime fixes, so it cannot deadlock; in Rust or Go that order is a rule every author of every such function has to follow. A Go path that returns without Unlock holds the lock forever; nib has no lock to give back. The compiler earns back what immutability would cost: a method that reads its slot once and then writes it updates the value in place, as a Rust or Go server does, because nothing else can see it in between.
When something does not compile
A construct the backend cannot lower is refused with a code (EMethodCall, EParkCarries, …) and a sentence saying why, and no executable is written — nibc --run still runs the program. To see the whole boundary at once rather than the first refusal, lower every function separately:
NIB_NATIVE_SURVEY=1 nibc build --target native --wire W FILE
prints one survey: FUNCTION: compiles or survey: FUNCTION: CODE: reason line per function.
Where it still differs
Kept in the open, because the rule is that nothing differs silently:
- A
Floatinside aJsondocument is not printed natively yet (Json.stringify/renderraiseErr.Unrenderablesaying so). - A plain program's answer is printed with a trailing newline;
nibc --runprints none. - Effector hooks (
on_start/on_stop) are not compiled.