Need help with rubyspeed?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

181 Stars 4 Forks 32 Commits 0 Opened issues


Compile ruby functions to C

Services available


Need anything else?

Contributors list

# 84,733
31 commits


Work in progress.

Welcome to Rubyspeed. Right now, Rubyspeed is a basic proof of concept (horribly hacked together) that allows annotating method declarations to automatically be specialized and compiled to C. Here's an example:

require 'rubyspeed'

class TestClass extend(Rubyspeed::Compiles)

compile!(params: [Rubyspeed::T.array(, Rubyspeed::T.array(], return_type: def, b) c = a.each_with_index do |a_val, idx| c += a_val * b[idx] end c end end

This will automatically replace the

implementation with a compiled C version, that runs quite a bit (5x) faster than the native ruby version:
$ rake bench
               user     system      total        real
compiled   0.000021   0.000004   0.000025 (  0.000018)
ruby       0.000105   0.000002   0.000107 (  0.000103)

How does this work?

In short:

  • Use a neat annotation trick inspired by the sorbet runtime to emulate annotations (compare to
    in Java for example)
  • Extract the ruby source from the given method
  • Transform it to s-expressions
  • Generate C code from the s-expressions
  • Use a C compiler to compile to a ruby module
  • Replace original implementation with a call to the compiled ruby module


This project was inspired by Stephen Diehl's LLVM specializer for Python and rubyinline.

Current Status

The project can only compile extremely primitive functions (basically only simple numeric computations). I am open to any pull requests for improvements, but would discourage using this in production anywhere :-)


$ bundle install
$ rake test # run the tests
$ rake bench # run the benchmarks

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.