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

Deoptimizing Ruby

Deoptimizing Ruby

Chris Seaton

November 17, 2014
Tweet

More Decks by Chris Seaton

Other Decks in Technology

Transcript

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

       |   Deop@mizing  Ruby   JRuby+Truffle  and  the  an@dote  to  JITs   Chris  Seaton   @ChrisGSeaton     Oracle  Labs   John  Tenniel  illustra@ons  public  domain  in  the  UK  and  US  
  2. Copyright  ©  2014,  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.   3  
  3. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   chrisseaton.com/rubytruffle/deop@mizing   4  
  4. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   5   A  new  open  source  implementa@on  of  Ruby   by  Oracle  Labs  with  a  JIT  using  next-­‐gen   JVM  technology  and  par6al  evalua6on,  now   part  of  JRuby   JRuby+Truffle  
  5. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   6   codon.com/compilers-­‐for-­‐free   Presenta@on,  by  Tom  Stuart  ,  licensed  under  a  Crea@ve  Commons  A\ribu@on  ShareAlike  3.0    
  6. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   Why  is  Ruby  hard  to  op@mize?   7  
  7. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   Fixnum  to  Bignum  promo@on   Monkey  patching  methods   #binding   ObjectSpace   set_trace_func   Thread#raise   8  
  8. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   9   Deop@miza@on   elegantly  solves  all  these  problems  
  9. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   10   Slow  interpreter   Fast  JITed  code  
  10. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   11   Slow  interpreter   Fast  JITed  code  
  11. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   13   John  Tenniel  illustra@ons  public  domain  in  the  UK  and  US  
  12. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   14   John  Tenniel  illustra@ons  public  domain  in  the  UK  and  US  
  13. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   15   John  Tenniel  illustra@ons  public  domain  in  the  UK  and  US  
  14. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   16   John  Tenniel  illustra@ons  public  domain  in  the  UK  and  US   Ruby   Door  to  utopia   of  high   performance  
  15. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   17   John  Tenniel  illustra@ons  public  domain  in  the  UK  and  US   Just-­‐in-­‐@me  compiler   Leh  something   behind  when  we   compiled  
  16. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   18   John  Tenniel  illustra@ons  public  domain  in  the  UK  and  US   Deop@miza@on   reverses  the   effects  of  the   JIT  
  17. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   What  does  deop@miza@on  do  for  Ruby?   19  
  18. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   21   a + b + c We’ll  assume  we  already   know  these  are  Fixnums  
  19. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

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

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

       |   24   t1 = Fixnum(a) + Fixnum(b) if t1.overflowed? t1 = Bignum(a) + Bignum(b) t2 = Bignum(t1) + Bignum(c) else t2 = Fixnum(t1) + Fixnum(c) deoptimize! if t2.overflowed? end
  22. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   27   lookup my_method in my_object call it with (x, y)
  23. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   28   if my_object.changed? lookup my_method in my_object call it with (x, y) else use cached my_method call it with (x, y) end
  24. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   29   if my_object.changed? deoptimize! else use cached my_method call it with (x, y) end
  25. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   30   use cached my_method call it with (x, y)
  26. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   33   …   a:   14   Array   0:   1:   8.2   3.4   b:   …  
  27. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   34   …   a:  14   b[0]:  8.2   …   b[1]:  3.4  
  28. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   35   14   Array   0:   1:   8.2   3.4   …   …   a:  14   b[0]:  8.2   b[1]:  3.4  
  29. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   38   1.     Recreate  the  interpreter  stack  frame   2.     Jump  from  the  JITed  code  into  the  interpreter   3.     Allow  us  to  force  threads  to  do  this  
  30. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   40   loop do a = 14 b = 2 a + b deoptimize! if should_deoptimize? end
  31. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   41   loop do a = 14 b = 2 a + b read the safepoint page end
  32. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   86%   RubySpec  language  specs   rubyspec.org,  Brian  Shirai  et  al   43  
  33. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   44   Fixnum  to  Bignum  promo@on   #eval   #binding   Proc#binding   #send   ObjectSpace   set_trace_func   Thread#raise   Float   Closures   C  extensions   Threads   Concurrency   Frame-­‐local  variables   Encodings   Debugging   Method  invalida@on   Constant  invalida@on   Regexp  
  34. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   45   Fixnum  to  Bignum  promo@on   #eval   #binding   Proc#binding   #send   ObjectSpace   set_trace_func   Thread#raise   Float   Closures   C  extensions   Threads   Concurrency   Frame-­‐local  variables   Encodings   Debugging   Method  invalida@on   Constant  invalida@on   Regexp  
  35. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   46   No,  we  can’t  run  Rails  yet     but  we’re  working  towards  it  
  36. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   48   chunky_png  and  psd.rb     Willem  van  Bergen,  Ryan  LeFevre,  Kelly  Su\on,  Layer  Vault,  Floorplanner  et  al   Ruby  logo  copyright  (c)  2006,  Yukihiro  Matsumoto,  licensed  under  the  terms  of  the  Crea@ve  Commons  A\ribu@on-­‐ShareAlike  2.5  agreement  
  37. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   Building  on  other  projects   52   JRuby  logo  copyright  (c)  Tony  Price  2011,  licensed  under  the  terms  of  the  Crea@ve  Commons  A\ribu@on-­‐NoDerivs  3.0  Unported  (CC  BY-­‐ND  3.0)   Ruby  logo  copyright  (c)  2006,  Yukihiro  Matsumoto,  licensed  under  the  terms  of  the  Crea@ve  Commons  A\ribu@on-­‐ShareAlike  2.5  agreement   Rubinius  logo  licensed  under  the  terms  of  the  Crea@ve  Commons  A\ribu@on-­‐NoDerivs  3.0  Unported  
  38. Copyright  ©  2014,  Oracle  and/or  its  affiliates.  All  rights  reserved.

       |   @ChrisGSeaton     chrisseaton.com/rubytruffle/deop@mizing   54  
  39. Copyright  ©  2014,  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  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.   55