Benchmarks

Graphs benchmarks

2 programs measured with axle bench: dijkstra_heap, graph_bfs_hashset. Run time, peak RAM at compile and run, and binary size, with the exact source for each language.

Benchmarks for v0.23.0

Graphs

dijkstra_heap

Dijkstra over a 500k-node / 3M-edge weighted digraph with a binary min-heap hand-rolled on each language's own growable list — deliberately NOT std::priority_queue vs BinaryHeap, which would compare two different sift algorithms. Same lazy-stale-skip rule, same packed (distance, node) key, same logical-size heap that never calls a tail-removal the others lack.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O3302 ms45.5 MB156 ms123.3 MB310 KB
C++ -O3302 ms45.5 MB314 ms110.6 MB16 KB
C++ -O2303 ms45.5 MB310 ms110.5 MB16 KB
Axle -O2311 ms45.5 MB154 ms125.4 MB310 KB
Rust -O3320 ms45.5 MB148 ms146.4 MB3866 KB
C++ -O1322 ms45.5 MB297 ms109.8 MB17 KB
Rust -O2330 ms45.5 MB153 ms146.4 MB3866 KB
Axle -O1337 ms45.5 MB124 ms125.3 MB310 KB
Rust -O1342 ms45.5 MB142 ms146.0 MB3867 KB
Axle -O0754 ms45.5 MB87 ms123.3 MB313 KB
C++ -O0886 ms45.5 MB261 ms105.9 MB36 KB
Rust -O01.13 s45.5 MB103 ms143.3 MB3882 KB
▸ Source
// Dijkstra's shortest paths over a weighted sparse digraph, with a binary
// min-heap built by hand on top of an ArrayList — the routing kernel of
// every map, network planner and build scheduler.
//
// The graph is generated twice from the same LCG (pass 1 counts
// out-degrees, pass 2 scatters targets and weights into CSR arrays), so no
// edge list is materialised. Weights are 1..100.
//
// The priority queue is deliberately NOT each language's stdlib heap: a
// `std::priority_queue` and Axle's ArrayList would then be running
// different sift code. Instead all three implement the same textbook
// binary heap — sift-up on push, swap-root-with-last then sift-down on
// pop — over their own growable list type, so the comparison is the list's
// element access and growth, not two different heap algorithms. Stale
// entries are dropped lazily (`d > dist[node]` → skip), which is what
// makes a heap-based Dijkstra practical without a decrease-key.
//
// The heap carries its own logical size and never shrinks the underlying
// list: a pop overwrites the root and drops the count, and a push reuses a
// retired slot before it appends. All three languages do exactly that — no
// language gets to call a tail-removal the others lack.
//
// The heap entry packs distance above node id (`d * 2^20 + node`), so one
// i64 compare orders the queue identically everywhere.
//
// Two lines are printed: how many nodes were settled, and the sum of their
// final distances.
//
// Equivalent to dijkstra_heap.{cpp,rs} — same graph, same heap, same
// lazy-skip rule, same two printed lines.

use std::collections::ArrayList;

// Store `value` at logical slot `size` and sift it up. The slot is reused
// when a previous pop retired it, appended otherwise. The caller owns the
// logical size and increments it.
fn heap_push(mut h : ArrayList<i64>, size : i64, value : i64) : void ! IndexOutOfBoundsException {
    if (size < h.size()) {
        h.set(size, value);
    } else {
        h.add(value);
    }
    let mut i : i64 = size;
    while (i > 0) {
        let parent : i64 = (i - 1) / 2;
        let pv : i64 = h.get(parent) ?? 0;
        let cv : i64 = h.get(i) ?? 0;
        if (pv <= cv) {
            break;
        }
        h.set(parent, cv);
        h.set(i, pv);
        i = parent;
    }
}

// Return the minimum of a heap of logical length `size`, having moved the
// last element into the root and sifted it down over the remaining
// `size - 1` slots. The caller owns the logical size and decrements it.
fn heap_pop(mut h : ArrayList<i64>, size : i64) : i64 ! IndexOutOfBoundsException {
    let top : i64 = h.get(0) ?? 0;
    let n : i64 = size - 1;
    let last : i64 = h.get(n) ?? 0;
    if (n > 0) {
        h.set(0, last);
        let mut i : i64 = 0;
        while (true) {
            let l : i64 = 2 * i + 1;
            if (l >= n) {
                break;
            }
            let mut smallest : i64 = l;
            let r : i64 = l + 1;
            if (r < n && (h.get(r) ?? 0) < (h.get(l) ?? 0)) {
                smallest = r;
            }
            let sv : i64 = h.get(smallest) ?? 0;
            let cv : i64 = h.get(i) ?? 0;
            if (cv <= sv) {
                break;
            }
            h.set(i, sv);
            h.set(smallest, cv);
            i = smallest;
        }
    }
    return top;
}

fn main() : i32 ! IndexOutOfBoundsException {
    let N : i64 = 500000;
    let M : i64 = 3000000;
    let SHIFT : i64 = 1048576;
    let INF : i64 = 1000000000000000000;

    // Pass 1 — out-degrees.
    let mut off : i32[500001] = i32[500001];
    let mut seed : i64 = 24681357;
    let mut e : i64 = 0;
    while (e < M) {
        seed = (seed * 48271) % 2147483647;
        let u : i64 = seed % N;
        seed = (seed * 48271) % 2147483647;
        seed = (seed * 48271) % 2147483647;
        off[u + 1] = off[u + 1] + 1;
        e = e + 1;
    }
    let mut i : i64 = 0;
    while (i < N) {
        off[i + 1] = off[i + 1] + off[i];
        i = i + 1;
    }

    // Pass 2 — replay the LCG, scatter targets and weights.
    let mut cursor : i32[500000] = i32[500000];
    let mut adj : i32[3000000] = i32[3000000];
    let mut wgt : i32[3000000] = i32[3000000];
    seed = 24681357;
    e = 0;
    while (e < M) {
        seed = (seed * 48271) % 2147483647;
        let u : i64 = seed % N;
        seed = (seed * 48271) % 2147483647;
        let v : i64 = seed % N;
        seed = (seed * 48271) % 2147483647;
        let w : i64 = 1 + seed % 100;
        let slot : i64 = (off[u] as i64) + (cursor[u] as i64);
        adj[slot] = v as i32;
        wgt[slot] = w as i32;
        cursor[u] = cursor[u] + 1;
        e = e + 1;
    }

    let mut dist : i64[500000] = i64[500000];
    let mut d : i64 = 0;
    while (d < N) {
        dist[d] = INF;
        d = d + 1;
    }

    let heap : ArrayList<i64> = new ArrayList<i64>();
    let mut hsize : i64 = 0;
    dist[0] = 0;
    heap_push(heap, hsize, 0);
    hsize = hsize + 1;

    let mut settled : i64 = 0;
    while (hsize > 0) {
        let packed : i64 = heap_pop(heap, hsize);
        hsize = hsize - 1;
        let node : i64 = packed % SHIFT;
        let dcur : i64 = packed / SHIFT;
        if (dcur > dist[node]) {
            // A shorter route to `node` was already settled — stale entry.
            continue;
        }
        settled = settled + 1;

        let start : i64 = off[node] as i64;
        let end : i64 = off[node + 1] as i64;
        let mut k : i64 = start;
        while (k < end) {
            let v : i64 = adj[k] as i64;
            let nd : i64 = dcur + (wgt[k] as i64);
            if (nd < dist[v]) {
                dist[v] = nd;
                heap_push(heap, hsize, nd * SHIFT + v);
                hsize = hsize + 1;
            }
            k = k + 1;
        }
    }

    let mut total : i64 = 0;
    let mut n : i64 = 0;
    while (n < N) {
        if (dist[n] < INF) {
            total = total + dist[n];
        }
        n = n + 1;
    }

    println(settled);
    println(total);
    return 0;
}

graph_bfs_hashset

Breadth-first search over a 1M-node / 3M-edge sparse digraph, frontier in a growable list and visited set in a hash set. The graph is replayed from one LCG (count degrees, then scatter into CSR) instead of being stored twice, and the BFS depth rides inside the queue entry so the hash set is the only visited bookkeeping — two set probes per discovered neighbour, everywhere.

VariantRunCompileOutput
Run timeRAMCompile timeRAMBinary
Axle -O1239 ms56.7 MB161 ms127.6 MB313 KB
Axle -O3242 ms56.6 MB193 ms128.0 MB313 KB
Axle -O2244 ms56.7 MB192 ms125.3 MB313 KB
Rust -O3375 ms55.8 MB199 ms164.8 MB3886 KB
Rust -O2390 ms55.9 MB188 ms165.8 MB3885 KB
Rust -O1391 ms55.9 MB177 ms167.8 MB3885 KB
Axle -O0496 ms56.9 MB100 ms125.8 MB317 KB
C++ -O3545 ms69.1 MB437 ms116.1 MB18 KB
C++ -O2551 ms69.1 MB433 ms116.2 MB18 KB
C++ -O1600 ms69.1 MB415 ms115.8 MB18 KB
C++ -O01.64 s69.1 MB364 ms111.9 MB66 KB
Rust -O01.78 s55.9 MB120 ms161.5 MB3930 KB
▸ Source
// Breadth-first search over a large sparse digraph, with the frontier in
// an ArrayList and the visited set in a HashSet — the reachability query
// behind dependency resolvers, crawlers and "who can see this" checks.
//
// The graph is generated, never stored twice: the SAME LCG runs over the
// edge list once to count out-degrees and a second time to scatter the
// targets into a CSR array. That is the standard memory-lean CSR build,
// and it keeps the working set at (N + M) i32 slots instead of holding an
// edge-pair list.
//
// The BFS itself carries the depth inside the queue entry — the frontier
// stores `depth * 2^20 + node`, so there is no side distance array and the
// HashSet is genuinely the only visited bookkeeping. Each discovered
// neighbour costs exactly two set probes (one `contains`, one `add`) in
// every language.
//
// Two lines are printed: how many nodes are reachable from node 0, and the
// sum of their BFS depths.
//
// Fair-play notes: same N, same M, same LCG and edge order, same CSR
// layout, same two-probe discovery, and each language uses its own stdlib
// hash set (Axle's HashSet, `std::unordered_set`, Rust's `HashSet`) — the
// hasher is part of what is being compared.
//
// Equivalent to graph_bfs_hashset.{cpp,rs}.

use std::collections::{ArrayList, HashSet};

fn main() : i32 {
    let N : i64 = 1000000;
    let M : i64 = 3000000;
    let SHIFT : i64 = 1048576;

    // Pass 1 — out-degree of every node.
    let mut off : i32[1000001] = i32[1000001];
    let mut seed : i64 = 987654321;
    let mut e : i64 = 0;
    while (e < M) {
        seed = (seed * 48271) % 2147483647;
        let u : i64 = seed % N;
        seed = (seed * 48271) % 2147483647;
        off[u + 1] = off[u + 1] + 1;
        e = e + 1;
    }

    // Prefix sum — off[v] is now where v's targets begin.
    let mut i : i64 = 0;
    while (i < N) {
        off[i + 1] = off[i + 1] + off[i];
        i = i + 1;
    }

    // Pass 2 — replay the same LCG and scatter the targets.
    let mut cursor : i32[1000000] = i32[1000000];
    let mut adj : i32[3000000] = i32[3000000];
    seed = 987654321;
    e = 0;
    while (e < M) {
        seed = (seed * 48271) % 2147483647;
        let u : i64 = seed % N;
        seed = (seed * 48271) % 2147483647;
        let v : i64 = seed % N;
        let slot : i64 = (off[u] as i64) + (cursor[u] as i64);
        adj[slot] = v as i32;
        cursor[u] = cursor[u] + 1;
        e = e + 1;
    }

    // BFS from node 0. The queue entry packs the depth above the node id.
    let visited : HashSet<i64> = new HashSet<i64>();
    let queue : ArrayList<i64> = new ArrayList<i64>();
    visited.add(0);
    queue.add(0);

    let mut depth_sum : i64 = 0;
    let mut head : i64 = 0;
    while (head < queue.size()) {
        let packed : i64 = queue.get(head) ?? 0;
        head = head + 1;
        let node : i64 = packed % SHIFT;
        let depth : i64 = packed / SHIFT;
        depth_sum = depth_sum + depth;

        let start : i64 = off[node] as i64;
        let end : i64 = off[node + 1] as i64;
        let mut k : i64 = start;
        while (k < end) {
            let nb : i64 = adj[k] as i64;
            if (!visited.contains(nb)) {
                visited.add(nb);
                queue.add((depth + 1) * SHIFT + nb);
            }
            k = k + 1;
        }
    }

    println(visited.size());
    println(depth_sum);
    return 0;
}