ruby-changes:46789
From: k0kubun <ko1@a...>
Date: Fri, 26 May 2017 21:12:19 +0900 (JST)
Subject: [ruby-changes:46789] k0kubun:r58904 (trunk): erb.rb: Use script encoding instead of force_encoding
k0kubun 2017-05-26 21:12:13 +0900 (Fri, 26 May 2017) New Revision: 58904 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58904 Log: erb.rb: Use script encoding instead of force_encoding The original intention of introducing `_erbout.force_encoding` in r21170 was: - "returns a string in the same character encoding as the input string." - "When the input string has a magic comment, however, it returns a string in the encoding specified by the magic comment." And they are tested by test/erb/test_erb_m17n.rb well and this patch passes the test. Since magic comment is always added in ERB compiled code, using ''.dup instead of String.new will set correct encoding without calling force_encoding method. The benchmark results are: * Before $ ./ruby benchmark/run.rb --matzruby=./ruby -m bm_app_erb MatzRuby: ruby 2.5.0dev (2017-05-26 skip-force-enc.. 58903) [x86_64-linux] last_commit=Skip force_encoding in compiled code of erb Ruby: app_erb: matz 0.715 * After $ ./ruby benchmark/run.rb --matzruby=./ruby -m bm_app_erb MatzRuby: ruby 2.5.0dev (2017-05-26 skip-force-enc.. 58903) [x86_64-linux] last_commit=Skip force_encoding in compiled code of erb Ruby: app_erb: matz 0.672 And perf(1) results are: * Before $ sudo perf stat ./ruby benchmark/bm_app_erb.rb Performance counter stats for './ruby benchmark/bm_app_erb.rb': 709.571746 task-clock (msec) # 1.000 CPUs utilized 5 context-switches # 0.007 K/sec 1 cpu-migrations # 0.001 K/sec 1,337 page-faults # 0.002 M/sec 3,088,936,521 cycles # 4.353 GHz <not supported> stalled-cycles-frontend <not supported> stalled-cycles-backend 4,849,564,282 instructions # 1.57 insns per cycle 1,027,042,087 branches # 1447.411 M/sec 19,983,456 branch-misses # 1.95% of all branches 0.709747823 seconds time elapsed * After $ sudo perf stat ./ruby benchmark/bm_app_erb.rb Performance counter stats for './ruby benchmark/bm_app_erb.rb': 693.494673 task-clock (msec) # 1.000 CPUs utilized 7 context-switches # 0.010 K/sec 1 cpu-migrations # 0.001 K/sec 1,316 page-faults # 0.002 M/sec 3,025,639,349 cycles # 4.363 GHz <not supported> stalled-cycles-frontend <not supported> stalled-cycles-backend 4,694,848,271 instructions # 1.55 insns per cycle 994,496,704 branches # 1434.037 M/sec 19,693,239 branch-misses # 1.98% of all branches 0.693724345 seconds time elapsed [fix GH-1147] Modified files: trunk/lib/erb.rb Index: lib/erb.rb =================================================================== --- lib/erb.rb (revision 58903) +++ lib/erb.rb (revision 58904) @@ -280,7 +280,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L280 # ERB#src: # # compiler = ERB::Compiler.new('<>') - # compiler.pre_cmd = ["_erbout=String.new"] + # compiler.pre_cmd = ["_erbout=''.dup"] # compiler.put_cmd = "_erbout.<<" # compiler.insert_cmd = "_erbout.<<" # compiler.post_cmd = ["_erbout"] @@ -291,7 +291,7 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L291 # <i>Generates</i>: # # #coding:UTF-8 - # _erbout=String.new; _erbout.<< "Got "; _erbout.<<(( obj ).to_s); _erbout.<< "!\n"; _erbout + # _erbout=''.dup; _erbout.<< "Got "; _erbout.<<(( obj ).to_s); _erbout.<< "!\n"; _erbout # # By default the output is sent to the print method. For example: # @@ -861,8 +861,8 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L861 def set_eoutvar(compiler, eoutvar = '_erbout') compiler.put_cmd = "#{eoutvar}.<<" compiler.insert_cmd = "#{eoutvar}.<<" - compiler.pre_cmd = ["#{eoutvar} = String.new"] - compiler.post_cmd = ["#{eoutvar}.force_encoding(__ENCODING__)"] + compiler.pre_cmd = ["#{eoutvar} = ''.dup"] + compiler.post_cmd = [eoutvar] end # Generate results and print them. (see ERB#result) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/