$30 off During Our Annual Pro Sale. View Details »

Tweaking Ruby GC parameters for fun, speed, and profit

Tweaking Ruby GC parameters for fun, speed, and profit

Whether you are building a Robot, controlling a Radar, or creating a Web App, the Ruby Garbage Collector (GC) can help you. The stats exposed by the Garbage Collector since Ruby v2.1 caught my attention and pushed me to dig deeper. Both Ruby 2.1 and 2.2 brought great performance improvements. From a practical point of view, we will discuss how to use the GC to enhance the performance of your software, from configuration parameters to different approaches on how you can change them yourself.

Helio Cola

May 04, 2016
Tweet

More Decks by Helio Cola

Other Decks in Programming

Transcript

  1. RailsConf 2016
    Tweaking Ruby GC
    parameters for fun,
    speed, and profit
    I mean: for fun!
    Helio Cola
    http://hac-rods.me
    @hacrods

    View Slide

  2. RailsConf 2016
    So, why I am here talking about GC?

    View Slide

  3. RailsConf 2016
    hac-rods.me
    Hi, I am Helio Cola!
    • ~15 years developing SW
    • ~10 years doing C/C++ on Solaris
    • 5/6 years since I found happiness
    • ~4 years full time happiness
    • I go by @hacrods on the internet…

    View Slide

  4. RailsConf 2016
    So, let’s get it started and talk about
    Ruby Garbage Collector

    View Slide

  5. RailsConf 2016
    Terms
    • GC: Garbage Collector
    • RGenGC: Restricted Generational GC
    • RIncGC: Restricted Incremental GC
    • CoW: Copy-on-write technique
    • ab: Apache Benchmark Tool

    View Slide

  6. RailsConf 2016
    Agenda
    • Why I am here talking about Ruby GC
    • A couple bits about its history
    • Configuration parameters
    • My approach to measure & tuning
    • Some of my findings
    • QA

    View Slide

  7. RailsConf 2016
    Why I am here… Ruby Garbage Collector

    View Slide

  8. RailsConf 2016
    Why I am here… GC
    • Once upon a time there was a Rails App
    • Why? How? How can I?

    View Slide

  9. RailsConf 2016
    Why I am here… GC
    • I did do a lot research on it…
    • I had a really great time learning & doing all this
    • I wanted to share it
    • And my talk got accepted!!!! :-)

    View Slide

  10. RailsConf 2016
    A bit about the history of Ruby GC

    View Slide

  11. RailsConf 2016
    A brief history about Ruby GC
    • Ruby 1.8: Simple Mark & Sweep
    • Ruby 1.9.3: Lazy Sweep
    • Ruby 2.0: Bitmap Masking (CoW friendly)
    • Ruby 2.1: RGenGC
    • Ruby 2.2: RIncGC and Symbol GC
    Reference: http://tmm1.net/ruby21-rgengc/

    View Slide

  12. RailsConf 2016
    A brief history about Ruby GC
    by
    Aman Gupta
    Reference: http://tmm1.net/ruby21-rgengc/

    View Slide

  13. RailsConf 2016
    Ruby 1.8: Simple Mark & Sweep
    Reference: http://tmm1.net/ruby21-rgengc/

    View Slide

  14. RailsConf 2016
    Ruby 1.9.3: Lazy Sweep
    Reference: http://tmm1.net/ruby21-rgengc/

    View Slide

  15. RailsConf 2016
    Ruby 2.0: Bitmap Masking (CoW)
    Reference: http://tmm1.net/ruby21-rgengc/

    View Slide

  16. RailsConf 2016
    Ruby 2.0.x: 2.0.0-p647

    View Slide

  17. RailsConf 2016
    Ruby 2.1: RGenGC
    Reference: http://tmm1.net/ruby21-rgengc/

    View Slide

  18. RailsConf 2016
    Ruby 2.2: RIncGC / Symbol GC
    • Symbols now gets collected by GC!
    • RIncGC
    https://engineering.heroku.com/blogs/2015-02-04-incremental-gc/

    View Slide

  19. RailsConf 2016
    Ruby 2.2: RIncGC / Symbol GC
    • RIncGC
    • Not a silver bullet
    • Don’t affect throughput or response time
    Reference: https://engineering.heroku.com/blogs/2015-02-04-incremental-gc/

    View Slide

  20. RailsConf 2016
    Ruby 2.1.x & 2.2.x

    View Slide

  21. RailsConf 2016
    Configuration parameters

    View Slide

  22. RailsConf 2016
    GC Parameters: ruby 2.0.x
    • RUBY_GC_MALLOC_LIMIT
    • RUBY_HEAP_MIN_SLOTS
    • RUBY_FREE_MIN
    https://github.com/ruby/ruby/blob/v2_0_0_647/gc.c#L3296

    View Slide

  23. RailsConf 2016
    GC Parameters: Ruby >= 2.1.x
    • RUBY_GC_HEAP_INIT_SLOTS
    • RUBY_GC_HEAP_FREE_SLOTS
    • RUBY_GC_HEAP_GROWTH_FACTOR
    • RUBY_GC_HEAP_GROWTH_MAX_SLOTS
    • RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR
    Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705

    View Slide

  24. RailsConf 2016
    GC Parameters: Ruby >= 2.1.x
    • RUBY_GC_MALLOC_LIMIT
    • RUBY_GC_MALLOC_LIMIT_MAX
    • RUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR
    Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705

    View Slide

  25. RailsConf 2016
    GC Parameters: Ruby >= 2.1.x
    • RUBY_GC_OLDMALLOC_LIMIT
    • RUBY_GC_OLDMALLOC_LIMIT_MAX
    • RUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR
    Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705

    View Slide

  26. RailsConf 2016
    Reference: https://github.com/ruby/ruby/blob/v2_1_7/gc.c#L5705 / https://github.com/ruby/ruby/blob/v2_2_3/gc.c#L6972
    https://github.com/ruby/ruby/blob/v2_3_0/gc.c#L7256
    Ruby v2.1.7
    And also:
    v2.2.x & v2.3.x

    View Slide

  27. RailsConf 2016
    Running it
    $ export RUBY_GC_HEAP_INIT_SLOTS=800000
    $ export RUBY_GC_HEAP_FREE_SLOTS=32768
    $ export RUBY_GC_MALLOC_LIMIT=33554432
    $ export RUBY_GC_MALLOC_LIMIT_MAX=50331648
    $ rails s

    View Slide

  28. RailsConf 2016
    My approach to measure & tuning

    View Slide

  29. RailsConf 2016
    Me: measuring & tuning
    • Find out if your current/default experience is good,
    normal, bad, or really bad.

    View Slide

  30. RailsConf 2016
    Me: measuring & tuning
    • Research mode, reading mode, studying mode,
    read ruby source code, compile it…
    ✴ I have to upgrade the one with 2.0.x
    ✴ I don’t know yet if my app with 2.1.x is in good or bad shape
    • And after a lot of research…

    View Slide

  31. RailsConf 2016
    So, what now?!
    • Let me start by running it all on my laptop
    … and ab it!
    … and… still doesn’t look like my production app!

    View Slide

  32. RailsConf 2016
    So, and now what?!
    • A couple of days go by…
    … testing, measuring, analyzing data, thinking about what I am seeing…
    • Some well known reasons:
    • My laptop isn’t like my AWS servers
    • I don’t have the same amount of data
    • My ab calls aren’t reflecting very well the traffic from my real users
    … so… the app on my laptop won’t be like it is in prod servers!

    View Slide

  33. RailsConf 2016
    So…
    • Sleep on it
    • Go spend a day doing something else
    • Grab a teammate and talk to him
    • Explain what you are doing to someone else
    (or at least try to explain! :-)
    • Plan your next steps

    View Slide

  34. RailsConf 2016
    And: either this or that
    • You may open your eyes to something else
    • Or you establish a laptop baseline
    DEV
    PROD

    View Slide

  35. RailsConf 2016
    Finally: I got this

    View Slide

  36. RailsConf 2016
    Change, Measure & Analyze
    • Do that over & over & over again…
    • Analyze your data, document your numbers in a
    piece of paper or notepad
    • Make one single change (at a time) and re-run your
    tests

    View Slide

  37. RailsConf 2016
    Again: either this or that
    • If you will find something that improves your app
    ̣ Document it nicely and share with your team
    ̣ Make a small talk out of it
    ̣ And you won’t sweat to make it into the
    Production environment!
    • Or that will happen…

    View Slide

  38. RailsConf 2016
    Some of my findings

    View Slide

  39. RailsConf 2016
    Some of my findings
    Parameters I changed:
    • RUBY_GC_HEAP_INIT_SLOTS
    • RUBY_GC_HEAP_FREE_SLOTS
    • RUBY_GC_MALLOC_LIMIT
    • RUBY_GC_MALLOC_LIMIT_MAX

    View Slide

  40. RailsConf 2016
    Some of my findings
    A few comparisons:
    • Ruby 2.0.0 vs Ruby 2.1.7 (before)
    • Ruby 2.1.7: before and after
    • Ruby 2.2.3: before and after
    • Ruby 2.1.7 vs Ruby 2.2.3 (after)

    View Slide

  41. RailsConf 2016
    Ruby 2.0.0 vs Ruby 2.1.7

    View Slide

  42. RailsConf 2016
    v2.0.0
    v2.1.7

    View Slide

  43. RailsConf 2016
    v2.0.0
    v2.1.7

    View Slide

  44. RailsConf 2016
    Ruby 2.1.7 before vs after

    View Slide

  45. RailsConf 2016
    v2.1.7
    before
    v2.1.7
    after

    View Slide

  46. RailsConf 2016
    v2.1.7
    before
    v2.1.7
    after

    View Slide

  47. RailsConf 2016
    Ruby 2.2.3 before vs after

    View Slide

  48. RailsConf 2016
    v2.2.3
    before
    v2.2.3
    after

    View Slide

  49. RailsConf 2016
    v2.2.3
    before
    v2.2.3
    after

    View Slide

  50. RailsConf 2016
    Ruby 2.1.7 vs 2.2.3 (after)

    View Slide

  51. RailsConf 2016
    v2.1.7
    after
    v2.2.3
    after

    View Slide

  52. RailsConf 2016
    v2.1.7
    after
    v2.2.3
    after

    View Slide

  53. RailsConf 2016
    Thank you!
    Q&A?!
    Helio Cola
    http://hac-rods.me
    @hacrods

    View Slide

  54. RailsConf 2016
    Thanks
    Thanks to these folks that helped me along the way
    NoTwitterErik
    [email protected]

    View Slide