Skip to content

agrafix/rubyspeed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rubyspeed

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.int), Rubyspeed::T.array(Rubyspeed::T.int)], return_type: Rubyspeed::T.int)
  def self.dot(a, b)
    c = Rubyspeed::Let.int(0)
    a.each_with_index do |a_val, idx|
      c += a_val * b[idx]
    end
    c
  end
end

This will automatically replace the dot 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 @Deprecated 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

Inspiration

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 :-)

Hacking

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

Releases

No releases published

Packages

No packages published