Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rubyspeed

> 数据库
Open source

Compile ruby functions to C

185 stars0 likes1 views
WebsiteGitHub

About

Compile ruby functions to C

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyccompilerperformanceruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库