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

ruby-changes:46654

From: hsbt <ko1@a...>
Date: Wed, 17 May 2017 16:56:34 +0900 (JST)
Subject: [ruby-changes:46654] hsbt:r58770 (trunk): Optimize CSV#shift.

hsbt	2017-05-17 16:56:27 +0900 (Wed, 17 May 2017)

  New Revision: 58770

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

  Log:
    Optimize CSV#shift.
    
      [Bug #12373][ruby-core:75462]
      Patch by Yuki Kurihara.
    
      Benchmark:
      ```
      Warming up --------------------------------------
               csv_shift     1.000  i/100ms
           new_csv_shift     1.000  i/100ms
    Calculating -------------------------------------
               csv_shift      1.192  (?\194?\177 0.0%) i/s -      6.000  in 5.034250s
           new_csv_shift      1.527  (?\194?\177 0.0%) i/s -      8.000  in 5.243446s
    
    Comparison:
           new_csv_shift:        1.5 i/s
               csv_shift:        1.2 i/s - 1.28x  slower
      ```

  Modified files:
    trunk/lib/csv.rb
Index: lib/csv.rb
===================================================================
--- lib/csv.rb	(revision 58769)
+++ lib/csv.rb	(revision 58770)
@@ -1874,32 +1874,32 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L1874
       parts.each do |part|
         if in_extended_col
           # If we are continuing a previous column
-          if part[-1] == @quote_char && part.count(@quote_char) % 2 != 0
+          if part.end_with?(@quote_char) && part.count(@quote_char) % 2 != 0
             # extended column ends
             csv[-1] = csv[-1].push(part[0..-2]).join("")
             if csv.last =~ @parsers[:stray_quote]
               raise MalformedCSVError,
                     "Missing or stray quote in line #{lineno + 1}"
             end
-            csv.last.gsub!(@quote_char * 2, @quote_char)
+            csv.last.gsub!(@double_quote_char, @quote_char)
             in_extended_col = false
           else
             csv.last.push(part, @col_sep)
           end
-        elsif part[0] == @quote_char
+        elsif part.start_with?(@quote_char)
           # If we are starting a new quoted column
           if part.count(@quote_char) % 2 != 0
             # start an extended column
             csv << [part[1..-1], @col_sep]
             in_extended_col =  true
-          elsif part[-1] == @quote_char
+          elsif part.end_with?(@quote_char)
             # regular quoted column
             csv << part[1..-2]
             if csv.last =~ @parsers[:stray_quote]
               raise MalformedCSVError,
                     "Missing or stray quote in line #{lineno + 1}"
             end
-            csv.last.gsub!(@quote_char * 2, @quote_char)
+            csv.last.gsub!(@double_quote_char, @quote_char)
           elsif @liberal_parsing
             csv << part
           else
@@ -2017,6 +2017,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L2017
     @col_sep    = options.delete(:col_sep).to_s.encode(@encoding)
     @row_sep    = options.delete(:row_sep)  # encode after resolving :auto
     @quote_char = options.delete(:quote_char).to_s.encode(@encoding)
+    @double_quote_char = @quote_char * 2
 
     if @quote_char.length != 1
       raise ArgumentError, ":quote_char has to be a single character String"

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

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