Benchmarks

Memory benchmarks

3 programs measured with axle bench: arena_micro, gather_sweep, shared_rc_micro. Run time, peak RAM at compile and run, and binary size, with the exact source for each language.

Benchmarks for v0.23.0

Memory

arena_micro

Arena-allocation micro-benchmark — millions of short-lived objects, all three languages performing a real per-iteration allocation (C++/Rust forced past inlining, Axle routed onto its real arena allocator).

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O118 ms45.4 MB80 ms125.4 MB318 KB
Axle -O219 ms45.4 MB84 ms123.0 MB318 KB
Axle -O319 ms45.4 MB86 ms123.4 MB318 KB
Axle -O033 ms45.4 MB73 ms123.5 MB319 KB
Rust -O253 ms45.4 MB111 ms142.7 MB3865 KB
Rust -O353 ms45.4 MB111 ms138.8 MB3865 KB
Rust -O155 ms45.4 MB109 ms142.6 MB3866 KB
C++ -O159 ms45.4 MB88 ms95.7 MB16 KB
C++ -O259 ms45.4 MB89 ms95.9 MB16 KB
C++ -O359 ms45.4 MB89 ms95.9 MB16 KB
C++ -O071 ms45.4 MB85 ms91.5 MB16 KB
Rust -O0156 ms45.4 MB89 ms143.0 MB3877 KB
▸ Source
// EXPECTED_EXIT: 0
// Arena-allocation microbench — a REAL per-iteration allocation the
// optimiser cannot elide, so it measures the allocator, not dead-code
// removal.
//
// Each iteration allocates a fresh `Node` and publishes it to `prev`, an
// outer-scope local the NEXT iteration reads back through `prev.value`.
// That cross-iteration alias is the whole point: a stack slot would be
// reused across iterations (every `new Node` re-initialising the one slot
// `prev` aliases), so sema cannot stack-promote it — the escape pass keeps
// it heap-resident and the arena pass, proving it never leaves the frame,
// routes it to the **arena** tier (`arena Node(...)`, a bump-pointer
// allocation). C++ and Rust cannot elide their siblings for the same
// reason: each object's address is observed a step later, so a genuine
// `new` / `Box::new` fires every iteration.
//
// This is the allocator comparison the arena tier exists for: Axle bumps a
// thread-local cursor (no per-object free — the whole run is reclaimed by
// the function's scope-exit rewind), against C++/Rust paying `new`/`delete`
// per iteration. `run` marks the arena once on entry and rewinds once on
// return, so the hot loop is 5M bumps.

use std::sys::process::argc;

class Node {
    pub(file) value : i64;

    constructor(v : i64) {
        self.value = v;
    }
}

// Per-iteration allocation, aliased one step forward so the object is a
// real (non-elidable) allocation the arena serves by bumping.
fn run(n : i64) : i64 {
    let mut prev : Node = new Node(0);
    let mut acc : i64 = 0;
    let mut i : i64 = 1;
    let mut state : i64 = 88172645463325252;
    while (i <= n) {
        state = state * 6364136223846793005 + 1442695040888963407;
        let cur : Node = new Node(state);
        acc = acc + prev.value;
        prev = cur;
        i = i + 1;
    }
    return acc;
}

fn main() : i32 {
    let n : i64 = 5000000 + (argc() as i64);
    println(run(n));
    return 0;
}

gather_sweep

One scattered read swept across the whole cache hierarchy — five working sets from 64 B to 64 MiB, reported as picoseconds per gathered element. The point is the shape of the two curves rather than either endpoint: inside L1 the loop is bound by the dependent index -> load -> accumulate chain, past L3 by memory latency, and whether Axle pays more than C++ for the second is a question about how they diverge.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
C++ -O2303 ms66.7 MB534 ms123.9 MB16 KB
C++ -O3303 ms66.7 MB551 ms123.6 MB16 KB
Axle -O2304 ms65.5 MB96 ms127.1 MB308 KB
Axle -O3304 ms65.5 MB96 ms126.0 MB308 KB
Rust -O3308 ms65.9 MB126 ms149.7 MB3873 KB
C++ -O1313 ms66.7 MB529 ms122.7 MB16 KB
Rust -O2313 ms65.9 MB127 ms147.9 MB3873 KB
Axle -O1344 ms65.5 MB86 ms125.9 MB308 KB
Rust -O1360 ms65.9 MB118 ms149.7 MB3873 KB
Axle -O0576 ms65.5 MB79 ms126.1 MB308 KB
C++ -O0785 ms66.7 MB522 ms117.3 MB17 KB
▸ Source
// EXPECTED_EXIT: 0
// Gather sweep — the same scattered read, over a table swept across the
// cache hierarchy. Shared logic with gather_sweep.cpp / gather_sweep.rs
// (run via `axle bench --compare cpp,rust`).
//
// # What it measures, and why a sweep
//
// A cache question cannot be answered at one working-set size. The point is
// the *shape of the curve*: a gather over a table that fits L1 is bounded by
// the dependent chain `index -> load -> accumulate`, and the same gather over
// a table past L3 is bounded by memory latency instead. Whether this language
// pays more than C++ for the second is a question about how the two curves
// diverge, not about either endpoint.
//
// So one kernel runs at five sizes — 64 B, 16 KiB, 256 KiB, 4 MiB, 64 MiB —
// landing inside L1, inside L2, around L3, and well past it on a typical
// desktop part. Each is timed on its own and reported as **picoseconds per
// element**; the iteration count is held fixed across sizes, so a rising
// number is the memory system and nothing else.
//
// # Why the reads cannot be elided or predicted
//
// Each index comes from an xorshift step over the previous one, so the address
// of a read depends on arithmetic the hardware prefetcher cannot run ahead of
// — that is what makes this a *gather* rather than a stride. The accumulator
// reaches the output, so no read is dead, and the table is filled from its own
// index, so no load folds to a constant.
//
// # Why these five sizes
//
// They are the sweep behind `axle_common::limits::RANDOM_ACCESS_UNROLL_FACTOR`,
// deliberately: that constant's measurement is the only prior reading of this
// hierarchy in the tree, and a second instrument choosing other points could
// not be compared against it.

use std::sys::process::argc;
use std::sys::nanoTime;

// Element counts, 8 bytes each: 64 B, 16 KiB, 256 KiB, 4 MiB, 64 MiB.
const SIZE_L1 : i64 = 8;
const SIZE_L2 : i64 = 2048;
const SIZE_L3 : i64 = 32768;
const SIZE_BIG : i64 = 524288;
const SIZE_RAM : i64 = 8388608;

// Held fixed across every size, so picoseconds/element compare directly.
const ITERS : i64 = 20000000;

// Time one working-set size and report picoseconds per gathered element.
//
// Allocation, fill, gather and release all happen here: a heap buffer whose
// count is dynamic is a pointer, and handing it across a call would say
// nothing this loop does not already say.
fn measure(len : i64, seed : i64) : i64 {
    let mut table : i64[] = new i64[len];

    let mut i : i64 = 0;
    while (i < len) {
        table[i] = i * 2654435761 + 1;
        i = i + 1;
    }

    // `len` is a power of two, so the index wraps with an `and` rather than a
    // division — the loop measures the memory system, not the modulo.
    let mask : i64 = len - 1;
    let mut acc : i64 = 0;
    let mut state : i64 = seed;
    let start : i64 = nanoTime();
    let mut k : i64 = 0;
    while (k < ITERS) {
        // `>>>`: the xorshift is specified over an unsigned word.
        state = state ^ (state << 13);
        state = state ^ (state >>> 7);
        state = state ^ (state << 17);
        acc = acc + table[state & mask];
        k = k + 1;
    }
    let elapsed : i64 = nanoTime() - start;

    drop table;
    // Picoseconds per gathered element. `acc` is *not* folded in here: an
    // expression a constant-fold can prove is zero (`acc & 0`) takes the whole
    // accumulation with it, and the first draft of this file measured five
    // eliminated loops. It is returned instead, and printed by the caller.
    println(elapsed * 1000 / ITERS);
    return acc;
}

fn main() : i32 {
    // `argc()` is opaque to the optimiser, so the seed is not a constant and
    // the sweep cannot be evaluated at compile time.
    let seed : i64 = 88172645463325252 + (argc() as i64);
    let mut sink : i64 = 0;

    // Ascending working-set order: the curve reads top to bottom.
    sink = sink + measure(SIZE_L1, seed);
    sink = sink + measure(SIZE_L2, seed);
    sink = sink + measure(SIZE_L3, seed);
    sink = sink + measure(SIZE_BIG, seed);
    sink = sink + measure(SIZE_RAM, seed);

    // The accumulators reach the output, which is what keeps five gather loops
    // alive. Printed after the curve so the five timings read as a block.
    println(sink);
    return 0;
}

shared_rc_micro

Thread-locality feature bench — Axle's Shared<T> proven never to cross a spawn drops to a non-atomic refcount, compared against Rust's non-atomic Rc<T> (the fair baseline) and the always-atomic Arc<T> / std::shared_ptr<T>.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O141 ms45.3 MB75 ms121.4 MB317 KB
Axle -O241 ms45.3 MB81 ms123.5 MB317 KB
Axle -O341 ms45.3 MB81 ms123.3 MB317 KB
C++ -O359 ms45.3 MB95 ms96.5 MB16 KB
C++ -O160 ms45.3 MB94 ms95.9 MB16 KB
C++ -O261 ms45.3 MB95 ms96.5 MB16 KB
Rust -O164 ms45.3 MB115 ms146.8 MB3866 KB
Rust -O364 ms45.3 MB116 ms149.2 MB3866 KB
Rust -O269 ms45.3 MB116 ms149.0 MB3866 KB
Axle -O0112 ms45.3 MB72 ms125.0 MB317 KB
C++ -O0134 ms45.3 MB86 ms91.4 MB16 KB
Rust -O0260 ms45.3 MB95 ms147.7 MB3878 KB
▸ Source
// EXPECTED_EXIT: 0
// Shared<Node> refcount microbench — a REAL per-iteration retain/release
// the optimiser cannot elide, so it measures the refcount + allocator,
// not dead-code removal.
//
// Each iteration allocates a fresh `shared Node`, aliases it once
// (a retain — strong 1→2), reads both the alias and the outer-scope
// `prev` handle the PREVIOUS iteration published, then hands the new node
// to `prev`. That reassignment releases the previous node (its last strong
// drop → free) and the intra-iteration alias drops at scope exit (a second
// release). The cross-iteration `prev` is the whole point: the handle
// escapes the loop body — a step later its value is still observed — so
// sema cannot stack-promote the node and elide its refcount, exactly as
// the C++ / Rust siblings cannot elide `new`/`Rc::new` whose address is
// observed a step later.
//
// `Node`'s `Shared<Node>` never crosses a `spawn`, so
// `axle_sema::hir_analyses::thread_locality` proves the class thread-local:
// every retain / release lowers to a plain load/add-or-sub/store instead of
// an `atomicrmw`, and the last-strong drop skips the `fence(Acquire)`
// The C++ sibling
// hand-rolls a non-atomic intrusive count and Rust uses `Rc<T>`, its own
// non-atomic shared pointer, so all three measure the identical thing:
// non-atomic retain/release churn under a genuine per-step allocation.
use std::sys::process::argc;

class Node {
    pub(file) value : i64;

    constructor(v : i64) {
        self.value = v;
    }
}

// Per-iteration alloc + retain + release, with the node aliased one step
// forward through `prev` so it is a real (non-elidable) shared allocation.
fn run(n : i64) : i64 {
    let mut prev = share new Node(0);
    let mut acc : i64 = 0;
    let mut i : i64 = 1;
    let mut state : i64 = 88172645463325252;
    while (i <= n) {
        state = state * 6364136223846793005 + 1442695040888963407;
        let cur = share new Node(state);
        let alias = cur;
        acc = acc + prev.value + alias.value;
        prev = cur;
        i = i + 1;
    }
    return acc;
}

fn main() : i32 {
    let n : i64 = 5000000 + (argc() as i64);
    println(run(n));
    return 0;
}