Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid dynamic parse method dispatch for faster access #311

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 10 additions & 13 deletions lib/csv/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,7 @@ def parse(&block)

begin
@scanner ||= build_scanner
if quote_character.nil?
parse_no_quote(&block)
elsif @need_robust_parsing
parse_quotable_robust(&block)
else
parse_quotable_loose(&block)
end
__send__(@parse_method, &block)
rescue InvalidEncoding
if @scanner
ignore_broken_line
Expand Down Expand Up @@ -459,7 +453,6 @@ def prepare
end

def prepare_variable
@need_robust_parsing = false
@encoding = @options[:encoding]
liberal_parsing = @options[:liberal_parsing]
if liberal_parsing
Expand All @@ -472,7 +465,6 @@ def prepare_variable
@double_quote_outside_quote = false
@backslash_quote = false
end
@need_robust_parsing = true
else
@liberal_parsing = false
@backslash_quote = false
Expand Down Expand Up @@ -554,15 +546,13 @@ def prepare_strip
@rstrip_value = Regexp.new(@escaped_strip +
"+\\z".encode(@encoding))
end
@need_robust_parsing = true
elsif @strip
strip_values = " \t\f\v"
@escaped_strip = strip_values.encode(@encoding)
if @quote_character
@strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding))
@rstrip_value = Regexp.new("[#{strip_values}]+\\z".encode(@encoding))
end
@need_robust_parsing = true
end
end

Expand Down Expand Up @@ -808,6 +798,13 @@ def adjust_headers(headers, quoted_fields)

def prepare_parser
@may_quoted = may_quoted?
if @quote_character.nil?
@parse_method = :parse_no_quote
elsif @liberal_parsing or @strip
@parse_method = :parse_quotable_robust
else
@parse_method = :parse_quotable_loose
end
end

def may_quoted?
Expand Down Expand Up @@ -987,7 +984,7 @@ def parse_quotable_loose(&block)
quoted_fields = []
elsif line.include?(@cr) or line.include?(@lf)
@scanner.keep_back
@need_robust_parsing = true
@parse_method = :parse_quotable_robust
return parse_quotable_robust(&block)
else
row = line.split(@split_column_separator, -1)
Expand All @@ -1011,7 +1008,7 @@ def parse_quotable_loose(&block)
row[i] = column[1..-2]
else
@scanner.keep_back
@need_robust_parsing = true
@parse_method = :parse_quotable_robust
return parse_quotable_robust(&block)
end
validate_field_size(row[i])
Expand Down