Performance tips
Most Tern code is fast without any tuning: it compiles to C, clang optimizes it with -O3, and there is no garbage collector. When a program is slower than it should be, the cause is usually one of a few things: a hidden copy of a big array, a slow sort key, a string built from the wrong end, or parallel tasks that are too small. This page shows how to find each one with TERN_STATS and emit, how to fix it, and how Tern compares with Go and Rust on the repository's benchmarks, including where Rust is faster.
Timings on this page were measured on an Apple M4 Pro (14 cores) with programs built by tern build. The copy counts come from TERN_STATS=1, explained in How memory works.
Measure first
TERN_STATS=1 tern run prog.tn # prints allocations, frees, leaks, and copies
tern build prog.tn -o prog # a native binary to time
time ./prog
TERN_THREADS=1 ./prog # run parallel code on one core, for comparison
tern emit prog.tn > prog.c # read the C that the compiler generatesTERN_STATS must be set when the program is compiled; its counters aren't in normal builds. The number to watch is copy-on-write copies: each one is a whole array, string, or map copied because it was written while shared. A count that grows with your input, like one copy per loop pass, is almost always a bug worth fixing.
Time a built binary, not tern run, which includes compile time.
Avoid hidden copies
Assignment never copies right away, but writing to a value that is shared does. The classic case is ys = xs followed by ys[0] = 1: that copies all of xs. It most often hides in a function that takes a read-only parameter, changes a local copy, and returns it:
fn record(counts: [int], bucket: int) [int]:
out = counts # copies all of `counts` at the next line
out[bucket] += 1
return out
fn main():
counts = [0] * 1000
for i in 0..100:
counts = record(counts, i % 7)
print(counts[0], counts[6])15 14-- memory: 101 allocations, 101 freed, 0 leaked, 100 copy-on-write copies; no GCOne copy of the whole array per call. With a mut parameter, the function changes the caller's array in place:
fn record(counts: mut [int], bucket: int):
counts[bucket] += 1 # writes the caller's array in place
fn main():
counts = [0] * 1000
for i in 0..100:
record(&counts, i % 7)
print(counts[0], counts[6])15 14-- memory: 1 allocations, 1 freed, 0 leaked, 0 copy-on-write copies; no GCScaled up to a 100,000-element array and 10,000 calls, the first version takes 0.46s (10,000 copies of 800KB) and the second under 10ms.
The same thing happens with array elements. p = team[i] copies the struct, and its array fields are then shared with the element, so writing to p.scores copies them:
struct Player:
name: str
scores: [int]
fn main():
team = [Player("ada", [1, 2]), Player("bob", [3])]
for i in 0..len(team):
team[i].scores.push(0) # not: p = team[i]; p.scores.push(0); team[i] = p
print(team)[Player{name: "ada", scores: [1, 2, 0]}, Player{name: "bob", scores: [3, 0]}]Written directly, this makes no copies; the commented-out version makes one per player. To change an element through a function, pass &team[i] to a mut parameter.
Hand values off with take
When a function consumes a value and returns a changed version of it (a builder, a pipeline stage), declare the parameter take. The caller's variable moves into the function, so there's no copy:
fn with_total(rows: take [str], total: int) [str]:
rows.push(f"total: {total}") # rows is ours now: changed in place, never copied
return rows
fn main():
rows = ["pears: 3", "figs: 12"]
report = with_total(rows, 15)
print("\n".join(report))pears: 3
figs: 12
total: 15-- memory: 5 allocations, 5 freed, 0 leaked, 0 copy-on-write copies; no GCOn the 100,000-element example above, fn record(counts: take [int], bucket: int) [int] called as counts = record(counts, i % 7) also makes zero copies. xs.push(v) moves v when it isn't used afterwards, so pushing a freshly built string or array into a list never copies it. Use mut when the function changes a value in place, and take when it keeps or returns it.
Use flat arrays of numbers
An [int], [float], or [bool] is one block of plain values: no reference counts per element. An array of arrays is one block per row, and each row is a separate value. Watch out for [[0] * n] * n: it makes n references to one row, so the first write to each row copies it:
fn main():
n = 100
grid = [[0] * n] * n # n references to one shared row
for r in 0..n:
for c in 0..n:
grid[r][c] = r * c # the first write to each row copies it
print(grid[n - 1][n - 1])9801-- memory: 101 allocations, 101 freed, 0 leaked, 99 copy-on-write copies; no GCThe results are correct, since each row really is its own value. But for a grid you fill yourself, one flat array indexed by r * n + c makes no copies and one allocation:
fn main():
n = 100
grid = [0] * (n * n) # one block; cell (r, c) is at r * n + c
for r in 0..n:
for c in 0..n:
grid[r * n + c] = r * c
print(grid[n * n - 1])9801-- memory: 1 allocations, 1 freed, 0 leaked, 0 copy-on-write copies; no GCAt 3000 × 3000, filling the flat grid took 5ms and the nested one 10ms.
More about arrays:
[bool]is bit-packed: 8 flags per byte. The 200-million-entry sieve benchmark uses 25MB in Tern and about 195MB in Go and Rust, which store one byte perbool.- Create hot arrays with
[v] * nwhen you know the size. A local array made that way, never pushed to and never shared, gets its length kept in a register, which lets LLVM drop bounds checks and unroll loops. - Slices copy.
xs[a..b]makes a new array.queue = queue[1..]in a loop is quadratic; keep an index into the array instead.
Build strings from the front
s += piece (or s = s + piece) appends in place when s isn't shared: the string grows into spare capacity, so a loop of appends is linear. Adding to the front copies the whole string every time:
fn main():
csv = ""
for i in 0..5:
csv += f"{i}," # appends in place
parts: [str] = []
for i in 0..5:
parts.push(f"{i * i}")
print(csv, ",".join(parts))0,1,2,3,4, 0,1,4,9,16With 100,000 pieces, s += f"{i}," took 2ms, collecting the pieces in an array and calling "".join(parts) also took 2ms, and s = f"{i}," + s took 1.8s. To build text in reverse order, push the pieces and join them in a reversed loop.
Sort by numbers when you can
xs.sort_by(key) computes each key once and sorts stably. Number keys (int or float) use a radix sort. String keys are radix-sorted on their first 8 bytes, and only keys that share those are compared as whole strings. On 1,000,000 structs, sorting by an int field took about 22ms, and sorting by a str field holding the same numbers took about 37ms.
struct Order:
id: int
total: float
customer: str
fn main():
orders = [Order(3, 9.5, "cy"), Order(1, 20.0, "ada"), Order(2, 9.5, "bob")]
by_id = orders.sort_by(fn(o) o.id) # radix sort
biggest = orders.sort_by(fn(o) -o.total) # descending: negate a number key
by_name = orders.sort_by(fn(o) o.customer) # radix on the first 8 bytes, then compares
print(by_id[0].id, biggest[0].id, biggest[1].id, by_name[2].customer)1 1 3 cyIf records have both a numeric id and a string form of it, sort by the number. Pass the lambda directly to sort_by, map, and filter: a closure the compiler can see is called directly, so it inlines like a hand-written loop.
Give parallel tasks enough work
parallel for splits its range into contiguous chunks, up to 8 per core (at most 256), so even tiny loop bodies parallelize well: the 2-million-task Collatz benchmark runs as fast as Rust's rayon. Each iteration should write only its own out[i].
parallel: blocks are different: each line is a task, and starting and joining a block costs much more than a function call. In recursive code, stop splitting once the work gets small and fall back to the sequential version:
fn fib(n: int) int:
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
fn pfib(n: int) int:
if n < 20:
return fib(n) # small: not worth a task
parallel:
a = pfib(n - 1)
b = pfib(n - 2)
return a + b
fn main():
print(pfib(30))832040For fib(40), the sequential version took 0.17s and this version 0.02s on 14 cores. Without the cutoff (splitting all the way down to n < 2), it took 17s, 100 times slower than not using parallel at all. Compare with TERN_THREADS=1 to see what parallelism is really buying you. The parallel programming guide covers the rules.
Read the generated C
tern emit prog.tn prints the C that clang compiles. Search it for _unique(: that's a copy-on-write check, and where it sits tells you what the loop costs. For this function:
fn scale(xs: mut [float], k: float):
for i in 0..len(xs):
xs[i] = xs[i] * k
fn main():
xs = [1.0] * 1000
scale(&xs, 2.0)
print(xs[999])2.0The compiler emits two versions:
static void tn_scale(Af_t** xs_p, double k_p) {
Af_unique(&(*xs_p)); // one check, hoisted out of the loop
{
int64_t i_1_lo = INT64_C(0), _hi2 = (((*xs_p))->len);
for (int64_t i_1 = i_1_lo; i_1 < _hi2; i_1++) {
(*Af_at((*xs_p), i_1, 3)) = (((*Af_at((*xs_p), i_1, 3))) * (k_p));
}
}
}
static void tn_scale_u(Af_t** xs_p, double k_p, const int64_t xs_len) {
{
int64_t i_5_lo = INT64_C(0), _hi6 = xs_len;
for (int64_t i_5 = i_5_lo; i_5 < _hi6; i_5++) {
(*Af_atn((*xs_p), i_5, xs_len, 3)) = (((*Af_atn((*xs_p), i_5, xs_len, 3))) * (k_p));
}
}
}The first checks once that xs isn't shared, then writes freely. The second (_u) is called when the caller's array is already known to be unshared with a fixed length: no check at all, and the length arrives as a parameter, so after inlining LLVM can see it. A _unique( call inside a loop means the compiler couldn't prove the array stays unshared there, usually because it is also copied, reassigned, or passed whole in the loop.
Benchmarks against Go and Rust
These are the repository's own benchmarks, on an Apple M4 Pro (14 cores), 2026-09-28, with runs interleaved across languages and every implementation printing the same answer. Run them with tern run bench/run.tn, bench/async/run.tn, bench/web2/run.tn, and bench/platforms.tn. The full tables, with the method and the load during each run, are in bench/results.md, bench/async/results.md, bench/web2/results.md, bench/platforms_results.md, and bench/linux_results.md (Linux, in a VM).
Compute (median of 7; bench/results.md):
| Benchmark | Tern | Go | Rust |
|---|---|---|---|
2M tiny tasks (parallel for) | 25ms, 18MB | 444ms per-goroutine, 35ms batched | 24ms rayon, 18MB |
| Fork-join fib(44) | 118ms | 196ms | 116ms rayon |
| N-body, one thread | 1003ms | 2956ms | 1094ms |
| Sieve to 200M | 666ms, 25MB | 767ms, 196MB | 685ms, 192MB (Vec<bool>); 745ms, 25MB (bit vector) |
| Binary trees, parallel | 297ms, 67MB | 1735ms, 178MB | 392ms, 96MB (mimalloc); 853ms, 75MB (system malloc) |
Tasks and servers (bench/async/results.md, bench/web2/results.md; servers on 1 thread):
| Tern | Go | Rust | |
|---|---|---|---|
| ping-pong, 1M messages | 75ms | 206ms | 106ms (tokio) |
| 8 producers into 1 channel, 8M messages | 207ms | 400ms | 884ms |
| 100k tasks sleeping 10ms | 21ms, 26MB | 116ms, 120MB | 56ms, 29MB |
| roost: JSON requests/s | 168k | 129k | 152k (axum) |
| roost: logging + CORS + request-id middleware | 162k | 101k | 108k |
| roost: p99 at 99k requests/s offered (open loop) | 160µs | 11.0ms | 1.67ms |
WebAssembly and the C boundary (bench/platforms_results.md):
| Tern | Rust | |
|---|---|---|
| wasm binary trees | 3.75s | 2.84s (mimalloc); 5.99s (default allocator) |
| wasm sieve | 735ms, 33MB | 773ms, 199MB |
| wasm fib / tasks / n-body | 1.81s / 377ms / 1.18s | 1.82s / 382ms / 1.42s |
| threaded wasm binary trees (14 threads) | 0.48s | 0.63s (mimalloc) |
| calling C: 200M calls + 5M string arguments | 217ms | 261ms |
| closures: map/filter/sort_by on 2M structs + 50M calls | 195ms, 94MB | 215ms, 103MB |
Copy-on-write snapshots (keeping snapshots of a big value while changing it, median of 7; bench/cow/README.md):
| Tern | Go | Rust .clone() | Rust Rc::make_mut | |
|---|---|---|---|---|
| 200k-int array, a snapshot per step | 46ms | 78ms | 45ms | 46ms |
| 1000x1000 grid, one write + a snapshot per step | 14ms | 1532ms | 752ms | 11ms |
| 100k-entry map, a snapshot per step | 21ms | 47ms | 14ms (HashMap); 51ms (IndexMap, ordered like Tern's) | 14ms |
Reading these honestly:
- Tern wins n-body, by 9% natively (8.9G instructions against Rust's 10.5G) and 17% on wasm: it fuses multiply-adds after vectorizing (
-ffp-contract=fast,bench/float/), and uses wasm SIMD. Fork-join is a tie. Rust is ahead on single-threaded wasm binary trees with mimalloc (its nodes are half the size on wasm32). - Parallel tasks, fork-join and the sieve are ties or close. When both compilers generate essentially the same machine code, a tie is the best either can do.
- Tern's wins come from its design, not from out-optimizing LLVM: its per-thread allocator (binary trees, 1.3x ahead of mimalloc), bit-packed
[bool](sieve memory), copied task stacks (a waiting task costs about 210 bytes), radix-sorted number keys, strings passed to C without a copy, and one read transaction per web request. - Copy-on-write costs what an explicit copy costs, and for nested values it copies only the row written, so the obvious Tern beats the obvious Rust 55x on the grid. Rust written with
Rc::make_mutis 1.2x faster there (plain counts, where Tern's are atomic). Rust's unorderedHashMapcopies 1.5x faster than Tern's insertion-ordered map, which is 2.4x faster than Rust's orderedIndexMap. - Go wins one: 8 producers into one channel with many threads on Linux (198ms vs 227ms), because Go moves a woken goroutine onto the waking thread. Everywhere else Go is slower, from 1.2x (sieve) to 18x (one goroutine per tiny task), and uses more memory.
- Near saturation on one server thread, axum's tail latency is lower than Tern's. At equal load Tern's is the lowest on macOS, and with 4 server threads the two are within noise.