Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Tour Through a New Ruby Implementation

Chris Seaton
January 01, 2015

A Tour Through a New Ruby Implementation

At FOSDEM. 2015.

Chris Seaton

January 01, 2015
Tweet

More Decks by Chris Seaton

Other Decks in Research

Transcript

  1. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   JRuby+Truffle   A  tour  through  a  new  Ruby  implementaHon   Chris  Seaton   @ChrisGSeaton   Oracle  Labs   Benoit  Daloze   @eregontp   JKU  Linz   Ruby  logo  copyright  (c)  2006,  Yukihiro  Matsumoto,  licensed  under  the  terms  of  the  CreaHve  Commons  AYribuHon-­‐ShareAlike  2.5  agreement   Kevin  Menard   @nirvdrum   Oracle  Labs  
  2. Copyright  ©  2015,  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  informaHon  purposes  only,  and  may  not  be  incorporated  into  any   contract.    It  is  not  a  commitment  to  deliver  any  material,  code,  or  funcHonality,  and   should  not  be  relied  upon  in  making  purchasing  decisions.  Oracle  reserves  the  right  to   alter  its  development  plans  and  pracHces  at  any  Hme,  and  the  development,  release,  and   Hming  of  any  features  or  funcHonality  described  in  connecHon  with  any  Oracle  product  or   service  remains  at  the  sole  discreHon  of  Oracle.    Any  views  expressed  in  this  presentaHon   are  my  own  and  do  not  necessarily  reflect  the  views  of  Oracle.   3  
  3. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   5   Prototype  a  new  language   Parser  and  language  work  to  build     syntax  tree  (AST),  AST  Interpreter   Write  a  “real”  VM   In  C/C++,  sHll  using  AST  interpreter,   spend  a  lot  of  Hme  implemenHng     runHme  system,  GC,  …   People  start  using  it   Define  a  bytecode  format  and     write  bytecode  interpreter   People  complain  about  performance   Write  a  JIT  compiler   Improve  the  garbage  collector   Performance  is  sHll  bad   Current situation
  4. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   6   Prototype  a  new  language   Parser  and  language  work  to  build     syntax  tree  (AST),  AST  Interpreter   Write  a  “real”  VM   In  C/C++,  sHll  using  AST  interpreter,   spend  a  lot  of  Hme  implemenHng     runHme  system,  GC,  …   People  start  using  it   Define  a  bytecode  format  and     write  bytecode  interpreter   People  complain  about  performance   Write  a  JIT  compiler   Improve  the  garbage  collector   Performance  is  sHll  bad   Prototype  a  new  language  in  Java   Parser  and  language  work  to  build     syntax  tree  (AST)   Execute  using  AST  interpreter   People  start  using  it   And  it  is  already  fast   Current situation How it should be
  5. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   8   Truffle:  a  framework  for  wriHng  AST   interpreters  for  languages  in  Java   Graal:  a  dynamic  compiler  (JIT)  for  Java,   wriYen  in  Java,  as  a  Java  library  
  6. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   9   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.  
  7. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   10   codon.com/compilers-­‐for-­‐free   PresentaHon,  by  Tom  Stuart  ,  licensed  under  a  CreaHve  Commons  AYribuHon  ShareAlike  3.0    
  8. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   13   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 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.      
  9. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   14   John  Tenniel  illustraHons  public  domain  in  the  UK  and  US      
  10. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   15   John  Tenniel  illustraHons  public  domain  in  the  UK  and  US      
  11. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   16       t1 = Fixnum(a) + Fixnum(b) if t1.overflowed? t1 = Bignum(a) + Bignum(b) t2 = Bignum(t1) + Bignum(c) else t2 = Fixnum(t1) + Fixnum(c) if t2.overflowed? t2 = Bignum(t1) + Bignum(c) end end
  12. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   17       t1 = Fixnum(a) + Fixnum(b) deoptimize! if t1.overflowed? t2 = Fixnum(t1) + Fixnum(c) deoptimize! if t2.overflowed?
  13. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   19   Guest  Language   Graal  VM   Java  IR,  machine  code  cache,   invalidaHon  and  deopHmisaHon,   opHmisaHon  phases,  replacements,   etc…  etc…  
  14. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   20   Guest  Language   Graal  VM   Truffle   AST  interpreter  
  15. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   21   A  tour  through  Ruby,  Truffle  and  Graal  
  16. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   22   SpecializaHons   class Array def [](index=Fixnum()) # return element at index end def [](index=Fixnum(), num=Fixnum()) # return num elements starting at index end def [](range=Range()) # return elements from range.start to range.end end def [](index) # coerce index and dispatch end B.  Shirai,  Rubinius  3.0,  Part  5,  The  Language,  hYp://rubini.us/2014/11/14/rubinius-­‐3-­‐0-­‐part-­‐5-­‐the-­‐language/  
  17. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   23   SpecializaHons   def  clamp(num,  min,  max)      [min,  num,  max].sort[1]   end   chunky_png  and  psd.rb,  Willem  van  Bergen,  Ryan  LeFevre,  Kelly  SuYon,  Layer  Vault,  Floorplanner  et  al  
  18. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   25   Digging  through  ObjectSpace  and  deopHmizaHon   l  DeopHmize   l  Get  a  consistent  view  of  memory:  safepoints   l  Find  all  reachable  objects   l  Iterate  through  them  
  19. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   26   Digging  through  ObjectSpace  and  deopHmizaHon   public Map<Long, RubyBasicObject> collectLiveObjects() { liveObjects = new HashMap<>(); visitor = new ObjectGraphVisitor() { @Override public boolean visit(RubyBasicObject object) { return liveObjects.put(object.getObjectID(), object) == null; } }; context.getSafepointManager().pauseAllThreadsAndExecute(new Consumer<RubyThread>() { @Override public void accept(RubyThread currentThread) { synchronized (liveObjects) { visitor.visit(currentThread); context.getCoreLibrary().getGlobalVariablesObject().visitObjectGraph(visitor); context.getCoreLibrary().getMainObject().visitObjectGraph(visitor); context.getCoreLibrary().getObjectClass().visitObjectGraph(visitor); visitCallStack(visitor); } } }); return Collections.unmodifiableMap(liveObjects); }
  20. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   27   Does  it  really  implement  Ruby?  
  21. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   93%   RubySpec  language  specs   Brian  Shirai  et  al   28  
  22. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   24%   RubySpec  core  library  specs   Brian  Shirai  et  al   29  
  23. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   30   Fixnum  to  Bignum  promoHon   #eval   #binding   Proc#binding   #send   ObjectSpace   set_trace_func   Thread#raise   Float   Closures   C  extensions   Threads   Concurrency   Frame-­‐local  variables   Encodings   Debugging   Method  invalidaHon   Constant  invalidaHon   Regexp  
  24. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   31   Fixnum  to  Bignum  promoHon   #eval   #binding   Proc#binding   #send   ObjectSpace   set_trace_func   Thread#raise   Float   Closures   C  extensions   Threads   Concurrency   Frame-­‐local  variables   Encodings   Debugging   Method  invalidaHon   Constant  invalidaHon   Regexp  
  25. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   33   Speedup  RelaHve  to  MRI  2.1.2   60   40   20   Mandelbrot  
  26. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   35   chunky_png  and  psd.rb     Willem  van  Bergen,  Ryan  LeFevre,  Kelly  SuYon,  Layer  Vault,  Floorplanner  et  al   Ruby  logo  copyright  (c)  2006,  Yukihiro  Matsumoto,  licensed  under  the  terms  of  the  CreaHve  Commons  AYribuHon-­‐ShareAlike  2.5  agreement  
  27. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   39   How  will  we  solve  startup  Hme,  memory   footprint  and  the  JVM  dependency?  
  28. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   40   Ahead-of-Time Compilation Static Analysis Substrate  VM   Java  ApplicaHon   JDK   Reachable  methods,     fields,  and  classes   IniHal  Heap   Machine  Code   OS   All  Java  classes  from     applicaHon,  JDK,     and  Substrate  VM   ApplicaHon  running     without  compilaHon     or  class  loading  
  29. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   41   13 353 688 14 0 200 400 600 800 MRI JRuby Truffle on JVM Truffle on SVM [msec] Execution Time 5 35 53 9 0 10 20 30 40 50 60 MRI JRuby Truffle on JVM Truffle on SVM [MByte] Memory Footprint
  30. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   42   How  do  we  implement  C  extensions?  
  31. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   45   10.6 4.2 2.5 32.0 14.8 0.0 5.0 10.0 15.0 20.0 25.0 30.0 35.0 MRI W ith C Extension Rubinius W ith C Extension JRuby W ith C Extension JRuby+Truffle W ith C Extension JRuby+Truffle W ith C Extension (No Inline) Mean Speedup Relative to MRI Without C Extension (s/s)
  32. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   46   How  does  it  build  on  other  projects?  
  33. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   47   JRuby  logo  copyright  (c)  Tony  Price  2011,  licensed  under  the  terms  of  the  CreaHve  Commons  AYribuHon-­‐NoDerivs  3.0  Unported  (CC  BY-­‐ND  3.0)   Ruby  logo  copyright  (c)  2006,  Yukihiro  Matsumoto,  licensed  under  the  terms  of  the  CreaHve  Commons  AYribuHon-­‐ShareAlike  2.5  agreement   Rubinius  logo  licensed  under  the  terms  of  the  CreaHve  Commons  AYribuHon-­‐NoDerivs  3.0  Unported   •  Lexer,  parser   •  Strings,  regexps,  IO   •  Command  line   •  Build  and  distribuHon   infrastructure   •  Cannot  re-­‐use  more  of   the  core  library  due  to   very  different   approaches   •  The  language  design   •  The  parts  of  the   standard  library  wriYen   in  Ruby   •  Considering  trying  to   use  some  of  the  C  code   •  Parts  of  the  core  library   •  Parts  of  the  standard   library   •  RubySpec   •  We  have  our  own   implementaHons  of  the   Rubinius  primiHves  
  34. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   49   What  other  big  ideas  do  we  have?  
  35. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   M.  Grimmer,  C.  Seaton,  T.  Würthinger,  H.  Mössenböck.  Dynamically  Composing  Languages  in  a   Modular  Way:  SupporEng  C  Extensions  for  Dynamic  Languages.  In  Proceedings  of  the  14th   InternaHonal  Conference  on  Modularity,  2015  (to  appear)   A.  Wöß,  C.  Wirth,  D.  BoneYa,  C.  Seaton,  C.  Humer,  and  H.  Mössenböck.  An  object  storage  model  for   the  Truffle  language  implementaEon  framework.  In  Proceedings  of  the  InternaHonal  Conference  on   Principles  and  PracHces  of  Programming  on  the  Java  Pla|orm  (PPPJ),  2014.   C.  Seaton,  M.  L.  Van  De  Vanter,  and  M.  Haupt.  Debugging  at  full  speed.  In  Proceedings  of  the  8th   Workshop  on  Dynamic  Languages  and  ApplicaHons  (DYLA),  2014   T.  Würthinger,  C.  Wimmer,  A.  Wöß,  L.  Stadler,  G.  Duboscq,  C.  Humer,  G.  Richards,  D.  Simon,  M.   Wolczko.  One  VM  to  Rule  Them  All.  In  Proceedings  of  Onward!.  2013.   T.  Würthinger,  A.  Wöß,  L.  Stadler,  G.  Duboscq,  D.  Simon,  C.  Wimmer.  Self-­‐OpEmizing  AST   Interpreters.  In  Proceedings  of  the  Dynamic  Languages  Symposium  (DLS),  2012   51  
  36. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   #jruby     github.com/jruby/jruby     chrisseaton.com/rubytruffle     @chrisgseaton          @eregontp              @nirvdrum   52  
  37. Copyright  ©  2015,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   Safe  Harbor  Statement   The  preceding  is  intended  to  provide  some  insight  into  a  line  of  research  in  Oracle  Labs.  It   is  intended  for  informaHon  purposes  only,  and  may  not  be  incorporated  into  any   contract.    It  is  not  a  commitment  to  deliver  any  material,  code,  or  funcHonality,  and   should  not  be  relied  upon  in  making  purchasing  decisions.  Oracle  reserves  the  right  to   alter  its  development  plans  and  pracHces  at  any  Hme,  and  the  development,  release,  and   Hming  of  any  features  or  funcHonality  described  in  connecHon  with  any  Oracle  product  or   service  remains  at  the  sole  discreHon  of  Oracle.    Any  views  expressed  in  this  presentaHon   are  my  own  and  do  not  necessarily  reflect  the  views  of  Oracle.   53