SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
FFI
MAKING CROSS ENGINE EXTENSIONS


  Jeremy Hinegardner
    jeremy@copiousfreetime.org
       twitter: copiousfreetime
COPIOUS FREE TIME
  Fedora           Ruby
 Packaging         Gems

•beanstalkd    •keybox
•haproxy       •rabal
•libtomcrypt   •launchy
•libtommath    •heel
•nginx         •htauth
•ragel         •amalgalite
•tinyproxy     •hitimes
               •stickler
               •crate
I LOVE RUBY
I LIKE C
ANY
EXTENSION DEVELOPERS
    IN THE HOUSE ?
WHO HAS USED FFI?
WHO HAS HEARD OF FFI?
WHAT IS FFI?
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.”
e
              m
           i
    Application
         t
      n
      uses

    u
libsomething_foo()

R       FFI


libsomething.so
App                                            App
JRuby                                          Rubinius
Interpreter                                    Interpreter
                               App
                   Matz Ruby
                something-ffi (pure ruby)
                   Interpreter


               ruby-somethingor ffi gem
                  built-in libffi C extension


                     libsomething.so
 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
 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
142   void Init_simple_metrics_ext()
143   {
144       VALUE cSM_Common;
145
146       mSM  = rb_define_module( quot;SimpleMetricsquot; );
147       mSME = rb_define_module_under( mSM, quot;Extquot; );
148
149       /* load the class we inherit from */
150       rb_require(quot;simplemetrics/commonquot;);
151
152       cSM_Common = rb_const_get( mSM, rb_intern( quot;Commonquot; ) );
153
154       cSME_Metric = rb_define_class_under( mSME, quot;Metricquot;, cSM_Common );
155
156       rb_define_alloc_func(cSME_Metric, sm_alloc);
157       rb_define_method( cSME_Metric, quot;initializequot;, sm_initialize, 1 );
158       rb_define_method( cSME_Metric, quot;updatequot;, sm_update, 1 );
159       rb_define_method( cSME_Metric, quot;countquot;, sm_count, 0 );
160       rb_define_method( cSME_Metric, quot;maxquot;, sm_max, 0 );
161       rb_define_method( cSME_Metric, quot;minquot;, sm_min, 0 );
162       rb_define_method( cSME_Metric, quot;meanquot;, sm_mean, 0 );
163       rb_define_method( cSME_Metric, quot;ratequot;, sm_rate, 0 );
164       rb_define_method( cSME_Metric, quot;sumquot;, sm_sum, 0 );
165       rb_define_method( cSME_Metric, quot;stddevquot;, sm_stddev, 0 );
166
167   }
7   module SimpleMetrics
 8     module FFI
 9
10       class Struct < ::FFI::ManagedStruct
11         layout :min, :double, :max, :double, :sum, :double,
12                :sumsq, :double, :count, :long
13         def self.release( ptr )
14           SimpleMetrics::FFI.simple_metrics_free( ptr )
15         end
                             library function      parameters          return type
16       end
17
18       extend ::FFI::Library
19       ffi_lib quot;libsimple_metricsquot;
20
21       attach_function    :simple_metrics_new,   [          ], :pointer
22       attach_function    :simple_metrics_free,  [ :pointer ], :void
23       attach_function    :simple_metrics_update,[ :pointer, :double  ], :void
24       attach_function    :simple_metrics_min,   [ :pointer ], :double
25       attach_function    :simple_metrics_max,   [ :pointer ], :double
26       attach_function    :simple_metrics_mean,  [ :pointer ], :double
27       attach_function    :simple_metrics_sum,   [ :pointer ], :double
28       attach_function    :simple_metrics_count, [ :pointer ], :long
29       attach_function    :simple_metrics_stddev,[ :pointer ], :double
30       attach_function    :simple_metrics_rate,  [ :pointer ], :double
31
32     end
33   end
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
10   class Struct < ::FFI::ManagedStruct                  6   typedef struct _simple_metrics{
11     layout :min, :double,                              7       double min;
12            :max, :double,                              8       double max;
13            :sum, :double,                              9       double sum;
14            :sumsq, :double,                           10       double sumsq;
15            :count, :long                              11       long   count;
16     def self.release( ptr )                           12   } simple_metrics ;
17       SimpleMetrics::FFI.simple_metrics_free( ptr )
18     end
19   end
21   extend ::FFI::Library
22   ffi_lib quot;libsimple_metricsquot;
23
24   attach_function   :simple_metrics_new,   [          ], :pointer
25   attach_function   :simple_metrics_free,  [ :pointer ], :void
26   attach_function   :simple_metrics_update,[ :pointer, :double  ], :void
27   attach_function   :simple_metrics_mean,  [ :pointer ], :double
28   attach_function   :simple_metrics_min,   [ :pointer ], :double
29   attach_function   :simple_metrics_max,   [ :pointer ], :double
30   attach_function   :simple_metrics_sum,   [ :pointer ], :double
31   attach_function   :simple_metrics_count, [ :pointer ], :long
32   attach_function   :simple_metrics_stddev,[ :pointer ], :double
33   attach_function   :simple_metrics_rate,  [ :pointer ], :double



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 );
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?
% ruby ~/Projects/simple_metrics/example/bench.rb 1000000
generating 1000000 random numbers between 0 and 10,000
Rehearsal -----------------------------------------------
ext           0.420000   0.000000   0.420000 ( 0.460806)
ffi           1.160000   0.000000   1.160000 ( 1.171498)
-------------------------------------- total: 1.580000sec

                  user     system      total         real
ext           0.420000   0.010000   0.430000 (   0.424809)
ffi           1.160000   0.000000   1.160000 (   1.182928)




% ./bin/jruby ~/Projects/simple_metrics/example/bench.rb 1000000
generating 1000000 random numbers between 0 and 10,000
Skipping Ext
Rehearsal -----------------------------------------------
ffi           0.922000   0.000000   0.922000 ( 0.922000)
-------------------------------------- total: 0.922000sec

                  user     system      total         real
ffi           0.834000   0.000000   0.834000 (   0.834000)
FFI GEMS
   ffi-ncurses                  tidy_ffi




                     4
ffi-swig-generator              ffi-zlib
GEMS WITH EXTENSIONS



   346
      Out of 4,501 gems
GET
 TO
WORK!!
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 Project Kenai wiki
LINKS

    http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html
•

    http://kenai.com/projects/ruby-ffi
•

    http://wmeissner.blogspot.com/
•

    http://www.igvita.com/2009/01/15/bridging-mri-jruby-rubinius-with-ffi/
•

    http://blog.segment7.net/articles/2008/01/15/rubinius-foreign-function-
•
    interface
QUESTIONS?

Más contenido relacionado

Similar a Ffi

The Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmThe Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmAndrey Karpov
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...PVS-Studio
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Andrey Karpov
 
I need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfI need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfpnaran46
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonManageIQ
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsMohammad Shaker
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Andrey Karpov
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
A Z Introduction To Ruby On Rails
A Z Introduction To Ruby On RailsA Z Introduction To Ruby On Rails
A Z Introduction To Ruby On Railsrailsconf
 
How I Built a Power Debugger Out of the Standard Library and Things I Found o...
How I Built a Power Debugger Out of the Standard Library and Things I Found o...How I Built a Power Debugger Out of the Standard Library and Things I Found o...
How I Built a Power Debugger Out of the Standard Library and Things I Found o...doughellmann
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
Verilog TASKS & FUNCTIONS
Verilog TASKS & FUNCTIONSVerilog TASKS & FUNCTIONS
Verilog TASKS & FUNCTIONSDr.YNM
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceJesse Vincent
 

Similar a Ffi (20)

The Unicorn's Travel to the Microcosm
The Unicorn's Travel to the MicrocosmThe Unicorn's Travel to the Microcosm
The Unicorn's Travel to the Microcosm
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
 
I need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdfI need help to modify my code according to the instructions- Modify th.pdf
I need help to modify my code according to the instructions- Modify th.pdf
 
Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron Patterson
 
Compiler tricks
Compiler tricksCompiler tricks
Compiler tricks
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+Operators
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Inline function
Inline functionInline function
Inline function
 
A Z Introduction To Ruby On Rails
A Z Introduction To Ruby On RailsA Z Introduction To Ruby On Rails
A Z Introduction To Ruby On Rails
 
A-Z Intro To Rails
A-Z Intro To RailsA-Z Intro To Rails
A-Z Intro To Rails
 
How I Built a Power Debugger Out of the Standard Library and Things I Found o...
How I Built a Power Debugger Out of the Standard Library and Things I Found o...How I Built a Power Debugger Out of the Standard Library and Things I Found o...
How I Built a Power Debugger Out of the Standard Library and Things I Found o...
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Verilog TASKS & FUNCTIONS
Verilog TASKS & FUNCTIONSVerilog TASKS & FUNCTIONS
Verilog TASKS & FUNCTIONS
 
Hiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret SauceHiveminder - Everything but the Secret Sauce
Hiveminder - Everything but the Secret Sauce
 

Más de LittleBIGRuby

The Building Blocks Of Modularity
The Building Blocks Of ModularityThe Building Blocks Of Modularity
The Building Blocks Of ModularityLittleBIGRuby
 
Outside In Development With Cucumber
Outside In Development With CucumberOutside In Development With Cucumber
Outside In Development With CucumberLittleBIGRuby
 
Outside-In Development with Cucumber
Outside-In Development with CucumberOutside-In Development with Cucumber
Outside-In Development with CucumberLittleBIGRuby
 
La Dolce Vita Rubyista
La Dolce Vita RubyistaLa Dolce Vita Rubyista
La Dolce Vita RubyistaLittleBIGRuby
 
Compiling And Optimizing Scripting Languages
Compiling And Optimizing Scripting LanguagesCompiling And Optimizing Scripting Languages
Compiling And Optimizing Scripting LanguagesLittleBIGRuby
 

Más de LittleBIGRuby (7)

The Building Blocks Of Modularity
The Building Blocks Of ModularityThe Building Blocks Of Modularity
The Building Blocks Of Modularity
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
Outside In Development With Cucumber
Outside In Development With CucumberOutside In Development With Cucumber
Outside In Development With Cucumber
 
Outside-In Development with Cucumber
Outside-In Development with CucumberOutside-In Development with Cucumber
Outside-In Development with Cucumber
 
La Dolce Vita Rubyista
La Dolce Vita RubyistaLa Dolce Vita Rubyista
La Dolce Vita Rubyista
 
Compiling And Optimizing Scripting Languages
Compiling And Optimizing Scripting LanguagesCompiling And Optimizing Scripting Languages
Compiling And Optimizing Scripting Languages
 
Sequel
SequelSequel
Sequel
 

Ffi

  • 1. FFI MAKING CROSS ENGINE EXTENSIONS Jeremy Hinegardner jeremy@copiousfreetime.org twitter: copiousfreetime
  • 2. COPIOUS FREE TIME Fedora Ruby Packaging Gems •beanstalkd •keybox •haproxy •rabal •libtomcrypt •launchy •libtommath •heel •nginx •htauth •ragel •amalgalite •tinyproxy •hitimes •stickler •crate
  • 5. ANY EXTENSION DEVELOPERS IN THE HOUSE ?
  • 7. WHO HAS HEARD OF FFI?
  • 9. 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.”
  • 10. e m i Application t n uses u libsomething_foo() R FFI libsomething.so
  • 11. App App JRuby Rubinius Interpreter Interpreter App Matz Ruby something-ffi (pure ruby) Interpreter ruby-somethingor ffi gem built-in libffi C extension libsomething.so
  • 12.  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
  • 13.  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
  • 14. 142 void Init_simple_metrics_ext() 143 { 144     VALUE cSM_Common; 145 146     mSM  = rb_define_module( quot;SimpleMetricsquot; ); 147     mSME = rb_define_module_under( mSM, quot;Extquot; ); 148 149     /* load the class we inherit from */ 150     rb_require(quot;simplemetrics/commonquot;); 151 152     cSM_Common = rb_const_get( mSM, rb_intern( quot;Commonquot; ) ); 153 154     cSME_Metric = rb_define_class_under( mSME, quot;Metricquot;, cSM_Common ); 155 156     rb_define_alloc_func(cSME_Metric, sm_alloc); 157     rb_define_method( cSME_Metric, quot;initializequot;, sm_initialize, 1 ); 158     rb_define_method( cSME_Metric, quot;updatequot;, sm_update, 1 ); 159     rb_define_method( cSME_Metric, quot;countquot;, sm_count, 0 ); 160     rb_define_method( cSME_Metric, quot;maxquot;, sm_max, 0 ); 161     rb_define_method( cSME_Metric, quot;minquot;, sm_min, 0 ); 162     rb_define_method( cSME_Metric, quot;meanquot;, sm_mean, 0 ); 163     rb_define_method( cSME_Metric, quot;ratequot;, sm_rate, 0 ); 164     rb_define_method( cSME_Metric, quot;sumquot;, sm_sum, 0 ); 165     rb_define_method( cSME_Metric, quot;stddevquot;, sm_stddev, 0 ); 166 167 }
  • 15. 7 module SimpleMetrics  8   module FFI  9 10     class Struct < ::FFI::ManagedStruct 11       layout :min, :double, :max, :double, :sum, :double, 12              :sumsq, :double, :count, :long 13       def self.release( ptr ) 14         SimpleMetrics::FFI.simple_metrics_free( ptr ) 15       end library function parameters return type 16     end 17 18     extend ::FFI::Library 19     ffi_lib quot;libsimple_metricsquot; 20 21     attach_function :simple_metrics_new,   [          ], :pointer 22     attach_function :simple_metrics_free,  [ :pointer ], :void 23     attach_function :simple_metrics_update,[ :pointer, :double  ], :void 24     attach_function :simple_metrics_min,   [ :pointer ], :double 25     attach_function :simple_metrics_max,   [ :pointer ], :double 26     attach_function :simple_metrics_mean,  [ :pointer ], :double 27     attach_function :simple_metrics_sum,   [ :pointer ], :double 28     attach_function :simple_metrics_count, [ :pointer ], :long 29     attach_function :simple_metrics_stddev,[ :pointer ], :double 30     attach_function :simple_metrics_rate,  [ :pointer ], :double 31 32   end 33 end
  • 16. 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
  • 17. 10 class Struct < ::FFI::ManagedStruct  6 typedef struct _simple_metrics{ 11   layout :min, :double,  7     double min; 12          :max, :double,  8     double max; 13          :sum, :double,  9     double sum; 14          :sumsq, :double, 10     double sumsq; 15          :count, :long 11     long   count; 16   def self.release( ptr ) 12 } simple_metrics ; 17     SimpleMetrics::FFI.simple_metrics_free( ptr ) 18   end 19 end
  • 18. 21 extend ::FFI::Library 22 ffi_lib quot;libsimple_metricsquot; 23 24 attach_function :simple_metrics_new,   [          ], :pointer 25 attach_function :simple_metrics_free,  [ :pointer ], :void 26 attach_function :simple_metrics_update,[ :pointer, :double  ], :void 27 attach_function :simple_metrics_mean,  [ :pointer ], :double 28 attach_function :simple_metrics_min,   [ :pointer ], :double 29 attach_function :simple_metrics_max,   [ :pointer ], :double 30 attach_function :simple_metrics_sum,   [ :pointer ], :double 31 attach_function :simple_metrics_count, [ :pointer ], :long 32 attach_function :simple_metrics_stddev,[ :pointer ], :double 33 attach_function :simple_metrics_rate,  [ :pointer ], :double 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 );
  • 19. 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?
  • 20. % ruby ~/Projects/simple_metrics/example/bench.rb 1000000 generating 1000000 random numbers between 0 and 10,000 Rehearsal ----------------------------------------------- ext 0.420000 0.000000 0.420000 ( 0.460806) ffi 1.160000 0.000000 1.160000 ( 1.171498) -------------------------------------- total: 1.580000sec user system total real ext 0.420000 0.010000 0.430000 ( 0.424809) ffi 1.160000 0.000000 1.160000 ( 1.182928) % ./bin/jruby ~/Projects/simple_metrics/example/bench.rb 1000000 generating 1000000 random numbers between 0 and 10,000 Skipping Ext Rehearsal ----------------------------------------------- ffi 0.922000 0.000000 0.922000 ( 0.922000) -------------------------------------- total: 0.922000sec user system total real ffi 0.834000 0.000000 0.834000 ( 0.834000)
  • 21. FFI GEMS ffi-ncurses tidy_ffi 4 ffi-swig-generator ffi-zlib
  • 22. GEMS WITH EXTENSIONS 346 Out of 4,501 gems
  • 24. 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 Project Kenai wiki
  • 25. LINKS http://blog.headius.com/2008/10/ffi-for-ruby-now-available.html • http://kenai.com/projects/ruby-ffi • http://wmeissner.blogspot.com/ • http://www.igvita.com/2009/01/15/bridging-mri-jruby-rubinius-with-ffi/ • http://blog.segment7.net/articles/2008/01/15/rubinius-foreign-function- • interface