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

A Quest for the Ultimate Template Engine

A Quest for the Ultimate Template Engine

Slides for MountainWest RubyConf 2015 talk "A Quest for the Ultimate Template Engine" http://mtnwestrubyconf.org/2015/sessions

Akira Matsuda

March 09, 2015
Tweet

More Decks by Akira Matsuda

Other Decks in Programming

Transcript

  1. Template Engines Text Files + Ruby => HTML On Rails

    (ActionView) app/views/*.html.*
  2. ERB: The Default One Included in the Matz Ruby package

    as a stdlib Still a part of DHH's "omakase"
  3. The Syntax <tbody> <% @users.each do |user| %> <tr> <td><%=

    user.name %></td> </tr> <% end %> </tbody>
  4. The syntax looked familiar to me I used to be

    an ASP programmer I used to be a PHP programmer I used to be a JSP programmer
  5. We are humans <tbody> <% @users.each do |user| %> <tr>

    <td><%= user.name %></td> </tr> <% end %> </tbody>
  6. We make mistakes <tbody> <% @users.each do |user| %> <tr>

    <td><%= user.name %></td> <% end %> </tr> </tbody>
  7. Valid HTML! Parses the markup then compiles it into an

    HTML Ensures that the output is always valid HTML This was a revolution!
  8. Then One Day, A Rails newbie came to me and

    said, "why is Haml so slow?" My app responds very quickly as an API server, but rendering the same set of data into an HTML is too slow
  9. @mineroaoki A Ruby committer The author of Net::HTTP, FileUtils, Ripper,

    Racc, TMail, etc. The author of legendary Ruby book "Ruby Hacking Guide"
  10. History of Minero Aoki 1999: Became a Ruby Committer 2002:

    Wrote RHG while he was a student ..2013: Had been doing something else, wearing suit 2013: Came back to the Ruby world as a Rails programmer
  11. RHG Even @ko1 learned Ruby implementation through this book English

    version is available online: http://ruby-hacking-guide.github.io/
  12. I thought, Yeah, Haml might not be as fast as

    ERB Because of its syntax, parsing & compiling should be slow, but who cares?
  13. He is a super hacker But a newbie Maybe his

    template is slow just because his Haml code sucks Or maybe what he meant was "ActionView is slow"?
  14. Parsing & compiling Haml might be slow Because Haml is

    so complex But compiled code will be cached by ActionView
  15. A Rails tips If you feel like your view is

    slow, it might be because of url_for
  16. From the Log File I, [2015-03-09T18:34:26.780189 #57017] INFO -- :

    Completed 200 OK in 312ms (Views: 307.5ms | ActiveRecord: 3.9ms) I, [2015-03-09T18:34:27.103951 #57017] INFO -- : Completed 200 OK in 319ms (Views: 314.8ms | ActiveRecord: 3.9ms) I, [2015-03-09T18:34:27.366058 #57017] INFO -- : Completed 200 OK in 258ms (Views: 254.7ms | ActiveRecord: 2.9ms) I, [2015-03-09T18:34:27.658356 #57017] INFO -- : Completed 200 OK in 289ms (Views: 285.7ms | ActiveRecord: 2.5ms) I, [2015-03-09T18:34:27.953852 #57017] INFO -- : Completed 200 OK in 290ms (Views: 287.2ms | ActiveRecord: 2.4ms) I, [2015-03-09T18:34:28.216769 #57017] INFO -- : Completed 200 OK in 258ms (Views: 255.1ms | ActiveRecord: 2.6ms) I, [2015-03-09T18:34:28.560278 #57017] INFO -- : Completed 200 OK in 339ms (Views: 334.9ms | ActiveRecord: 3.6ms)
  17. OMG

  18. Haml is slow! It turned out that Haml is 7%

    slower in production runtime!
  19. "Premature optimization is the root of all evil" There must

    be so many other reasons that make your Rails app slow DB network Business logic
  20. We're Ruby programmers If you really care about the app's

    performance, your app should've written in C What we should care is the balance
  21. What shall we do? 1. Do Nothing 2. Switch Back

    to ERB 3. Find Something Else
  22. Google says, "I heard that Slim is faster than Haml,

    so it must be a good one" "I read someone's blog that shows Slim is faster than Haml, so I switched to Slim"
  23. "Faster than Haml" Seems like a good marketing Some people

    may choose Python over Ruby Some people may choose JRuby over CRuby because b() runs faster on JRuby
  24. What shall we do? 1. Do Nothing 2. Switch Back

    to ERB 3. Find Something Else 4. Improve Haml
  25. I don't know why but I had a commit bit

    When I PRed one line of code
  26. I Had to Do This Because, Our super large legacy

    Rails app was still on Ruby 1.8 at that moment...
  27. Hello! in Haml begin;extend Haml::Helpers;_hamlout = @haml_buffer = Haml::Buffer.new(haml_buffer, {:autoclose=>["area",

    "base", "basefont", "br", "col", "command", "embed", "frame", "hr", "img", "input", "isindex", "keygen", "link", "menuitem", "meta", "param", "source", "track", "wbr"], :preserve=>["textarea", "pre", "code"], :attr_wrapper=>"'", :ugly=>false, :format=>:html5, :encoding=>"UTF-8", :escape_html=>false, :escape_attrs=>true, :hyphenate_data_attrs=>true, :cdata=>false});_erbout = _hamlout.buffer;@output_buffer = output_buffer ||= ActionView::OutputBuffer.new rescue nil;;_hamlout.push_text("#{_hamlout.format_script_false_false_ false_false_false_true_false(( 'Hello!' ));}\n", 0, false);_erbout;ensure;@haml_buffer = @haml_buffer.upper if @haml_buffer;end;
  28. What shall we do? 1. Do Nothing 2. Switch Back

    to ERB 3. Find Something Else 4. Improve Haml 5. Fork Haml
  29. I concluded that I can not make Haml as fast

    as ERB without breaking compatibility
  30. What shall we do? 1. Do Nothing 2. Switch Back

    to ERB 3. Find Something Else 4. Improve Haml 5. Fork Haml 6. Create a New Haml Implementation
  31. His Concept Slim is faster than Haml because Slim is

    built on Temple Slim and Haml's syntax is similar Why don't we make a Haml compiler on top of Temple?
  32. The Creator of Temple Says, Nathan Weizenbaum (the maintainer of

    Haml) told me targeting Haml could be difficult, but I’m quite optimistic. It’s better to try, fail and learn, than not try at all. http://timelessrepo.com/temple#another_abstraction_haml_and_html
  33. But @egletmt did it. Because he is a better hacker

    than me Or maybe because he is young and brave
  34. The Result (100 requests) ERB: 27135.9 msec Haml: 29116.8 msec

    HamlX: 27407.7 msec FastHaml: 23769.8 msec
  35. Worth Trying This If you're really sure that your system's

    bottleneck lies in the template engine If you want to experience the world's fastest Template engine
  36. YOU can do it! YOU can patch YOU can create

    your own engine Rather than just sitting and waiting and complaining