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


                               Jeremy Hinegardner
                                 jeremy@copiousfreetime.org
                                    twitter: copiousfreetime




Tuesday, November 24, 2009                                     1
I LOVE RUBY

Tuesday, November 24, 2009   2
I LIKE C

Tuesday, November 24, 2009              3
SURVEY TIME




Tuesday, November 24, 2009                 4
RUBY DEVELOPERS?




Tuesday, November 24, 2009                      5
WRITER OF LIBRARIES?




Tuesday, November 24, 2009                     6
WRITER OF EXTENSIONS?




Tuesday, November 24, 2009             7
USED BY MULTIPLE ENGINES?




Tuesday, November 24, 2009         8
WHAT IS FFI?




Tuesday, November 24, 2009                  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.”




Tuesday, November 24, 2009                                           10
FOR RUBYISTS

                                         The Goal :

                             An extension based upon FFI should
                               be compatible with many ruby
                                          engines.




Tuesday, November 24, 2009                                        11
libfoo.so




Tuesday, November 24, 2009               12
Application
                               uses
                             libfoo_bar()




                              libfoo.so




Tuesday, November 24, 2009                  12
Application
                               uses
                             libfoo_bar()



                                 FFI


                              libfoo.so




Tuesday, November 24, 2009                  12
m e
                                         i
                                 Application
                                       t
                                   n
                                   uses


                             R   u
                                 libfoo_bar()



                                     FFI


                                  libfoo.so




Tuesday, November 24, 2009                            12
App
                               Matz Ruby
                               Interpreter


                             ruby-foo C extension


                                   libfoo.so


Tuesday, November 24, 2009                          13
App                                   App
       JRuby                                              Rubinius
       Interpreter                                        Interpreter
                                               App
                                     Matz Ruby
                                     Interpreter


                                   ruby-foo C extension


                                         libfoo.so


Tuesday, November 24, 2009                                               13
App                           App
       JRuby                                      Rubinius
       Interpreter                                Interpreter
                                            App
                                   Matz Ruby
                                   Interpreter




                                      libfoo.so


Tuesday, November 24, 2009                                       13
App            App            App
       JRuby                       Matz Ruby      Rubinius
       Interpreter                 Interpreter    Interpreter




                                      libfoo.so


Tuesday, November 24, 2009                                       13
App                App                    App
       JRuby                          Matz Ruby               Rubinius
       Interpreter                    Interpreter             Interpreter




                                   built-in libffi or ffi gem


                                          libfoo.so


Tuesday, November 24, 2009                                                   13
App                App                    App
       JRuby                          Matz Ruby               Rubinius
       Interpreter                    Interpreter             Interpreter



                                     foo-ffi (pure ruby)


                                   built-in libffi or ffi gem


                                          libfoo.so


Tuesday, November 24, 2009                                                   13
LIBSIMPLE_METRICS
                               http://github.com/copiousfreetime/libsimple_metrics




Tuesday, November 24, 2009                                                           14
 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
 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
142     void Init_simple_metrics_ext()
           143     {
           144         VALUE cSM_Common;
           145
           146         mSM  = rb_define_module( "SimpleMetrics" );
           147         mSME = rb_define_module_under( mSM, "Ext" );
           148
           149         /* load the class we inherit from */
           150         rb_require("simplemetrics/common");
           151
           152         cSM_Common = rb_const_get( mSM, rb_intern( "Common" ) );
           153
           154         cSME_Metric = rb_define_class_under( mSME, "Metric", cSM_Common );
           155
           156         rb_define_alloc_func(cSME_Metric, sm_alloc);
           157         rb_define_method( cSME_Metric, "initialize", sm_initialize, 1 );
           158         rb_define_method( cSME_Metric, "update", sm_update, 1 );
           159         rb_define_method( cSME_Metric, "count", sm_count, 0 );
           160         rb_define_method( cSME_Metric, "max", sm_max, 0 );
           161         rb_define_method( cSME_Metric, "min", sm_min, 0 );
           162         rb_define_method( cSME_Metric, "mean", sm_mean, 0 );
           163         rb_define_method( cSME_Metric, "rate", sm_rate, 0 );
           164         rb_define_method( cSME_Metric, "sum", sm_sum, 0 );
           165         rb_define_method( cSME_Metric, "stddev", sm_stddev, 0 );
           166
           167     }




Tuesday, November 24, 2009                                                                  17
142     void Init_simple_metrics_ext()
           143     {
           144         VALUE cSM_Common;
           145
           146         mSM  = rb_define_module( "SimpleMetrics" );
           147         mSME = rb_define_module_under( mSM, "Ext" );
           148
           149         /* load the class we inherit from */
           150         rb_require("simplemetrics/common");
           151
           152         cSM_Common = rb_const_get( mSM, rb_intern( "Common" ) );
           153
           154         cSME_Metric = rb_define_class_under( mSME, "Metric", cSM_Common );
           155
           156         rb_define_alloc_func(cSME_Metric, sm_alloc);
           157         rb_define_method( cSME_Metric, "initialize", sm_initialize, 1 );
           158         rb_define_method( cSME_Metric, "update", sm_update, 1 );
           159         rb_define_method( cSME_Metric, "count", sm_count, 0 );
           160         rb_define_method( cSME_Metric, "max", sm_max, 0 );
           161         rb_define_method( cSME_Metric, "min", sm_min, 0 );
           162         rb_define_method( cSME_Metric, "mean", sm_mean, 0 );
           163         rb_define_method( cSME_Metric, "rate", sm_rate, 0 );
           164         rb_define_method( cSME_Metric, "sum", sm_sum, 0 );
           165         rb_define_method( cSME_Metric, "stddev", sm_stddev, 0 );
           166
           167     }




Tuesday, November 24, 2009                                                                  17
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
            16       end
            17
            18       extend ::FFI::Library
            19       ffi_lib "libsimple_metrics"
            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



Tuesday, November 24, 2009                                                                     18
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
            16       end                 library function      parameters          return type
            17
            18       extend ::FFI::Library
            19       ffi_lib "libsimple_metrics"
            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



Tuesday, November 24, 2009                                                                       18
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
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




Tuesday, November 24, 2009                                                                   20
21    extend ::FFI::Library
        22    ffi_lib "libsimple_metrics"
        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 );




Tuesday, November 24, 2009                                                                   21
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
DEMO




Tuesday, November 24, 2009          23
% 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
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
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
LINKS


    •   http://github.com/ffi/ffi

    •   http://wmeissner.blogspot.com/

    •   http://groups.google.com/group/ruby-ffi




Tuesday, November 24, 2009                       27
QUESTIONS?




Tuesday, November 24, 2009                28

Más contenido relacionado

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

FFI -- creating cross engine rubygems

  • 1. FFI MAKING CROSS ENGINE EXTENSIONS Jeremy Hinegardner jeremy@copiousfreetime.org twitter: copiousfreetime Tuesday, November 24, 2009 1
  • 2. I LOVE RUBY Tuesday, November 24, 2009 2
  • 3. I LIKE C Tuesday, November 24, 2009 3
  • 6. WRITER OF LIBRARIES? Tuesday, November 24, 2009 6
  • 7. WRITER OF EXTENSIONS? Tuesday, November 24, 2009 7
  • 8. USED BY MULTIPLE ENGINES? Tuesday, November 24, 2009 8
  • 9. WHAT IS FFI? Tuesday, November 24, 2009 9
  • 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
  • 13. Application uses libfoo_bar() libfoo.so Tuesday, November 24, 2009 12
  • 14. Application uses libfoo_bar() FFI libfoo.so Tuesday, November 24, 2009 12
  • 15. m e i Application t n uses R u libfoo_bar() FFI libfoo.so Tuesday, November 24, 2009 12
  • 16. App Matz Ruby Interpreter ruby-foo C extension libfoo.so Tuesday, November 24, 2009 13
  • 17. App App JRuby Rubinius Interpreter Interpreter App Matz Ruby Interpreter ruby-foo C extension libfoo.so Tuesday, November 24, 2009 13
  • 18. App App JRuby Rubinius Interpreter Interpreter App Matz Ruby Interpreter libfoo.so Tuesday, November 24, 2009 13
  • 19. App App App JRuby Matz Ruby Rubinius Interpreter Interpreter Interpreter libfoo.so Tuesday, November 24, 2009 13
  • 20. App App App JRuby Matz Ruby Rubinius Interpreter Interpreter Interpreter built-in libffi or ffi gem libfoo.so Tuesday, November 24, 2009 13
  • 21. App App App JRuby Matz Ruby Rubinius Interpreter Interpreter Interpreter foo-ffi (pure ruby) built-in libffi or ffi gem libfoo.so Tuesday, November 24, 2009 13
  • 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
  • 25. 142 void Init_simple_metrics_ext() 143 { 144     VALUE cSM_Common; 145 146     mSM  = rb_define_module( "SimpleMetrics" ); 147     mSME = rb_define_module_under( mSM, "Ext" ); 148 149     /* load the class we inherit from */ 150     rb_require("simplemetrics/common"); 151 152     cSM_Common = rb_const_get( mSM, rb_intern( "Common" ) ); 153 154     cSME_Metric = rb_define_class_under( mSME, "Metric", cSM_Common ); 155 156     rb_define_alloc_func(cSME_Metric, sm_alloc); 157     rb_define_method( cSME_Metric, "initialize", sm_initialize, 1 ); 158     rb_define_method( cSME_Metric, "update", sm_update, 1 ); 159     rb_define_method( cSME_Metric, "count", sm_count, 0 ); 160     rb_define_method( cSME_Metric, "max", sm_max, 0 ); 161     rb_define_method( cSME_Metric, "min", sm_min, 0 ); 162     rb_define_method( cSME_Metric, "mean", sm_mean, 0 ); 163     rb_define_method( cSME_Metric, "rate", sm_rate, 0 ); 164     rb_define_method( cSME_Metric, "sum", sm_sum, 0 ); 165     rb_define_method( cSME_Metric, "stddev", sm_stddev, 0 ); 166 167 } Tuesday, November 24, 2009 17
  • 26. 142 void Init_simple_metrics_ext() 143 { 144     VALUE cSM_Common; 145 146     mSM  = rb_define_module( "SimpleMetrics" ); 147     mSME = rb_define_module_under( mSM, "Ext" ); 148 149     /* load the class we inherit from */ 150     rb_require("simplemetrics/common"); 151 152     cSM_Common = rb_const_get( mSM, rb_intern( "Common" ) ); 153 154     cSME_Metric = rb_define_class_under( mSME, "Metric", cSM_Common ); 155 156     rb_define_alloc_func(cSME_Metric, sm_alloc); 157     rb_define_method( cSME_Metric, "initialize", sm_initialize, 1 ); 158     rb_define_method( cSME_Metric, "update", sm_update, 1 ); 159     rb_define_method( cSME_Metric, "count", sm_count, 0 ); 160     rb_define_method( cSME_Metric, "max", sm_max, 0 ); 161     rb_define_method( cSME_Metric, "min", sm_min, 0 ); 162     rb_define_method( cSME_Metric, "mean", sm_mean, 0 ); 163     rb_define_method( cSME_Metric, "rate", sm_rate, 0 ); 164     rb_define_method( cSME_Metric, "sum", sm_sum, 0 ); 165     rb_define_method( cSME_Metric, "stddev", sm_stddev, 0 ); 166 167 } Tuesday, November 24, 2009 17
  • 27. 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 16     end 17 18     extend ::FFI::Library 19     ffi_lib "libsimple_metrics" 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 Tuesday, November 24, 2009 18
  • 28. 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 16     end library function parameters return type 17 18     extend ::FFI::Library 19     ffi_lib "libsimple_metrics" 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 Tuesday, November 24, 2009 18
  • 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
  • 30. 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 Tuesday, November 24, 2009 20
  • 31. 21 extend ::FFI::Library 22 ffi_lib "libsimple_metrics" 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 ); Tuesday, November 24, 2009 21
  • 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
  • 37. LINKS • http://github.com/ffi/ffi • http://wmeissner.blogspot.com/ • http://groups.google.com/group/ruby-ffi Tuesday, November 24, 2009 27