🕷 software projects

by ryan davis



sitemap

rubyinline

Inlined foreign code for ultra-fast and flexible ruby.

Inline allows you to write foreign code within your ruby code. It automatically determines if the code in question has changed and builds it only when necessary. The extensions are then automatically loaded into the class/module that defines it.

You can even write extra builders that will allow you to write inlined code in any language. Use Inline::C as a template and look at Module#inline for the required API.

  • Quick and easy inlining of your C or C++ code embedded in your ruby script.
  • Extendable to work with other languages.
  • Automatic conversion between ruby and C basic types
    • char, unsigned, unsigned int, char *, int, long, unsigned long
  • inline_c_raw exists for when the automatic conversion isn’t sufficient.
  • Only recompiles if the inlined code has changed.
  • Pretends to be secure.
  • Only requires standard ruby libraries, nothing extra to download.
1
2
3
4
5
6
7
8
9
10
11
12
13
require "inline"
class MyTest
  inline do |builder|
    builder.c "
      long factorial(int max) {
        int i=max, result=1;
        while (i >= 2) { result *= i--; }
        return result;
      }"
  end
end

factorial_5 = MyTest.new.factorial 5

Benchmarks

> make bench

Running native
Type = Native   , Iter = 1000000, T = 28.70058100 sec, 0.00002870 sec / iter

Running primer - preloads the compiler and stuff
With full builds
Type = Inline C , Iter = 1000000, T = 7.55118600 sec, 0.00000755 sec / iter
Type = InlineRaw, Iter = 1000000, T = 7.54488300 sec, 0.00000754 sec / iter
Type = Alias    , Iter = 1000000, T = 7.53243100 sec, 0.00000753 sec / iter

Without builds
Type = Inline C , Iter = 1000000, T = 7.59543300 sec, 0.00000760 sec / iter
Type = InlineRaw, Iter = 1000000, T = 7.54097200 sec, 0.00000754 sec / iter
Type = Alias    , Iter = 1000000, T = 7.53654000 sec, 0.00000754 sec / iter

Profiling Strategy

  1. Always keep a log of your progress and changes.
  2. Run code with ‘time’ and large dataset.
  3. Run code with ‘-rprofile’ and smaller dataset, large enough to get good #s.
  4. Examine profile output and translate 1 bottleneck to C.
  5. Run new code with ‘time’ and large dataset. Repeat 2-3 if unsatisfied.
  6. Run final code with ‘time’ and compare to the first run.

Get The Code

If you just want to use rubyinline, you can install it via RubyGems:
gem install rubyinline
Fork me on GitHub If you want to hack on rubyinline, clone it from GitHub:
git clone git://github.com/seattlerb/rubyinline

Latest Activity