FFI is a way to create cross engine rubygems. Wrap a library once, and use it from MRI, JRuby, Rubinious, and mingw32 Ruby. This talk will discuss FFI why you want to use it.
10. LIBFFI
http://sourceware.org/libffi/
“A foreign function interface is the
popular name for the interface that
allows code written in one language
to call code written in another
language.”
Tuesday, November 24, 2009 10
11. FOR RUBYISTS
The Goal :
An extension based upon FFI should
be compatible with many ruby
engines.
Tuesday, November 24, 2009 11
22. LIBSIMPLE_METRICS
http://github.com/copiousfreetime/libsimple_metrics
Tuesday, November 24, 2009 14
23. 1 #ifndef __SIMPLE_METRICS_H__
2 #define __SIMPLE_METRICS_H__
3 #include <stdlib.h>
4 #include <math.h>
5
6 typedef struct _simple_metrics{
7 double min;
8 double max;
9 double sum;
10 double sumsq;
11 long count;
12 } simple_metrics ;
13
14 simple_metrics* simple_metrics_new();
15 void simple_metrics_free( simple_metrics* sm );
16 void simple_metrics_update( simple_metrics* sm, double value );
17 double simple_metrics_mean( simple_metrics* sm );
18 double simple_metrics_min( simple_metrics* sm );
19 double simple_metrics_max( simple_metrics* sm );
20 double simple_metrics_sum( simple_metrics* sm );
21 long simple_metrics_count( simple_metrics* sm );
22 double simple_metrics_stddev( simple_metrics* sm );
23 double simple_metrics_rate( simple_metrics* sm );
24
25 #endif
Tuesday, November 24, 2009 15
24. 1 class Metric
2 attr_reader :name
3 def initialize( name )
4 @name
5 end
6
7 def update( new_value) ... end
8 def count ... end
9 def max ... end
10 def mean ... end
11 def min ... end
12 def rate ... end
13 def sum ... end
14 def stddev ... end
15 end
Tuesday, November 24, 2009 16
29. 35 module SimpleMetrics
36 module FFI
37 class Metric < ::SimpleMetrics::Common
38 include ::SimpleMetrics::FFI
39 def initialize( name )
40 super
41 @impl = FFI.simple_metrics_new
42 end
43
44 def update( v )
45 simple_metrics_update( @impl, v )
46 end
47
48 self.keys.each do |f|
49 module_eval <<-code
50 def #{f}
51 simple_metrics_#{f}( @impl )
52 end
53 code
54 end
55 end
56 end
57 end
Tuesday, November 24, 2009 19
32. MEANINGLESS METRICS
Traditional C
FFI
Extension
Jeremy’s Time 40 minutes 70 minutes
1 Million update()
1.16 sec 0.42 sec
calls
Will it Blend run
Yes No
on JRuby?
Tuesday, November 24, 2009 22
34. % ruby example/bench.rb 1000000
generating 1000000 random numbers between 0 and 10,000
Rehearsal -----------------------------------------------
ext 0.500000 0.000000 0.500000 ( 0.504501)
ffi 0.960000 0.000000 0.960000 ( 0.956847)
-------------------------------------- total: 1.460000sec
user system total real
ext 0.510000 0.000000 0.510000 ( 0.521388)
ffi 0.940000 0.000000 0.940000 ( 0.946538)
% ~/pkgs/jruby-1.4.0/bin/jruby example/bench.rb 1000000
generating 1000000 random numbers between 0 and 10,000
Rehearsal -----------------------------------------------
ffi 0.697000 0.000000 0.697000 ( 0.696000)
-------------------------------------- total: 0.697000sec
user system total real
ffi 0.493000 0.000000 0.493000 ( 0.493000)
Tuesday, November 24, 2009 24
35. FFI BASED PROJECTS
ffi-opengl Johnson FFI port
ffi-tk ffi-tcc
Nokogiri Ruby-SDL-FFI
ffi-wiiuse
ffi-life ffi-opencl rufus-tokyo
ffi-inliner
Rubygame
Ruby-GIR-FFI
Gnu Linear Programming Toolkit NiceFFI
Tuesday, November 24, 2009 25
36. THANKS
• Wayne Meissner - adding libffi to JRuby and providing the
ffi gem for MRI.
• Evan Phoenix - using libffi in Rubinius
• Authors of the examples on the ffi project wiki
Tuesday, November 24, 2009 26