[前][次][番号順一覧][スレッド一覧]

ruby-changes:46725

From: k0kubun <ko1@a...>
Date: Mon, 22 May 2017 12:56:21 +0900 (JST)
Subject: [ruby-changes:46725] k0kubun:r58842 (trunk): erb.rb: Skip creating regexp

k0kubun	2017-05-22 12:56:16 +0900 (Mon, 22 May 2017)

  New Revision: 58842

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58842

  Log:
    erb.rb: Skip creating regexp
    
    if stags and etags are not changed from default.
    
    :putobject insn (of regexp) will be used instead of :toregexp insn.
    This means that the regexp won't be compiled for every
    `SimpleScanner#scan` call.
    
    It may not be a good idea to apply this kind of optimization for all cases.
    But I applied this because it is default scanner and used frequently and has
    relatively large impact for benchmark like this:
    
    * Before
    app_erb 1.023
    
    * After
    app_erb 0.781
    
    This commit fixes only the bottleneck of performance regression introduced
    in r53412. For maintainability, I won't fix other small regressions like
    additional overhead of method calls.
    
    [ruby-core:73820] [Bug #12074]

  Modified files:
    trunk/lib/erb.rb
Index: lib/erb.rb
===================================================================
--- lib/erb.rb	(revision 58841)
+++ lib/erb.rb	(revision 58842)
@@ -371,11 +371,13 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L371
         klass.new(src, trim_mode, percent)
       end
 
+      DEFAULT_STAGS = %w(<%% <%= <%# <%).freeze
+      DEFAULT_ETAGS = %w(%%> %>).freeze
       def initialize(src, trim_mode, percent)
         @src = src
         @stag = nil
-        @stags = %w(<%% <%= <%# <%).freeze
-        @etags = %w(%%> %>).freeze
+        @stags = DEFAULT_STAGS
+        @etags = DEFAULT_ETAGS
       end
       attr_accessor :stag
       attr_reader :stags, :etags
@@ -505,8 +507,8 @@ class ERB https://github.com/ruby/ruby/blob/trunk/lib/erb.rb#L507
     else
       class SimpleScanner < Scanner # :nodoc:
         def scan
-          stag_reg = /(.*?)(#{stags.join('|')}|\z)/m
-          etag_reg = /(.*?)(#{etags.join('|')}|\z)/m
+          stag_reg = (stags == DEFAULT_STAGS) ? /(.*?)(<%[%=#]?|\z)/m : /(.*?)(#{stags.join('|')}|\z)/m
+          etag_reg = (etags == DEFAULT_ETAGS) ? /(.*?)(%%?>|\z)/m : /(.*?)(#{etags.join('|')}|\z)/m
           scanner = StringScanner.new(@src)
           while ! scanner.eos?
             scanner.scan(@stag ? etag_reg : stag_reg)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]