ruby-changes:46790
From: k0kubun <ko1@a...>
Date: Fri, 26 May 2017 22:49:42 +0900 (JST)
Subject: [ruby-changes:46790] k0kubun:r58905 (trunk): erb.rb: Generate static string with opt_str_uminus
k0kubun 2017-05-26 22:49:35 +0900 (Fri, 26 May 2017) New Revision: 58905 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58905 Log: erb.rb: Generate static string with opt_str_uminus to skip object allocation for static string. We can't always enable frozen_string_literal pragma because we can't freeze string literals embedded by user for backward compatibility. So we need to use fstring for each static string. Since adding ".freeze" to string literals in #content_dump is slow on compiling, I used unary "-" operator instead. benchmark/bm_app_erb_render.rb: Added rendering-only benchmark to test rendering performance on production environment. This benchmark is created to reproduce the behavior on Sinatra (Tilt). Thus it doesn't use ERB#result to skip parsing compiled code. It doesn't use ERB#def_method too to regard `title` and `content` as local variables. If we use #def_method, `title` and `content` needs to be method call. I wanted to avoid it. This patch's benchmark results is: * Before app_erb_render 1.250 app_erb 0.704 * After app_erb_render 1.066 app_erb 0.686 This patch optimizes rendering performance (app_erb_render) without spoiling (total of rendering +) compiling performance (app_erb). Added files: trunk/benchmark/bm_app_erb_render.rb Modified files: trunk/lib/erb.rb Index: benchmark/bm_app_erb_render.rb =================================================================== --- benchmark/bm_app_erb_render.rb (nonexistent) +++ benchmark/bm_app_erb_render.rb (revision 58905) @@ -0,0 +1,26 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_app_erb_render.rb#L1 +require 'erb' + +data = DATA.read +max = 1_500_000 +title = "hello world!" +content = "hello world!\n" * 10 + +src = "def self.render(title, content); #{ERB.new(data).src}; end" +mod = Module.new +mod.instance_eval(src, "(ERB)") + +max.times do + mod.render(title, content) +end + +__END__ + +<html> + <head> <%= title %> </head> + <body> + <h1> <%= title %> </h1> + <p> + <%= content %> + </p> + </body> +</html> Index: lib/erb.rb =================================================================== --- lib/erb.rb (revision 58904) +++ lib/erb.rb (revision 58905) @@ -589,7 +589,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L589 end def add_put_cmd(out, content) - out.push("#{@put_cmd} #{content_dump(content)}") + out.push("#{@put_cmd} -#{content_dump(content)}") end def add_insert_cmd(out, content) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/