Performance

Loft runs a program four ways: the interpreter, native (compiled through C99 via --native or --ship), WebAssembly (--native-wasm, compiled from C via clang for WASI), and — for comparison — a hand-written Rust version of the same benchmark. The table below shows the time per operation of the ten engine benchmarks in bench/, loft native (--ship) against its Rust twin.

loft native (--ship, cc -O3) Rust reference (rustc -O)
Benchmark loft native Rust native ÷ Rust
01 fibonacci (recursive fib(30) / fib(31))
1.19 ms
2.30 ms
0.52×
02 sum loop (5 M steps of a loop-carried integer mix)
1.78 ms
2.56 ms
0.70×
03 prime sieve (trial division below 300 000)
15.37 ms
9.15 ms
1.68×
04 Collatz lengths (starts below 200 000)
27.56 ms
20.26 ms
1.36×
05 Mandelbrot (200×200, 256 iter)
4.85 ms
4.84 ms
1.00×
06 Newton sqrt (100 000 calls of 50 steps)
13.68 ms
10.08 ms
1.36×
07 string build (200 000 formatted appends)
4.16 ms
3.59 ms
1.16×
08 word frequency (hash map, 300 000 operations)
4.79 ms
4.16 ms
1.15×
09 dot product (2 M floats)
1.53 ms
1.29 ms
1.19×
10 insertion sort (3 000 integers)
0.98 ms
0.74 ms
1.33×

Time per operation: the median of seven interleaved samples, pinned to one core of an x86-64 Linux machine (bench/portal/results/laptop.tsv), and both lanes printed the same result hash. The source is the performance portal (make perf-portal), which also covers the standard library and the published libraries. Run bench/run_bench.sh from the project root for a quick look on your own hardware, with the Python, loft interpreter and loft wasm lanes beside these two.

Key takeaways

The four loft execution modes have very different performance profiles depending on what the program does.

loft interpreter vs Python

The interpreter runs your program without a separate compilation step. Loft is statically typed, so the interpreter never checks the type of a value at run time; its cost is dispatching each bytecode operation and reaching collection data through the store layer (with looping standard-library helpers compiled ahead of time when a C compiler is available). It is slower than CPython on tight loops and keyed kernels. For hot code, compile with --native or --ship.

loft native vs Rust

The native pipeline compiles loft source to C99 via --native (checked) or --ship (release, cc -O3 with proven-safe fault checks elided), then invokes the host C compiler. Every one of the ten engine benchmarks runs within 1.7× of hand-written Rust: recursion and scalar mixes beat the rustc -O twin (Fibonacci 0.52×, sum loop 0.70×), floating-point kernels match or stay close (Mandelbrot 1.00×, dot product 1.19×, Newton sqrt 1.36×), and text, hash, and sorting workloads sit right alongside them (word count 1.15×, string build 1.16×, insertion sort 1.33×, Collatz 1.36×, prime sieve 1.68×).

loft wasm vs loft native

The wasm build (--native-wasm) compiles the generated C99 via clang for WASI (and --html bundles it for the browser), so it runs anywhere wasmtime or a browser is installed. Arithmetic runs close to native speed; text and store growth trail native slightly because heap expansion inside WebAssembly linear memory goes through memory.grow. Wasm is a good target when native execution is unavailable or portable single-file delivery is the goal.

Bottlenecks

Interpreter — bytecode dispatch The interpreter executes bytecode. The most frequent operator shapes — an integer operator over locals, a compare-and-jump, an element read of an integer vector, the step of a text walk — run as one fused instruction, and every other operation is one dispatch. There is no just-in-time compiler (JIT). Each iteration of a tight loop pays this per-instruction cost.
Native — collection access through the store layer Hash lookup (word count: 1.15×) and indexed vector reads and writes (insertion sort: 1.33×) go through the C store runtime in src/c/. Store-backed records keep headers, bounds checks, and null-sentinel handling that hand-written Rust slices and maps avoid.
Native — checked integer and division arithmetic Integer overflow and division by zero answer null rather than wrapping or trapping (Collatz: 1.36×, Newton sqrt: 1.36×, prime sieve: 1.68×). Counted loops run their index chains unchecked behind one guard; general integer and division expressions still check for the null sentinel.
Native — cross-unit library calls Each used library compiles as its own C unit, so a tight caller loop invoking a small scalar helper across a library boundary keeps state in memory where a single-unit Rust build holds it in registers.
wasm — linear-memory growth Text buffers and stores in the C-to-WASI runtime (src/c/wasm/) live inside WebAssembly linear memory, which grows via memory.grow; workloads that repeatedly grow buffers pay more per reallocation than on native hardware.

Open performance work