Skip to content

Commit 3a28897

Browse files
committed
Avoid excessive splitting of nodes caused by a unique proc being created per call when a method reference would suffice.
1 parent b4fe248 commit 3a28897

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

src/main/ruby/truffleruby/core/splitter.rb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def split(string, pattern, limit, &orig_block)
4444
# To simplify the code, we present a single block
4545
# that either calls user (origin) block or adds a substring to the resulting array
4646
# See motivation: https://github.com/oracle/truffleruby/pull/2052#issuecomment-663449395
47-
block = orig_block || result.method(:<<).to_proc
47+
callable = orig_block || result.method(:<<)
4848

4949
return (orig_block ? string : result) if string.empty?
5050

@@ -57,7 +57,7 @@ def split(string, pattern, limit, &orig_block)
5757
if limit == 1
5858
dup_string = string.dup
5959

60-
block.call(dup_string)
60+
callable.call(dup_string)
6161
return orig_block ? dup_string : result
6262
end
6363

@@ -72,17 +72,17 @@ def split(string, pattern, limit, &orig_block)
7272
# See motivation: https://github.com/oracle/truffleruby/pull/2052#issuecomment-663494235
7373
return Primitive.string_awk_split string, awk_limit, orig_block
7474
elsif Primitive.is_a?(pattern, Regexp)
75-
split_type_regexp(string, pattern, limit, block)
75+
split_type_regexp(string, pattern, limit, callable)
7676
else
7777
pattern = StringValue(pattern)
7878

7979
valid_encoding?(string)
8080
valid_encoding?(pattern)
8181

8282
if pattern.empty?
83-
split_type_chars(string, limit, block)
83+
split_type_chars(string, limit, callable)
8484
else
85-
split_type_string(string, pattern, limit, block)
85+
split_type_string(string, pattern, limit, callable)
8686
end
8787
end
8888

@@ -95,24 +95,24 @@ def valid_encoding?(string)
9595
raise ArgumentError, "invalid byte sequence in #{string.encoding.name}" unless string.valid_encoding?
9696
end
9797

98-
def split_type_chars(string, limit, block)
98+
def split_type_chars(string, limit, callable)
9999
if limit > 0
100100
last = string.size > (limit - 1) ? string[(limit - 1)..-1] : empty_string(string)
101101

102102
string.each_char.each_with_index do |char, index|
103103
break if index == limit - 1
104-
block.call(char)
104+
callable.call(char)
105105
end
106106

107-
block.call(last)
107+
callable.call(last)
108108
else
109-
string.each_char(&block)
109+
string.each_char(&callable)
110110

111-
block.call(empty_string(string)) if tail_empty?(limit)
111+
callable.call(empty_string(string)) if tail_empty?(limit)
112112
end
113113
end
114114

115-
def split_type_string(string, pattern, limit, block)
115+
def split_type_string(string, pattern, limit, callable)
116116
pos = 0
117117
empty_count = 0
118118
limited = limit > 0
@@ -128,24 +128,24 @@ def split_type_string(string, pattern, limit, block)
128128
break unless nxt
129129

130130
match_size = nxt - pos
131-
empty_count = add_substring(string, ret, string.byteslice(pos, match_size), empty_count, block)
131+
empty_count = add_substring(string, ret, string.byteslice(pos, match_size), empty_count, callable)
132132

133133
pos = nxt + pat_size
134134
count += 1
135135
end
136136

137137
# No more separators, but we need to grab the last part still.
138-
empty_count = add_substring(string, ret, string.byteslice(pos, str_size - pos), empty_count, block)
138+
empty_count = add_substring(string, ret, string.byteslice(pos, str_size - pos), empty_count, callable)
139139

140140
if tail_empty?(limit)
141-
add_empty(string, ret, empty_count, block)
141+
add_empty(string, ret, empty_count, callable)
142142
end
143143
end
144144

145-
def split_type_regexp(string, pattern, limit, block)
145+
def split_type_regexp(string, pattern, limit, callable)
146146
# Handle // as a special case.
147147
if pattern.source.empty?
148-
return split_type_chars(string, limit, block)
148+
return split_type_chars(string, limit, callable)
149149
end
150150

151151
start = 0
@@ -164,12 +164,12 @@ def split_type_regexp(string, pattern, limit, block)
164164

165165
unless collapsed && (Primitive.match_data_byte_begin(match, 0) == last_match_end)
166166
substring = Truffle::RegexpOperations.pre_match_from(match, last_match_end)
167-
empty_count = add_substring(string, ret, substring, empty_count, block)
167+
empty_count = add_substring(string, ret, substring, empty_count, callable)
168168

169169
# length > 1 means there are captures
170170
if match.length > 1
171171
match.captures.compact.each do |capture|
172-
empty_count = add_substring(string, ret, capture, empty_count, block)
172+
empty_count = add_substring(string, ret, capture, empty_count, callable)
173173
end
174174
end
175175

@@ -186,31 +186,31 @@ def split_type_regexp(string, pattern, limit, block)
186186
end
187187

188188
if last_match
189-
empty_count = add_substring(string, ret, last_match.post_match, empty_count, block)
189+
empty_count = add_substring(string, ret, last_match.post_match, empty_count, callable)
190190
elsif ret.empty?
191-
empty_count = add_substring(string, ret, string.dup, empty_count, block)
191+
empty_count = add_substring(string, ret, string.dup, empty_count, callable)
192192
end
193193

194194
if tail_empty?(limit)
195-
add_empty(string, ret, empty_count, block)
195+
add_empty(string, ret, empty_count, callable)
196196
end
197197

198-
block ? string : ret
198+
callable ? string : ret
199199
end
200200

201201

202-
def add_substring(string, array, substring, empty_count, block)
202+
def add_substring(string, array, substring, empty_count, callable)
203203
return empty_count + 1 if substring.length == 0 # remember another one empty match
204204

205-
add_empty(string, array, empty_count, block)
205+
add_empty(string, array, empty_count, callable)
206206

207-
block.call(substring)
207+
callable.call(substring)
208208

209209
0 # always release all empties when we get non empty substring
210210
end
211211

212-
def add_empty(string, array, count, block)
213-
count.times { block.call(empty_string(string)) }
212+
def add_empty(string, array, count, callable)
213+
count.times { callable.call(empty_string(string)) }
214214
end
215215

216216
def empty_string(original)

0 commit comments

Comments
 (0)