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

Rails from "good" to fast - Colognerb

Rails from "good" to fast - Colognerb

Rails from "good" to fast. How to get the view rendering from ~600ms to ~60ms.
https://github.com/timoschilling/YetAnotherBlog
https://github.com/apotonick/cells
https://github.com/apotonick/representable

Timo Schilling

February 17, 2016
Tweet

More Decks by Timo Schilling

Other Decks in Programming

Transcript

  1. Rails from “good” to fast How to get the view

    rendering from ~600ms to ~60ms
  2. What’s the slowest part in Rails? • Request Handling (ActionDispatch)

    • NO! • Database Queries (ActiveRecord) • NO, just a bit! • Rendering (ActionView, HAML, JBuilder) • YES!
  3. Side Note:
 Every Query is a lie! 0.3ms + 0.4ms

    + 4.4ms +0.6ms = 5.7ms 5.7ms != 6.1ms
  4. Why 5.7ms != 6.1ms? ActiveRecord show only the query time!

    Not the real amount of each operation!
  5. How to get a real value? Before: (0.3ms) SELECT COUNT(*)…


    Post Load (0.4ms) …
 User Load (0.4ms) …
 Comment Load (4.4ms) …
 User Load (0.6ms) … 
 
 Completed in 568ms
 (Views: 561.9ms |
 ActiveRecord: 6.1ms) After: (0.3ms) SELECT COUNT(*)…
 Post Load (0.4ms) …
 User Load (0.4ms) …
 Comment Load (4.4ms) …
 User Load (0.6ms) …
 
 DB Queries: 78.85ms
 
 Completed in 568ms
 (Views: 489,1ms |
 ActiveRecord: 6.1ms)
  6. Hint! If you use Trailblazer:
 You will get this kind

    of measurement for free, in the future.
  7. Russian Doll Caching It helps only if you have slow

    views! This view has the same render time as a view without `sleep` with Russian Doll Caching
  8. Where is the Time? • 150ms Request • DB: 70ms

    • ActiveRecord: 6.1ms • ActionView: 78.1ms • Cells: 71.4ms
  9. Where is the Time? • 145ms Request • DB: 70ms

    • ActiveRecord: 6.1ms • ActionView: 74.1ms • Cells: 71.4ms
  10. Where is the Time? • 145ms Request • DB: 70ms

    • ActiveRecord: 6.1ms • Cells: 71.4ms
  11. Why need that ~70ms? • 1 Layout • Index List

    • 100 Post • + 1 User • + 1 Comment • + 1 User • = 404 Renderings • 404 Renderings • each rendering needs 100 method call (theoretical assumption)
  12. What does Tilt do? • one time • compile the

    template • build a method • cache this method • per rendering • bind the method to the context • call the method • unbind the method from context • one time • compile the template • build a method • cache this method • per rendering • bind the method to the context • call the method • unbind the method from context
  13. Where is the Time? • 91ms Request • DB: 70ms

    • ActiveRecord: 6.1ms • Cells: 17.4ms
  14. Path helpers • post_path(model) # => f***ing slow • post_path(id:

    model.id) # => slow • “/posts/#{id}” # => fast • brings up to 4ms (50%)