Skip to content
Tech News
← Back to articles

Spinel: Ruby AOT Native Compiler

read original get Spinel Ruby Compiler Book → more articles
Why This Matters

Spinel represents a significant advancement in Ruby performance by compiling Ruby code into optimized native executables, resulting in speedups of over 11 times compared to traditional CRuby. Its self-hosting design and efficient compilation process make it a promising tool for developers seeking faster Ruby applications, especially in computation-intensive scenarios. This development could influence future Ruby runtime optimizations and inspire similar approaches in other dynamic languages.

Key Takeaways

Spinel -- Ruby AOT Compiler

Spinel compiles Ruby source code into standalone native executables. It performs whole-program type inference and generates optimized C code, achieving significant speedups over CRuby.

Spinel is self-hosting: the compiler backend is written in Ruby and compiles itself into a native binary.

How It Works

Ruby (.rb) | v spinel_parse Parse with Prism (libprism), serialize AST | (C binary, or CRuby + Prism gem as fallback) v AST text file | v spinel_codegen Type inference + C code generation | (self-hosted native binary) v C source (.c) | v cc -O2 -Ilib -lm Standard C compiler + runtime header | v Native binary Standalone, no runtime dependencies

Quick Start

# Fetch libprism sources (from the prism gem on rubygems.org): make deps # Build everything: make # Write a Ruby program: cat > hello.rb << ' RUBY ' def fib ( n ) if n < 2 n else fib(n - 1 ) + fib(n - 2 ) end end puts fib( 34 ) RUBY # Compile and run: ./spinel hello.rb ./hello # prints 5702887 (instantly)

Options

./spinel app.rb # compiles to ./app ./spinel app.rb -o myapp # compiles to ./myapp ./spinel app.rb -c # generates app.c only ./spinel app.rb -S # prints C to stdout

Spinel compiles its own backend. The bootstrap chain:

... continue reading