Skip to content

Commit

Permalink
Don't build quoted_fields array when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall-lee committed Jul 5, 2024
1 parent 4534f35 commit dceae89
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 11 deletions.
2 changes: 2 additions & 0 deletions benchmark/convert_nil.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ contexts:
csv: 3.0.1
- gems:
csv: 3.0.2
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
2 changes: 2 additions & 0 deletions benchmark/parse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ contexts:
csv: 3.0.1
- gems:
csv: 3.0.2
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
2 changes: 2 additions & 0 deletions benchmark/parse_liberal_parsing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ loop_count: 100
contexts:
- gems:
csv: 3.0.2
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
2 changes: 2 additions & 0 deletions benchmark/parse_quote_char_nil.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
loop_count: 100
contexts:
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
2 changes: 2 additions & 0 deletions benchmark/parse_strip.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
loop_count: 100
contexts:
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
2 changes: 2 additions & 0 deletions benchmark/read.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ contexts:
csv: 3.0.1
- gems:
csv: 3.0.2
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
2 changes: 2 additions & 0 deletions benchmark/shift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ contexts:
csv: 3.0.1
- gems:
csv: 3.0.2
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
2 changes: 2 additions & 0 deletions benchmark/write.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ contexts:
csv: 3.0.1
- gems:
csv: 3.0.2
- gems:
csv: 3.3.0
- name: "master"
prelude: |
$LOAD_PATH.unshift(File.expand_path("lib"))
Expand Down
6 changes: 6 additions & 0 deletions lib/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,12 @@ def initialize(encoding, line_number)
write_empty_value: "",
}.freeze

module NoQuotedFields # :nodoc:
def self.[](_index)
false
end
end

class << self
# :call-seq:
# instance(string, **options)
Expand Down
2 changes: 1 addition & 1 deletion lib/csv/fields_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def empty?
@converters.empty?
end

def convert(fields, headers, lineno, quoted_fields)
def convert(fields, headers, lineno, quoted_fields = NoQuotedFields)
return fields unless need_convert?

fields.collect.with_index do |field, index|
Expand Down
14 changes: 6 additions & 8 deletions lib/csv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ def prepare_header
case headers
when Array
@raw_headers = headers
quoted_fields = [false] * @raw_headers.size
quoted_fields = NoQuotedFields
@use_headers = true
when String
@raw_headers, quoted_fields = parse_headers(headers)
Expand Down Expand Up @@ -944,11 +944,9 @@ def parse_no_quote(&block)
if line.empty?
next if @skip_blanks
row = []
quoted_fields = []
else
line = strip_value(line)
row = line.split(@split_column_separator, -1)
quoted_fields = [false] * row.size
if @max_field_size
row.each do |column|
validate_field_size(column)
Expand All @@ -962,7 +960,7 @@ def parse_no_quote(&block)
end
end
@last_line = original_line
emit_row(row, quoted_fields, &block)
emit_row(row, &block)
end
end

Expand All @@ -984,7 +982,7 @@ def parse_quotable_loose(&block)
next
end
row = []
quoted_fields = []
quoted_fields = NoQuotedFields
elsif line.include?(@cr) or line.include?(@lf)
@scanner.keep_back
@need_robust_parsing = true
Expand Down Expand Up @@ -1046,13 +1044,13 @@ def parse_quotable_robust(&block)
quoted_fields << @quoted_column_value
elsif parse_row_end
if row.empty? and value.nil?
emit_row([], [], &block) unless @skip_blanks
emit_row(row, &block) unless @skip_blanks
else
row << value
quoted_fields << @quoted_column_value
emit_row(row, quoted_fields, &block)
row = []
quoted_fields = []
quoted_fields.clear
end
skip_needless_lines
start_row
Expand Down Expand Up @@ -1257,7 +1255,7 @@ def start_row
@scanner.keep_start
end

def emit_row(row, quoted_fields, &block)
def emit_row(row, quoted_fields = NoQuotedFields, &block)
@lineno += 1

raw_row = row
Expand Down
3 changes: 1 addition & 2 deletions lib/csv/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def <<(row)
@lineno += 1

if @fields_converter
quoted_fields = [false] * row.size
row = @fields_converter.convert(row, nil, lineno, quoted_fields)
row = @fields_converter.convert(row, nil, lineno)
end

i = -1
Expand Down

0 comments on commit dceae89

Please sign in to comment.