百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rubyspeed

> 数据库
开源

将 Ruby 函数编译为 C

185 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

将 Ruby 函数编译为 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:

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

bash
$ 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

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyccompilerperformanceruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

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