Skip to content

Commit 378f55f

Browse files
committed
Save @parse_method for faster access
1 parent 4534f35 commit 378f55f

File tree

9 files changed

+20
-29
lines changed

9 files changed

+20
-29
lines changed

benchmark/convert_nil.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
loop_count: 100
22
contexts:
33
- gems:
4-
csv: 3.0.1
5-
- gems:
6-
csv: 3.0.2
4+
csv: 3.3.0
75
- name: "master"
86
prelude: |
97
$LOAD_PATH.unshift(File.expand_path("lib"))

benchmark/parse.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
loop_count: 100
22
contexts:
33
- gems:
4-
csv: 3.0.1
5-
- gems:
6-
csv: 3.0.2
4+
csv: 3.3.0
75
- name: "master"
86
prelude: |
97
$LOAD_PATH.unshift(File.expand_path("lib"))

benchmark/parse_liberal_parsing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
loop_count: 100
22
contexts:
33
- gems:
4-
csv: 3.0.2
4+
csv: 3.3.0
55
- name: "master"
66
prelude: |
77
$LOAD_PATH.unshift(File.expand_path("lib"))

benchmark/parse_quote_char_nil.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
loop_count: 100
22
contexts:
3+
- gems:
4+
csv: 3.3.0
35
- name: "master"
46
prelude: |
57
$LOAD_PATH.unshift(File.expand_path("lib"))

benchmark/parse_strip.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
loop_count: 100
22
contexts:
3+
- gems:
4+
csv: 3.3.0
35
- name: "master"
46
prelude: |
57
$LOAD_PATH.unshift(File.expand_path("lib"))

benchmark/read.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
loop_count: 100
22
contexts:
33
- gems:
4-
csv: 3.0.1
5-
- gems:
6-
csv: 3.0.2
4+
csv: 3.3.0
75
- name: "master"
86
prelude: |
97
$LOAD_PATH.unshift(File.expand_path("lib"))

benchmark/shift.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
loop_count: 100
22
contexts:
33
- gems:
4-
csv: 3.0.1
5-
- gems:
6-
csv: 3.0.2
4+
csv: 3.3.0
75
- name: "master"
86
prelude: |
97
$LOAD_PATH.unshift(File.expand_path("lib"))

benchmark/write.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
loop_count: 100
22
contexts:
33
- gems:
4-
csv: 3.0.1
5-
- gems:
6-
csv: 3.0.2
4+
csv: 3.3.0
75
- name: "master"
86
prelude: |
97
$LOAD_PATH.unshift(File.expand_path("lib"))

lib/csv/parser.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,7 @@ def parse(&block)
409409

410410
begin
411411
@scanner ||= build_scanner
412-
if quote_character.nil?
413-
parse_no_quote(&block)
414-
elsif @need_robust_parsing
415-
parse_quotable_robust(&block)
416-
else
417-
parse_quotable_loose(&block)
418-
end
412+
@parse_method.call(&block)
419413
rescue InvalidEncoding
420414
if @scanner
421415
ignore_broken_line
@@ -459,7 +453,6 @@ def prepare
459453
end
460454

461455
def prepare_variable
462-
@need_robust_parsing = false
463456
@encoding = @options[:encoding]
464457
liberal_parsing = @options[:liberal_parsing]
465458
if liberal_parsing
@@ -472,7 +465,6 @@ def prepare_variable
472465
@double_quote_outside_quote = false
473466
@backslash_quote = false
474467
end
475-
@need_robust_parsing = true
476468
else
477469
@liberal_parsing = false
478470
@backslash_quote = false
@@ -554,15 +546,13 @@ def prepare_strip
554546
@rstrip_value = Regexp.new(@escaped_strip +
555547
"+\\z".encode(@encoding))
556548
end
557-
@need_robust_parsing = true
558549
elsif @strip
559550
strip_values = " \t\f\v"
560551
@escaped_strip = strip_values.encode(@encoding)
561552
if @quote_character
562553
@strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding))
563554
@rstrip_value = Regexp.new("[#{strip_values}]+\\z".encode(@encoding))
564555
end
565-
@need_robust_parsing = true
566556
end
567557
end
568558

@@ -808,6 +798,13 @@ def adjust_headers(headers, quoted_fields)
808798

809799
def prepare_parser
810800
@may_quoted = may_quoted?
801+
@parse_method = if @quote_character.nil?
802+
method(:parse_no_quote)
803+
elsif @liberal_parsing || @strip
804+
method(:parse_quotable_robust)
805+
else
806+
method(:parse_quotable_loose)
807+
end
811808
end
812809

813810
def may_quoted?
@@ -987,7 +984,7 @@ def parse_quotable_loose(&block)
987984
quoted_fields = []
988985
elsif line.include?(@cr) or line.include?(@lf)
989986
@scanner.keep_back
990-
@need_robust_parsing = true
987+
@parse_method = method(:parse_quotable_robust)
991988
return parse_quotable_robust(&block)
992989
else
993990
row = line.split(@split_column_separator, -1)
@@ -1011,7 +1008,7 @@ def parse_quotable_loose(&block)
10111008
row[i] = column[1..-2]
10121009
else
10131010
@scanner.keep_back
1014-
@need_robust_parsing = true
1011+
@parse_method = method(:parse_quotable_robust)
10151012
return parse_quotable_robust(&block)
10161013
end
10171014
validate_field_size(row[i])

0 commit comments

Comments
 (0)