Skip to content

Commit

Permalink
[wip] Reuse instance when generate_line
Browse files Browse the repository at this point in the history
I'm still thinking...

I used benchmark script as below:
fluent/fluentd#2529

Warming up --------------------------------------
                 now     5.553k i/100ms
                 new    10.626k i/100ms
            instance     9.009k i/100ms
Calculating -------------------------------------
                 now     57.255k (± 4.1%) i/s -    288.756k in   5.051981s
                 new    114.090k (± 7.1%) i/s -    573.804k in   5.062333s
            instance     95.062k (± 4.1%) i/s -    477.477k in   5.031413s
  • Loading branch information
284km committed Sep 18, 2019
1 parent 71b072e commit afca3b4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ def initialize(message, line_number)
skip_lines: nil,
liberal_parsing: false,
quote_empty: true,
use_last_instance: false,
}.freeze

#
Expand Down Expand Up @@ -565,7 +566,19 @@ def self.generate_line(row, **options)
elsif field = row.find {|f| f.is_a?(String)}
str.force_encoding(field.encoding)
end
(new(str, options) << row).string
if options[:use_last_instance]
csv = last_instance(str, options)
line = (csv << row).string.dup
csv.rewind
csv.truncate(0)
line
else
(new(str, options) << row).string
end
end

def self.last_instance(data, **options)
(@@instance ||= {})[Thread.current] ||= new(data, options)
end

#
Expand Down Expand Up @@ -936,7 +949,8 @@ def initialize(data,
write_converters: nil,
write_nil_value: nil,
write_empty_value: "",
strip: false)
strip: false,
use_last_instance: false)
raise ArgumentError.new("Cannot parse nil as CSV") if data.nil?

# create the IO object we will read from
Expand Down

0 comments on commit afca3b4

Please sign in to comment.