Spinel: Ruby AOT Native Compiler

· coding devtools · Source ↗

TLDR

  • Matz’s experimental AOT compiler converts Ruby to optimized C via whole-program type inference, producing standalone native binaries with no runtime dependencies and a geometric mean 11.6x speedup over CRuby miniruby.

Key Takeaways

  • Pipeline: Prism parses Ruby to AST, spinel_codegen.rb (21K lines) does type inference and emits C, then cc -O2 produces a fully standalone binary.
  • Benchmark range is wide: Conway’s Game of Life runs 86.7x faster, GC-heavy gcbench only 2x – speedup scales with computation intensity, not I/O or GC pressure.
  • Self-hosting: the compiler backend is written in the Ruby subset it targets; bootstrap chain closes when gen2.c == gen3.c across three compile passes.
  • Memory model is serious: mark-and-sweep GC with size-segregated free lists, sticky mark bits, and automatic stack allocation for small value-type classes that eliminates GC entirely for qualifying programs.
  • Hard limitations by design: no eval, no send/method_missing/define_method, no Thread (Fiber supported), UTF-8/ASCII only – these cuts are what enable static type inference.

Hacker News Comment Review

  • Commenters agree the metaprogramming and eval cuts are the core trade-off: most real-world Ruby gems depend on these, so Spinel targets a statically analyzable Ruby subset rather than general MRI compatibility.
  • The 21K-line spinel_codegen.rb with up to 15 nesting levels raised maintainability concerns – the consensus is this codebase is AI-assisted and likely AI-maintained going forward, not human-reviewable at scale.
  • Thread omission puzzled several commenters given pthreads would compile cleanly to C; no official answer yet, likely deferred rather than fundamental.

Notable Comments

  • @dluan: Built with Claude in about a month, demoed live at RubyKaigi 2026; project name traces to Matz’s cat, itself named after a Card Captor Sakura character.
  • @throwaway041207: Argues the restricted subset – no eval, no metaprogramming – is actually the Ruby they want: “simpler and easier to understand but with the aesthetic beauty of Ruby intact.”
  • @nu11ptr: “Say what you will about AI, but it has enabled serious speedups in the hands of a talented coder.”

Original | Discuss on HN