ruby-changes:47541
From: glass <ko1@a...>
Date: Fri, 25 Aug 2017 17:13:40 +0900 (JST)
Subject: [ruby-changes:47541] glass:r59657 (trunk): csv.rb: optimize CSV::Table#to_a and #to_csv
glass 2017-08-25 17:13:32 +0900 (Fri, 25 Aug 2017) New Revision: 59657 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=59657 Log: csv.rb: optimize CSV::Table#to_a and #to_csv * lib/csv.rb (CSV::Table#to_a, #to_csv): use Array#push instead of Array#concat for performance improvement. This performance improvement is proposed by zdennis <zach.dennis@g...>. The patch is from Mau Magnaguagno <maumagnaguagno@g...>. close #946 Modified files: trunk/lib/csv.rb Index: lib/csv.rb =================================================================== --- lib/csv.rb (revision 59656) +++ lib/csv.rb (revision 59657) @@ -879,13 +879,11 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L879 # then all of the field rows will follow. # def to_a - @table.inject([headers]) do |array, row| - if row.header_row? - array - else - array + [row.fields] - end + array = [headers] + @table.each do |row| + array.push(row.fields) unless row.header_row? end + return array end # @@ -896,13 +894,11 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L894 # pass <tt>:write_headers => false</tt>. # def to_csv(write_headers: true, **options) - @table.inject(write_headers ? [headers.to_csv(options)] : [ ]) do |rows, row| - if row.header_row? - rows - else - rows + [row.fields.to_csv(options)] - end - end.join('') + array = write_headers ? [headers.to_csv(options)] : [] + @table.each do |row| + array.push(row.fields.to_csv(options)) unless row.header_row? + end + return array.join('') end alias_method :to_s, :to_csv -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/