Slide 1

Slide 1 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Op?mizing  Ruby  with   TruffleRuby   The  Ruby  Logo  is  Copyright  (c)  2006,  Yukihiro  Matsumoto.  It  is  licensed  under  the  terms  of  the  Crea?ve  Commons  APribu?on-­‐ShareAlike  2.5  agreement.   Slides  from  Chris  Seaton   Oracle  Labs  @ChrisGSeaton     Benoit  Daloze   @eregontp     15  March  2017   Linz  Ruby  User  Group  

Slide 2

Slide 2 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Safe  Harbor  Statement   The  following  is  intended  to  provide  some  insight  into  a  line  of  research  in  Oracle  Labs.  It   is  intended  for  informa?on  purposes  only,  and  may  not  be  incorporated  into  any   contract.    It  is  not  a  commitment  to  deliver  any  material,  code,  or  func?onality,  and   should  not  be  relied  upon  in  making  purchasing  decisions.  Oracle  reserves  the  right  to   alter  its  development  plans  and  prac?ces  at  any  ?me,  and  the  development,  release,  and   ?ming  of  any  features  or  func?onality  described  in  connec?on  with  any  Oracle  product  or   service  remains  at  the  sole  discre?on  of  Oracle.    Any  views  expressed  in  this  presenta?on   are  my  own  and  do  not  necessarily  reflect  the  views  of  Oracle.   2  

Slide 3

Slide 3 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Who  am  I  ?   •  Benoit  Daloze   •  PhD  student  at  Johannes  Kepler  University   •  Research  with  TruffleRuby  on  concurrency   •  MRI  and  JRuby  commiPer   •  Maintainer  of  the  Ruby  Spec  Suite  (ruby/spec)   3   TwiPer:  @eregontp   GitHub:  @eregon  

Slide 4

Slide 4 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Agenda   •  Introduc?on   •  Truffle  and  Ruby  implementa?ons   •  Why  is  Ruby  apparently  so  hard  to  run  fast?   •  Par?al  Evalua?on   •  Performance  &  Compa?bility   4  

Slide 5

Slide 5 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Computer  Language  Benchmarks  Game  

Slide 6

Slide 6 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Goal: Computer  Language  Benchmarks  Game  

Slide 7

Slide 7 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   What  is  Truffle?  

Slide 8

Slide 8 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   How  do  Ruby  interpreters  work?  

Slide 9

Slide 9 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   x  +  y  *  z  

Slide 10

Slide 10 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   x  +  y  *  z   +   x   *   y   z  

Slide 11

Slide 11 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   x  +  y  *  z   +   x   *   y   z   load_local  x   load_local  y   load_local  z   call  :*   call  :+  

Slide 12

Slide 12 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Just-­‐In-­‐Time  Compilers  

Slide 13

Slide 13 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   x  +  y  *  z   +   x   *   y   z   load_local  x   load_local  y   load_local  z   call  :*   call  :+  

Slide 14

Slide 14 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   x  +  y  *  z   +   x   *   y   z   load_local  x   load_local  y   load_local  z   call  :*   call  :+   pushq  %rbp   movq    %rsp,  %rbp   movq    %rdi,  -­‐8(%rbp)   movq    %rsi,  -­‐16(%rbp)   movq    %rdx,  -­‐24(%rbp)   movq    -­‐16(%rbp),  %rax   movl    %eax,  %edx   movq    -­‐24(%rbp),  %rax   imull  %edx,  %eax   movq    -­‐8(%rbp),  %rdx   addl    %edx,  %eax   popq    %rbp   ret  

Slide 15

Slide 15 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   The  JRuby  logo  is  copyright  (c)  Tony  Price  2011,  licensed  under  the  terms  of  Crea?ve  Commons  APribu?on-­‐NoDerivs  3.0  Unported  (CC  BY-­‐ND  3.0)   JRuby   JITs  by  emihng  JVM  bytecode   VM  in  Java   Core  library  mostly  in  Java  

Slide 16

Slide 16 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   The  Rubinius  logo  is  copyright  2011  Shane  Becker,  licensed  under  the  terms  of  Crea?ve  Commons  APribu?on-­‐NoDeriva?ves  4.0  Interna?onal  —  CC  BY-­‐ND  4.0   Rubinius   JITs  by  emihng  LLVM  bitcode   VM  in  C++   Core  library  mostly  in  Ruby  

Slide 17

Slide 17 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   The  Rubinius  logo  is  copyright  2011  Shane  Becker,  licensed  under  the  terms  of  Crea?ve  Commons  APribu?on-­‐NoDeriva?ves  4.0  Interna?onal  —  CC  BY-­‐ND  4.0   Rubinius   JITs  by  emihng  LLVM  bitcode   VM  in  C++   Core  library  mostly  in  Ruby  

Slide 18

Slide 18 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JVM   18  

Slide 19

Slide 19 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JVM   19  

Slide 20

Slide 20 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JVM   JIT   20  

Slide 21

Slide 21 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JVM   JIT   21  

Slide 22

Slide 22 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JVM   Graal   22  

Slide 23

Slide 23 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   JVM   Graal   Truffle   23  

Slide 24

Slide 24 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Truffle   Graal  VM   JVM   24  

Slide 25

Slide 25 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   U U U U U I I I G G I I I G G Node Rewriting for Profiling Feedback AST Interpreter Rewritten Nodes AST Interpreter Uninitialized Nodes Compilation using Partial Evaluation Compiled Code Node Transitions S U I D G Uninitialized Integer Generic Double String T.  Würthinger,  C.  Wimmer,  A.  Wöß,  L.  Stadler,  G.  Duboscq,  C.  Humer,  G.  Richards,  D.  Simon,   and  M.  Wolczko.  One  VM  to  rule  them  all.  In  Proceedings  of  Onward!,  2013.   Compiling  the  AST  to  machine  code  

Slide 26

Slide 26 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   T.  Würthinger,  C.  Wimmer,  A.  Wöß,  L.  Stadler,  G.  Duboscq,  C.  Humer,  G.  Richards,  D.  Simon,   and  M.  Wolczko.  One  VM  to  rule  them  all.  In  Proceedings  of  Onward!,  2013.   I I I G G I I I G G Deoptimization to AST Interpreter D I D G G D I D G G Node Rewriting to Update Profiling Feedback Recompilation using Partial Evaluation Deop?miza?on  and  recompila?on  

Slide 27

Slide 27 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   27  

Slide 28

Slide 28 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   28  

Slide 29

Slide 29 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   29  

Slide 30

Slide 30 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   30  

Slide 31

Slide 31 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Why  is  Ruby  apparently  so  hard   to  run  fast?  

Slide 32

Slide 32 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   32  

Slide 33

Slide 33 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   33  

Slide 34

Slide 34 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 35

Slide 35 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   35  

Slide 36

Slide 36 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   36  

Slide 37

Slide 37 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   require  'benchmark/ips'       Benchmark.ips  do  |x|      x.report(  "direct",            "14  +  2"                    )      x.report(  "send-­‐symbol",  "14.send(:+,  2)"    )      x.report(  "send-­‐string",  "14.send('+',  2)"  )      x.report(  "eval",                "eval('14  +  2')"    )      x.compare!   end      

Slide 38

Slide 38 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |                              direct:  37299872.6  i/s                    send-­‐symbol:  13060179.1  i/s  -­‐  2.86x  slower                    send-­‐string:    4974575.3  i/s  -­‐  7.50x  slower                                  eval:      171835.9  i/s  -­‐  217.07x  slower  

Slide 39

Slide 39 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Throwing  away  metaprogramming   •  Crystal  –  throws  away  metaprogramming  en?rely  to  make  a  faster  Ruby   •  Rubinius  –  doesn’t  support  set_trace_func   •  JRuby  –  doesn’t  support  set_trace_func  or  ObjectSpace   •  RubyMo?on  –  doesn’t  support  eval,  Binding  

Slide 40

Slide 40 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   How  does  Truffle  +  Graal  solve  this?  

Slide 41

Slide 41 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   ParCal  EvaluaCon   And  DeopCmizaCon  

Slide 42

Slide 42 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   42   def min(a, b) [a, b].sort[0] end puts min(2, 8)

Slide 43

Slide 43 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   43   def min(a, b) [a, b].sort[0] end puts [2, 8].sort[0]

Slide 44

Slide 44 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   44   t0 = 2 <=> 8 t1 = t0 < 0 ? 2 : 8 t2 = t0 > 0 ? 8 : 2 t3 = [t1, t2] puts t3[0]

Slide 45

Slide 45 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   45   t0 = 2 <=> 8 t1 = t0 < 0 ? 2 : 8 t2 = t0 > 0 ? 8 : 2 t3 = [t1, t2] puts t1

Slide 46

Slide 46 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   46   t0 = -1 t1 = t0 < 0 ? 2 : 8 puts t1

Slide 47

Slide 47 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   47   t0 = -1 t1 = -1 < 0 ? 2 : 8 puts t1

Slide 48

Slide 48 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   48   t1 = true ? 2 : 8 puts t1

Slide 49

Slide 49 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   49   t1 = 2 puts t1

Slide 50

Slide 50 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   50   t1 = 2 puts 2

Slide 51

Slide 51 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   require  'benchmark/ips'       Benchmark.ips  do  |x|      x.report(  "direct",            "14  +  2"                    )      x.report(  "send-­‐symbol",  "14.send(:+,  2)"    )      x.report(  "send-­‐string",  "14.send('+',  2)"  )      x.report(  "eval",                "eval('14  +  2')"    )      x.compare!   end      

Slide 52

Slide 52 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |                              direct:  37299872.6  i/s                    send-­‐symbol:  13060179.1  i/s  -­‐  2.86x  slower                    send-­‐string:    4974575.3  i/s  -­‐  7.50x  slower                                  eval:      171835.9  i/s  -­‐  217.07x  slower  

Slide 53

Slide 53 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |                              direct:  73099792.5  i/s                      send-­‐symbol:  73458837.7  i/s  –  difference  within  err                    send-­‐string:  66882023.8  i/s  -­‐  difference  within  err                                  eval:  67024838.3  i/s  -­‐  difference  within  err  

Slide 54

Slide 54 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   An  extreme  example  

Slide 55

Slide 55 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 56

Slide 56 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 57

Slide 57 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 58

Slide 58 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 59

Slide 59 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 60

Slide 60 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 61

Slide 61 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   =  22  !  

Slide 62

Slide 62 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 63

Slide 63 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 64

Slide 64 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 65

Slide 65 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 66

Slide 66 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 67

Slide 67 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Performance  

Slide 68

Slide 68 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Speedup  rela?ve  to  MRI  

Slide 69

Slide 69 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |  

Slide 70

Slide 70 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   ● ● ● ● MRI 2.3 JRuby 9.0.4 Node.js JRuby+Truffle Java 1.8.0u66 1 5 10 25 50 75 12  Benchmarks  using  Objects,  Closures  and  Arrays        from  hPps://github.com/smarr/are-­‐we-­‐fast-­‐yet                TruffleRuby  

Slide 71

Slide 71 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   71   Optcarrot:  NES  emulator  benchmark  

Slide 72

Slide 72 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Optcarrot:  NES  emulator  benchmark  –  9x  faster  than  MRI   72  

Slide 73

Slide 73 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   73   Demo  Time  !  

Slide 74

Slide 74 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   CompaCbility  

Slide 75

Slide 75 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Language  specs   96%   Core  library  specs   100%   92%   C-­‐extension  API  specs   Ruby  specs   75  

Slide 76

Slide 76 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   But  does  it  run   “Rails”,  “Ruby  on  Rails”,  and  the  Rails  logo  are  registered  trademarks  of  David  Heinemeier  Hansson   ?  

Slide 77

Slide 77 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   77  

Slide 78

Slide 78 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Rails  tests   Ac?ve  Model   Ac?ve  Support   Ac?ve  Record   Ac?on  View   Ac?on  Pack   Ac?on  Mailer   Rail?es   Sprockets-­‐Rails   Ac?ve  Job   Spring   100%   100%   98%   Basic  func0onality  works   37%   78  

Slide 79

Slide 79 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   So  then  why  can’t  we  run  real  applica?ons  yet?   •  C  extensions  are  s?ll  a  work  in  progress   – Almost  no  database  drivers   – No  openssl   – No  nokogiri   – Prevents  us  running  almost  everything  unfortunately   •  The  specs  don’t  have  perfect  coverage   79  

Slide 80

Slide 80 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Ongoing  Research   •  Running  C  extension  with  Sulong  (Truffle+LLVM)   •  ObjectSpace:  stop  the  world  and  walk  the  frames   •  Represen?ng  String  as  Ropes   •  Efficient  parallelism:   •  Thread-­‐Safe  Objects  for  Dynamic  Languages   •  Thread-­‐Safe  and  Scalable  Ruby  collec?ons  

Slide 81

Slide 81 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Conclusions   Simple  VM  ideas  can  solve  the  major  problems  of   making  Ruby  fast  

Slide 82

Slide 82 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Conclusions   Simple  VM  ideas  can  solve  the  major  problems  of   making  Ruby  fast     We  don’t  need  to  tell  Ruby  programmers  to  avoid   language  features  to  get  performance  

Slide 83

Slide 83 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   Where  to  get  more  info  

Slide 84

Slide 84 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |       hPps://github.com/graalvm/truffleruby     (or  just  search  for  “truffleruby”)  

Slide 85

Slide 85 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   85   Search  for  ’graal  otn’   www.oracle.com/technetwork/oracle-­‐labs/program-­‐languages  

Slide 86

Slide 86 text

Copyright  ©  2016,  Oracle  and/or  its  affiliates.  All  rights  reserved.    |   86   Acknowledgements   Oracle   Danilo  Ansaloni   Stefan  Anzinger   Cosmin  Basca   Daniele  BonePa   MaPhias  Brantner   Petr  Chalupa   Jürgen  Christ   Laurent  Daynès   Gilles  Duboscq   Mar?n  Entlicher   Brandon  Fish   Bas?an  Hossbach   Chris?an  Humer   Mick  Jordan   Vojin  Jovanovic   Peter  Kessler   David  Leopoldseder   Duncan  MacGregor   Kevin  Menard   Jakub  Podlešák   Aleksandar  Prokopec   Tom  Rodriguez   Oracle  (con0nued)   Roland  Schatz   Chris  Seaton   Doug  Simon   Štěpán  Šindelář   Zbyněk  Šlajchrt   Lukas  Stadler   Codrut  Stancu   Jan  Štola   Jaroslav  Tulach   Michael  Van  De  Vanter   Adam  Welc   Chris?an  Wimmer   Chris?an  Wirth   Paul  Wögerer   Mario  Wolczko   Andreas  Wöß   Thomas  Würthinger   JKU  Linz   Prof.  Hanspeter  Mössenböck   Benoit  Daloze   Josef  Eisl   Thomas  Feich?nger   MaPhias  Grimmer   Chris?an  Häubl   Josef  Haider   Chris?an  Huber   Stefan  Marr   Manuel  Rigger   Stefan  Rumzucker   Bernhard  Urban     University  of  Edinburgh   Christophe  Dubach   Juan  José  Fumero  Alfonso   Ranjeet  Singh   Toomas  Remmelg     LaBRI   Floréal  Morandat   University  of  California,  Irvine   Prof.  Michael  Franz   Gulfem  Savrun  Yeniceri   Wei  Zhang     Purdue  University   Prof.  Jan  Vitek   Tomas  Kalibera   Petr  Maj   Lei  Zhao     T.  U.  Dortmund   Prof.  Peter  Marwedel   Helena  KoPhaus   Ingo  Korb     University  of  California,  Davis   Prof.  Duncan  Temple  Lang   Nicholas  Ulle     University  of  Lugano,  Switzerland   Prof.  Walter  Binder   Sun  Haiyang   Yudi  Zheng   Oracle  Interns   Brian  Belleville     Miguel  Garcia   Shams  Imam   Alexey  Karyakin   Stephen  Kell   Andreas  Kun…   Volker  Lan?ng   Gero  Leinemann   Julian  LePner   Joe  Nash   David  Piorkowski   Gregor  Richards   Robert  Seilbeck   Rifat  Shariyar     Alumni   Erik  Eckstein   Michael  Haupt   Christos  Kotselidis   Hyunjin  Lee   David  Leibs   Chris  Thalinger   Till  Westmann