Skip to content

Performance

Performance

go-ruby-rational/rational is the pure-Go library that rbgo binds for Ruby’s Rational. This page records the methodology for the comparative benchmark of that module against the reference Ruby runtimes — part of the ecosystem-wide per-module parity suite. No numbers are quoted here until they are measured on a pinned host and committed to the benchmark harness.

What is measured

The same Ruby script — a mix of exact Rational arithmetic, inspect/to_s, rounding and Rational(String) parsing over a representative corpus — is run under every runtime. rbgo’s number reflects this pure-Go library doing the work; every other column is that interpreter’s own Rational. So the comparison is the Ruby-visible operation, apples-to-apples across interpreters. The script prints a deterministic checksum and its output is checked byte-identical to MRI before any timing is taken.

Method

  • Best-of-N wall time (best, not mean, to suppress scheduler noise); single-shot processes, no warm-up beyond the script’s own loop.
  • Runtimes: MRI (the oracle) and MRI --yjit; JRuby (on OpenJDK); TruffleRuby (GraalVM CE Native) — each timed cold, single-shot, so JVM / Graal startup is on every run; read those as one-shot ruby file.rb costs, not steady-state JIT numbers.
  • The benchmark script and harness live in rbgo’s repo under bench/modules/. Reproduce with RBGO=./rbgo bash bench/modules/run.sh N.

Honest framing

Exact Rational arithmetic is dominated by big-integer GCD/reduction cost, so the profile depends heavily on operand size; a small-operand round-trip completes fast and the relative noise is high — treat any ratio that completes in well under ~200 ms as order-of-magnitude. Numbers will be added here only once they are real, measured on a pinned host, and reproducible from the committed harness — nothing is estimated or cherry-picked.

Library-level benchmark (Go API vs runtimes) — 2026-07-03

This section measures the pure-Go library directly, through its Go API — not the rbgo interpreter path described above. It isolates the library primitive from Ruby-interpreter dispatch, answering the parity question head-on: is the pure-Go implementation as fast as the reference runtime’s own Rational? The same workload, same inputs, same iteration counts run through the Go library and through each reference runtime’s core Rational; outputs were checked identical to MRI (canonical Rational#to_s) before any timing.

  • Host: Apple M4 Max (Mac16,5, arm64), macOS 26.5.1 — date 2026-07-03.
  • Runtimes: Go 1.26.4 · MRI ruby 4.0.5 +PRISM · MRI + YJIT · JRuby 10.1.0.0 (OpenJDK 25) · TruffleRuby 34.0.1 (GraalVM CE Native).
  • Method: each process runs 3 untimed warm-up passes, then 25 timed passes of a fixed inner loop, timed with a monotonic clock; the best pass is reported as ns/op (lower is better). vs MRI < 1.00× means faster than MRI. Interpreter start-up is outside the timed region, so these are operation costs, not ruby file.rb process costs.
  • Operands: large, non-trivially-reducible values (e.g. Rational(1234567890123, 9876543210)) so GCD normalisation is real work; from-decimal is the Rational(String) conversion ("3.14159"), reduce is a fresh Rational(12345678901234567890, 9876543210987654321) construction.
  • Library version: the numbers below are the pure-Go library with the int64 fast path (a Rational whose numerator and denominator both fit int64 is held in machine words; arithmetic runs in int64 with exact overflow detection and promotes to *big.Rat only on overflow — MRI’s fixnum-num/den strategy). The earlier all-*big.Rat measurement (add 1.58×, mul 1.83×, div 1.77×, from-decimal 2.01×) is superseded; the before→after appears in the table below.

add

Runtimens/opvs MRI
go-ruby (pure Go)33.20.32×
MRI103.01.00×
MRI + YJIT81.00.79×
JRuby207.12.01×
TruffleRuby594.55.77×

mul

Runtimens/opvs MRI
go-ruby (pure Go)42.10.46×
MRI92.01.00×
MRI + YJIT55.00.60×
JRuby241.22.62×
TruffleRuby331.13.60×

div

Runtimens/opvs MRI
go-ruby (pure Go)53.10.54×
MRI99.01.00×
MRI + YJIT61.00.62×
JRuby160.11.62×
TruffleRuby324.03.27×

from-decimal

Runtimens/opvs MRI
go-ruby (pure Go)25.40.25×
MRI102.01.00×
MRI + YJIT67.00.66×
JRuby781.57.66×
TruffleRuby3011.729.53×

to_s

Runtimens/opvs MRI
go-ruby (pure Go)46.30.46×
MRI100.01.00×
MRI + YJIT72.00.72×
JRuby241.42.41×
TruffleRuby429.94.30×

reduce

Runtimens/opvs MRI
go-ruby (pure Go)76.80.28×
MRI272.01.00×
MRI + YJIT253.00.93×
JRuby553.92.04×
TruffleRuby949.83.49×

cmp

Runtimens/opvs MRI
go-ruby (pure Go)4.30.11×
MRI39.01.00×
MRI + YJIT25.00.64×
JRuby80.82.07×
TruffleRuby357.59.17×

Reading the numbers. With the int64 fast path the pure-Go library is now faster than MRI on every operation, and the small-operand rows that used to trail have flipped decisively:

opbefore (all-*big.Rat)after (int64 fast path)
add1.58×0.32×
mul1.83×0.46×
div1.77×0.54×
from-decimal2.01×0.25×
to_s0.96×0.46×
cmp0.93×0.11×
reduce0.35×0.28×

add/mul/div now run entirely in machine words (gcd-reduced in int64, overflow-checked with 128-bit math/bits products), so they land at 0.3–0.5× MRI — roughly 2–3× faster than MRI’s C Rational — instead of ~1.6–1.8× slower. from-decimal gained a dedicated int64 decimal parser and drops to 0.25× (~4× faster). cmp is now an int64 cross-product comparison — a handful of nanoseconds, 0.11×. to_s formats the two int64 fields directly (no *big.Rat), roughly halving its cost to 0.46×. reduce (big coprime operands that intentionally do not fit int64) still exercises the math/big GCD path and holds its existing ~3.5× lead at 0.28×. Exactness is untouched: any operand or intermediate that exceeds int64 promotes to the exact *big.Rat path, and the run.sh verification confirms every result is byte-identical to MRI (canonical Rational#to_s) before timing. MRI + YJIT still trims MRI’s own rows but no longer leads the go column on any operation. The TruffleRuby columns on the shortest loops remain cold-JIT outliers — most visibly from-decimal (29.53×) and cmp (9.17×) — where Graal had not compiled the loop within the warm-up budget; they are not steady-state figures. Every value is a real measured ns/op from the dated run above.

Reproduce

The harness is committed under benchmarks/: a self-contained Go driver (go/, pins the published library by pseudo-version via go.mod — resolved through the Go module proxy, no replace), the equivalent ruby/rational.rb workload, and run.sh. Run bash benchmarks/run.sh; env OUTER/WARM tune the pass budget and RUBY/JRUBY/TRUFFLERUBY select the runtime binaries. run.sh verifies the Go output is byte-identical to MRI (canonical Rational#to_s) and aborts before timing on any mismatch.

Warm-up budget & noise — honest framing

Numbers reflect a fixed warm-process budget (3 warm-up + 25 timed passes in one process). The JVM/GraalVM JITs (JRuby, TruffleRuby) may need a larger warm-up to reach steady state, so their columns can understate peak throughput — most visibly TruffleRuby on the shortest loops (the cold-JIT outliers noted above). Sub-microsecond rows carry the most relative noise; treat those ratios as order-of-magnitude. Every number here is a real measured value from the dated run above — nothing is fabricated, estimated, or cherry-picked. The go-ruby column is the pure-Go library; every other column is that interpreter’s own core Rational doing the equivalent work.