SlideShare a Scribd company logo
1 of 26
Download to read offline
Applying Compiler
                                Technology to Ruby
                                      Evan Phoenix
                                       Sept 8, 2009

Wednesday, September 16, 2009
What makes Ruby great
                                 can make Ruby slow.


Wednesday, September 16, 2009
‣ Highly Dynamic




Wednesday, September 16, 2009
‣ Highly Dynamic
                                • Very high level operations
                                • New code can be introduced at anytime
                                • Dynamic typing
                                • Exclusively late bound method calls
                                • Easier to implement as an interpreter




Wednesday, September 16, 2009
Haven’t other languages
                                had these same features/
                                      weaknesses?


Wednesday, September 16, 2009
‣Prior Work




Wednesday, September 16, 2009
‣Prior Work
                                • Smalltalk
                                 • 1980-1994: Extensive work to make it fast
                                • Self
                                 • 1992-1996: A primary research vehicle for making dynamic
                                    languages fast
                                • Java / Hotspot
                                 • 1996-present: A battle hardened engine for (limited) dynamic
                                    dispatch




Wednesday, September 16, 2009
‣What Can We Learn From Them?




Wednesday, September 16, 2009
‣What Can We Learn From Them?
                                • Complied code is faster than interpreted code
                                • It’s very hard (almost impossible) to figure things out staticly
                                • The type profile of a program is stable over time
                                • Therefore:
                                 • Learn what a program does and optimize based on that
                                 • This is called Type Feedback




Wednesday, September 16, 2009
‣Code Generation (JIT)
                                • Eliminating overhead of interpreter instantly increases
                                  performance a fixed percentage
                                • Naive code generation results in small improvement over
                                  interpreter
                                 • Method calling continues to dominate time
                                • Need a way to generate better code
                                 • Combine with program type information!




Wednesday, September 16, 2009
‣Type Profile
                                • As the program executes, it’s possible to see how one method
                                    calls another methods
                                •   The relationship of one method and all the methods it calls is the
                                    type profile of the method
                                •   Just because you CAN use dynamic dispatch, doesn’t mean you
                                    always do.
                                •   It’s common that a call site always calls the same method every
                                    time it’s run




Wednesday, September 16, 2009
1: 25245
                                          2: 275
                                  2
                                          3: 86
                                 1%
                                          4: 50
                                          5: 35
                                          6: 6
                                          7: 10
                                          8: 5
                                          9: 5
                                          10: 2
                                          10+: 34


                                1 class
                                 98%

          Call sites running
            Array specs
Wednesday, September 16, 2009
‣Type Profiling (Cont.)
                                • 98% of all method calls are to the same method
                                  every time
                                • In other words, 98% of all method calls are statically
                                  bound




Wednesday, September 16, 2009
‣Type Feedback
                                • Optimize a semi-static relationship to generate faster code
                                 • Semi-static relationships are found by profiling all call sites
                                • Allow JIT to make vastly better decisions
                                • Most common optimization: Method Inlining




Wednesday, September 16, 2009
‣Method Inlining
                                • Rather than emit a call to a target method, copy it’s body at the
                                  call site
                                • Eliminates code to lookup and begin execution of target method
                                • Simplifies (or eliminates) setup for target method
                                • Allows for type propagation, as well as providing a wider horizon
                                  for optimization.
                                 • A wider horizon means better generated code, which means
                                    less work to do per method == faster execution.




Wednesday, September 16, 2009
Implementation



Wednesday, September 16, 2009
‣Code Generation (JIT)
                                • Early experimentation with custom JIT
                                 • Realized we weren’t experts
                                 • Would take years to get good code being generated
                                • Switched to LLVM




Wednesday, September 16, 2009
‣LLVM
                                • Provides an internal AST (LLVM IR) for describing work to
                                  be done
                                 • Text representation of AST allows for easy debugging
                                • Provides ability to compile AST to machine code in
                                  memory
                                • Contains thousands of optimizations
                                 • Competitive with GCC


Wednesday, September 16, 2009
‣Type Profiling
                                • All call sites use a class called InlineCache, one per call site
                                • InlineCache accelerates method dispatch by caching previous
                                  method used
                                • In addition, tracks a fixed number of receiver classes seen when
                                  there is a cache miss
                                • When compiling a method using LLVM, all InlineCaches for a
                                  method can be read
                                 • InlineCaches with good information can be used to accurately
                                    find a method to inline



Wednesday, September 16, 2009
‣When To Compile
                                • It takes time for a method’s type information to settle down
                                • Compiling too early means not having enough type info
                                • Compiling too late means lost performance
                                • Use simple call counters to allow a method to “heat up”
                                 • Each invocation of a method increments counter
                                 • When counter reaches a certain value, method is queued for
                                     compilation.
                                 •   Threshold value is tunable: -Xjit.call_til_compile
                                 •   Still experimenting with good default values


Wednesday, September 16, 2009
‣How to Compile
                                • To impact runtime as little as possible, all JIT compilation happens
                                  in a background OS thread
                                • Methods are queued, and background thread reads queue to find
                                  methods to compile
                                • After compiling, function pointers to JIT generated code are
                                  installed in methods
                                 • All future invocations of method use JIT code




Wednesday, September 16, 2009
‣Benchmarks                                   Seconds

                                         9

                                              8.02

                                       6.75


         def foo()                                   5.30
                                                             5.90

          ary = []                      4.5
          100.times { |i| ary << i }
                                                                       3.60
         end
                                       2.25                                          2.59
                      300,000 times
                                         0
                                              1.8    1.9      rbx     rbx jit   rbx jit +blocks




Wednesday, September 16, 2009
‣Benchmarks                                     Seconds

                                          30


                                                                25.36
                                         22.5


         def foo()
          hsh = {}                        15
          100.times { |i| hsh[i] = 0 }                                  12.54          12.01
         end
                                          7.5
                      100,000 times                    5.26
                                                4.85

                                           0
                                                1.8    1.9       rbx    rbx jit   rbx jit +blocks




Wednesday, September 16, 2009
‣Benchmarks                                  Seconds

                                        7

                                                              6.26

                                      5.25


         def foo()
          hsh = { 47 => true }         3.5   3.64
          100.times { |i| hsh[i] }
         end                                                          2.68          2.66

                                      1.75          2.09
                      100,000 times
                                        0
                                             1.8    1.9       rbx    rbx jit   rbx jit +blocks




Wednesday, September 16, 2009
‣Benchmarks                                           Seconds

                                                8

                                                    7.36                      7.27

                                                6



                                                4
                                tak(18, 9, 0)

                                                2
                                                                  1.89
                                                           1.58                       1.53          1.53


                                                0
                                                    1.8    1.9    jruby       rbx    rbx jit   rbx jit +blocks




Wednesday, September 16, 2009
‣Conclusion
                                • Ruby is a wonderful language because it is organized for humans
                                • By gather and using information about a running program, it’s
                                    possible to make that program much faster without impacting
                                    flexibility
                                •   Thank You!




Wednesday, September 16, 2009

More Related Content

Similar to Ruby World

What is Ruby on Rails?
What is Ruby on Rails?What is Ruby on Rails?
What is Ruby on Rails?Karmen Blake
 
No Really, It's All About You
No Really, It's All About YouNo Really, It's All About You
No Really, It's All About YouChris Cornutt
 
Open Source Tools For Freelancers
Open Source Tools For FreelancersOpen Source Tools For Freelancers
Open Source Tools For FreelancersChristie Koehler
 
Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)Stefan Koopmanschap
 
Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Stefan Koopmanschap
 
PHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An IntroductionPHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An Introductionalexmace
 
Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)Yan Cui
 
How to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphoneHow to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphoneJoel Maher
 
Web assembly overview by Mikhail Sorokovsky
Web assembly overview by Mikhail SorokovskyWeb assembly overview by Mikhail Sorokovsky
Web assembly overview by Mikhail SorokovskyValeriia Maliarenko
 
CNPM: Private NPM for Company / 企業級私有NPM
CNPM: Private NPM for Company / 企業級私有NPMCNPM: Private NPM for Company / 企業級私有NPM
CNPM: Private NPM for Company / 企業級私有NPMFeng Yuan
 
Myphp-busters: symfony framework
Myphp-busters: symfony frameworkMyphp-busters: symfony framework
Myphp-busters: symfony frameworkStefan Koopmanschap
 
Content Management Selection and Strategy
Content Management Selection and StrategyContent Management Selection and Strategy
Content Management Selection and StrategyIvo Jansch
 
Ibuildings Cms Talk
Ibuildings Cms TalkIbuildings Cms Talk
Ibuildings Cms Talkdean1985
 
Putting Compilers to Work
Putting Compilers to WorkPutting Compilers to Work
Putting Compilers to WorkSingleStore
 
The Architect Way - JSCamp.asia 2012
The Architect Way - JSCamp.asia 2012The Architect Way - JSCamp.asia 2012
The Architect Way - JSCamp.asia 2012Jan Jongboom
 
Scaling with Symfony - PHP UK
Scaling with Symfony - PHP UKScaling with Symfony - PHP UK
Scaling with Symfony - PHP UKRicard Clau
 
Xamarin - Victim of Phonegap’s horrible reputation
Xamarin - Victim of Phonegap’s horrible reputationXamarin - Victim of Phonegap’s horrible reputation
Xamarin - Victim of Phonegap’s horrible reputationGabor Wnuk
 

Similar to Ruby World (20)

What is Ruby on Rails?
What is Ruby on Rails?What is Ruby on Rails?
What is Ruby on Rails?
 
No Really, It's All About You
No Really, It's All About YouNo Really, It's All About You
No Really, It's All About You
 
Open Source Tools For Freelancers
Open Source Tools For FreelancersOpen Source Tools For Freelancers
Open Source Tools For Freelancers
 
MPI n OpenMP
MPI n OpenMPMPI n OpenMP
MPI n OpenMP
 
Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)Myphp-busters: symfony framework (php|tek 09)
Myphp-busters: symfony framework (php|tek 09)
 
Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)
 
PHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An IntroductionPHPUnit & Continuous Integration: An Introduction
PHPUnit & Continuous Integration: An Introduction
 
Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)Introduction to Aspect Oriented Programming (DDD South West 4.0)
Introduction to Aspect Oriented Programming (DDD South West 4.0)
 
How to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphoneHow to stuff a 900 pound gorilla into a smartphone
How to stuff a 900 pound gorilla into a smartphone
 
Web assembly overview by Mikhail Sorokovsky
Web assembly overview by Mikhail SorokovskyWeb assembly overview by Mikhail Sorokovsky
Web assembly overview by Mikhail Sorokovsky
 
CNPM: Private NPM for Company / 企業級私有NPM
CNPM: Private NPM for Company / 企業級私有NPMCNPM: Private NPM for Company / 企業級私有NPM
CNPM: Private NPM for Company / 企業級私有NPM
 
Xamarin介紹
Xamarin介紹Xamarin介紹
Xamarin介紹
 
Go fundamentals
Go fundamentalsGo fundamentals
Go fundamentals
 
Myphp-busters: symfony framework
Myphp-busters: symfony frameworkMyphp-busters: symfony framework
Myphp-busters: symfony framework
 
Content Management Selection and Strategy
Content Management Selection and StrategyContent Management Selection and Strategy
Content Management Selection and Strategy
 
Ibuildings Cms Talk
Ibuildings Cms TalkIbuildings Cms Talk
Ibuildings Cms Talk
 
Putting Compilers to Work
Putting Compilers to WorkPutting Compilers to Work
Putting Compilers to Work
 
The Architect Way - JSCamp.asia 2012
The Architect Way - JSCamp.asia 2012The Architect Way - JSCamp.asia 2012
The Architect Way - JSCamp.asia 2012
 
Scaling with Symfony - PHP UK
Scaling with Symfony - PHP UKScaling with Symfony - PHP UK
Scaling with Symfony - PHP UK
 
Xamarin - Victim of Phonegap’s horrible reputation
Xamarin - Victim of Phonegap’s horrible reputationXamarin - Victim of Phonegap’s horrible reputation
Xamarin - Victim of Phonegap’s horrible reputation
 

More from evanphx

Rubinius For You - GoRuCo
Rubinius For You - GoRuCoRubinius For You - GoRuCo
Rubinius For You - GoRuCoevanphx
 
Developing a Language
Developing a LanguageDeveloping a Language
Developing a Languageevanphx
 
Rubinius - What Have You Done For Me Lately?
Rubinius - What Have You Done For Me Lately?Rubinius - What Have You Done For Me Lately?
Rubinius - What Have You Done For Me Lately?evanphx
 
Rubinius - What Have You Done For Me Lately
Rubinius - What Have You Done For Me LatelyRubinius - What Have You Done For Me Lately
Rubinius - What Have You Done For Me Latelyevanphx
 
Staking Your Claim In Open Source
Staking Your Claim In Open SourceStaking Your Claim In Open Source
Staking Your Claim In Open Sourceevanphx
 
Rubinius 1.0 and more!
Rubinius 1.0 and more!Rubinius 1.0 and more!
Rubinius 1.0 and more!evanphx
 
RubyConf 2009
RubyConf 2009RubyConf 2009
RubyConf 2009evanphx
 
Accelerating Ruby with LLVM
Accelerating Ruby with LLVMAccelerating Ruby with LLVM
Accelerating Ruby with LLVMevanphx
 
Rubinius Community - MWRC
Rubinius Community - MWRCRubinius Community - MWRC
Rubinius Community - MWRCevanphx
 
Rubinius - Improving the Rails ecosystem
Rubinius - Improving the Rails ecosystemRubinius - Improving the Rails ecosystem
Rubinius - Improving the Rails ecosystemevanphx
 
Rubinius - A Tool of the Future
Rubinius - A Tool of the FutureRubinius - A Tool of the Future
Rubinius - A Tool of the Futureevanphx
 

More from evanphx (11)

Rubinius For You - GoRuCo
Rubinius For You - GoRuCoRubinius For You - GoRuCo
Rubinius For You - GoRuCo
 
Developing a Language
Developing a LanguageDeveloping a Language
Developing a Language
 
Rubinius - What Have You Done For Me Lately?
Rubinius - What Have You Done For Me Lately?Rubinius - What Have You Done For Me Lately?
Rubinius - What Have You Done For Me Lately?
 
Rubinius - What Have You Done For Me Lately
Rubinius - What Have You Done For Me LatelyRubinius - What Have You Done For Me Lately
Rubinius - What Have You Done For Me Lately
 
Staking Your Claim In Open Source
Staking Your Claim In Open SourceStaking Your Claim In Open Source
Staking Your Claim In Open Source
 
Rubinius 1.0 and more!
Rubinius 1.0 and more!Rubinius 1.0 and more!
Rubinius 1.0 and more!
 
RubyConf 2009
RubyConf 2009RubyConf 2009
RubyConf 2009
 
Accelerating Ruby with LLVM
Accelerating Ruby with LLVMAccelerating Ruby with LLVM
Accelerating Ruby with LLVM
 
Rubinius Community - MWRC
Rubinius Community - MWRCRubinius Community - MWRC
Rubinius Community - MWRC
 
Rubinius - Improving the Rails ecosystem
Rubinius - Improving the Rails ecosystemRubinius - Improving the Rails ecosystem
Rubinius - Improving the Rails ecosystem
 
Rubinius - A Tool of the Future
Rubinius - A Tool of the FutureRubinius - A Tool of the Future
Rubinius - A Tool of the Future
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

Ruby World

  • 1. Applying Compiler Technology to Ruby Evan Phoenix Sept 8, 2009 Wednesday, September 16, 2009
  • 2. What makes Ruby great can make Ruby slow. Wednesday, September 16, 2009
  • 3. ‣ Highly Dynamic Wednesday, September 16, 2009
  • 4. ‣ Highly Dynamic • Very high level operations • New code can be introduced at anytime • Dynamic typing • Exclusively late bound method calls • Easier to implement as an interpreter Wednesday, September 16, 2009
  • 5. Haven’t other languages had these same features/ weaknesses? Wednesday, September 16, 2009
  • 7. ‣Prior Work • Smalltalk • 1980-1994: Extensive work to make it fast • Self • 1992-1996: A primary research vehicle for making dynamic languages fast • Java / Hotspot • 1996-present: A battle hardened engine for (limited) dynamic dispatch Wednesday, September 16, 2009
  • 8. ‣What Can We Learn From Them? Wednesday, September 16, 2009
  • 9. ‣What Can We Learn From Them? • Complied code is faster than interpreted code • It’s very hard (almost impossible) to figure things out staticly • The type profile of a program is stable over time • Therefore: • Learn what a program does and optimize based on that • This is called Type Feedback Wednesday, September 16, 2009
  • 10. ‣Code Generation (JIT) • Eliminating overhead of interpreter instantly increases performance a fixed percentage • Naive code generation results in small improvement over interpreter • Method calling continues to dominate time • Need a way to generate better code • Combine with program type information! Wednesday, September 16, 2009
  • 11. ‣Type Profile • As the program executes, it’s possible to see how one method calls another methods • The relationship of one method and all the methods it calls is the type profile of the method • Just because you CAN use dynamic dispatch, doesn’t mean you always do. • It’s common that a call site always calls the same method every time it’s run Wednesday, September 16, 2009
  • 12. 1: 25245 2: 275 2 3: 86 1% 4: 50 5: 35 6: 6 7: 10 8: 5 9: 5 10: 2 10+: 34 1 class 98% Call sites running Array specs Wednesday, September 16, 2009
  • 13. ‣Type Profiling (Cont.) • 98% of all method calls are to the same method every time • In other words, 98% of all method calls are statically bound Wednesday, September 16, 2009
  • 14. ‣Type Feedback • Optimize a semi-static relationship to generate faster code • Semi-static relationships are found by profiling all call sites • Allow JIT to make vastly better decisions • Most common optimization: Method Inlining Wednesday, September 16, 2009
  • 15. ‣Method Inlining • Rather than emit a call to a target method, copy it’s body at the call site • Eliminates code to lookup and begin execution of target method • Simplifies (or eliminates) setup for target method • Allows for type propagation, as well as providing a wider horizon for optimization. • A wider horizon means better generated code, which means less work to do per method == faster execution. Wednesday, September 16, 2009
  • 17. ‣Code Generation (JIT) • Early experimentation with custom JIT • Realized we weren’t experts • Would take years to get good code being generated • Switched to LLVM Wednesday, September 16, 2009
  • 18. ‣LLVM • Provides an internal AST (LLVM IR) for describing work to be done • Text representation of AST allows for easy debugging • Provides ability to compile AST to machine code in memory • Contains thousands of optimizations • Competitive with GCC Wednesday, September 16, 2009
  • 19. ‣Type Profiling • All call sites use a class called InlineCache, one per call site • InlineCache accelerates method dispatch by caching previous method used • In addition, tracks a fixed number of receiver classes seen when there is a cache miss • When compiling a method using LLVM, all InlineCaches for a method can be read • InlineCaches with good information can be used to accurately find a method to inline Wednesday, September 16, 2009
  • 20. ‣When To Compile • It takes time for a method’s type information to settle down • Compiling too early means not having enough type info • Compiling too late means lost performance • Use simple call counters to allow a method to “heat up” • Each invocation of a method increments counter • When counter reaches a certain value, method is queued for compilation. • Threshold value is tunable: -Xjit.call_til_compile • Still experimenting with good default values Wednesday, September 16, 2009
  • 21. ‣How to Compile • To impact runtime as little as possible, all JIT compilation happens in a background OS thread • Methods are queued, and background thread reads queue to find methods to compile • After compiling, function pointers to JIT generated code are installed in methods • All future invocations of method use JIT code Wednesday, September 16, 2009
  • 22. ‣Benchmarks Seconds 9 8.02 6.75 def foo() 5.30 5.90 ary = [] 4.5 100.times { |i| ary << i } 3.60 end 2.25 2.59 300,000 times 0 1.8 1.9 rbx rbx jit rbx jit +blocks Wednesday, September 16, 2009
  • 23. ‣Benchmarks Seconds 30 25.36 22.5 def foo() hsh = {} 15 100.times { |i| hsh[i] = 0 } 12.54 12.01 end 7.5 100,000 times 5.26 4.85 0 1.8 1.9 rbx rbx jit rbx jit +blocks Wednesday, September 16, 2009
  • 24. ‣Benchmarks Seconds 7 6.26 5.25 def foo() hsh = { 47 => true } 3.5 3.64 100.times { |i| hsh[i] } end 2.68 2.66 1.75 2.09 100,000 times 0 1.8 1.9 rbx rbx jit rbx jit +blocks Wednesday, September 16, 2009
  • 25. ‣Benchmarks Seconds 8 7.36 7.27 6 4 tak(18, 9, 0) 2 1.89 1.58 1.53 1.53 0 1.8 1.9 jruby rbx rbx jit rbx jit +blocks Wednesday, September 16, 2009
  • 26. ‣Conclusion • Ruby is a wonderful language because it is organized for humans • By gather and using information about a running program, it’s possible to make that program much faster without impacting flexibility • Thank You! Wednesday, September 16, 2009