Skip to content

Benchmarks

Buntos edited this page Dec 5, 2026 · 1 revision

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.


Running the Benchmarks

From the repository root (after zig build self-host):

./benchmarks/run.sh

benchmarks/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.


Measured Results (Linux x86_64)

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)

Technical Analysis

1. Why N-Body Matches gcc -O3 (75.37 ms vs 76.00 ms)

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
end

Three design choices combine to eliminate abstraction overhead:

  1. Unboxed value structs: Vec3 and Body are flat LLVM aggregates stored contiguously in a scoped with_arena buffer with zero GC headers or pointer chasing.
  2. @[nogc] hot path: The entire advance / energy / run_nbody loop is statically verified @[nogc] with zero runtime allocation.
  3. LLVM default<O3> optimization pipeline: Because hlc runs LLVMRunPasses("default<O3>"), LLVM inlines every operator method (+, -, *, norm2), applies Scalar Replacement of Aggregates (SROA) to keep x/y/z in SSE/AVX registers, and emits essentially identical floating-point loops to benchmarks/nbody.c's static inline helpers.

2. Why Sieve Is Slightly Behind C (13.75 ms vs 11.21 ms)

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_alloc allocates via malloc and eagerly memsets the 10 MB block to zero, and sieve.hl performs an explicit initialization pass (for i in 0..=limit). In sieve.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 default Array[T] and Slice[T] indexing inserts runtime bounds checks (hl_bounds_check) for safety. Even when using raw arena buffers, H's sieve stays within ~1.23x of gcc -O3 and ~42.6x ahead of MRI Ruby (585.97 ms).

3. Pure-H JSON Throughput (40.29 ms for 20,000 Roundtrips)

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

Clone this wiki locally