Repository navigation
Benchmarks
The benchmarks/ directory contains reproducible native performance benchmarks comparing H (hlc -O3, running LLVM's default<O3> IR optimization pipeline via LLVMRunPasses) against C (gcc -O3) and Ruby, with exact output/checksum verification across implementations.
From the repository root (after zig build self-host):
./benchmarks/run.shbenchmarks/run.sh compiles every benchmark at -O3 (hlc-pure -O3 --runtime zig-out/lib/libhl_runtime_h.o and gcc -O3), runs them sequentially, and prints each implementation's verified result and wall-clock time via CLOCK_MONOTONIC.
| Benchmark | Workload & Checksum | H (hlc -O3) |
C (gcc -O3) |
Ruby | Relative Speed |
|---|---|---|---|---|---|
| Sieve of Eratosthenes |
N = 10,000,000, count = 664579
|
13.75 ms | 11.21 ms | 585.97 ms | ~42.6x faster than Ruby, ~1.23x C |
| N-Body Simulation | 5-body gravitational, 2,000,000 steps, energy = -0.169026286
|
75.37 ms | 76.00 ms | — |
Matches gcc -O3 (~1.01x faster) |
| JSON Parse + Stringify |
20,000 full roundtrips (std/json) |
40.29 ms | — | — | ~2.0 µs / roundtrip (pure-H GC) |
benchmarks/nbody.hl writes the simulation idiomatically using value types and overloaded arithmetic operators — not flattened scalar arrays:
@[repr(C)]
struct Vec3
x: F64
y: F64
z: F64
@[nogc]
def +(o: Vec3) -> Vec3
Vec3.new(@x + o.x, @y + o.y, @z + o.z)
end
@[nogc]
def -(o: Vec3) -> Vec3
Vec3.new(@x - o.x, @y - o.y, @z - o.z)
end
@[nogc]
def *(s: F64) -> Vec3
Vec3.new(@x * s, @y * s, @z * s)
end
@[nogc]
def norm2() -> F64
@x * @x + @y * @y + @z * @z
end
endThree design choices combine to eliminate abstraction overhead:
-
Unboxed value
structs:Vec3andBodyare flat LLVM aggregates stored contiguously in a scopedwith_arenabuffer with zero GC headers or pointer chasing. -
@[nogc]hot path: The entireadvance/energy/run_nbodyloop is statically verified@[nogc]with zero runtime allocation. -
LLVM
default<O3>optimization pipeline: BecausehlcrunsLLVMRunPasses("default<O3>"), LLVM inlines every operator method (+,-,*,norm2), applies Scalar Replacement of Aggregates (SROA) to keepx/y/zin SSE/AVX registers, and emits essentially identical floating-point loops tobenchmarks/nbody.c'sstatic inlinehelpers.
benchmarks/sieve.hl runs the sieve inside @[nogc] with_arena(limit + 64) over a 10 MB Slice[U8] (a.alloc_slice[U8](limit + 1)), indexing via p = is_comp.ptr. The ~2.5 ms gap relative to benchmarks/sieve.c comes from two concrete differences:
-
Initialization cost:
hl_arena_allocallocates viamallocand eagerlymemsets the 10 MB block to zero, andsieve.hlperforms an explicit initialization pass (for i in 0..=limit). Insieve.c,calloc(limit + 1, 1)obtains zeroed pages directly from the OS without redundant writes. -
Bounds checking in general H code: Outside raw
Ptr[T]indexing, H's defaultArray[T]andSlice[T]indexing inserts runtime bounds checks (hl_bounds_check) for safety. Even when using raw arena buffers, H's sieve stays within ~1.23x ofgcc -O3and ~42.6x ahead of MRI Ruby (585.97 ms).
benchmarks/json_bench.hl parses a nested JSON object (containing integers, strings, booleans, arrays, and floats) into a GC-managed JsonValue AST using std/json.parse and serializes it back with std/json.stringify 20,000 times (~2.0 µs per parse + stringify cycle). Unlike Sieve and N-Body, this benchmark exercises the default Boehm GC heap (class, Array[T], String allocation) end to end.
Return to Wiki Home