Skip to content

Commit 1b6a1d4

Browse files
committed
[GR-32765] Split internal strscan method into multiple parts for better inlining.
PullRequest: truffleruby/2828
2 parents b6b1645 + 6efe4be commit 1b6a1d4

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

lib/truffle/strscan.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,31 @@ def peep(len)
308308
peek len
309309
end
310310

311-
def scan_internal(pattern, advance_pos, getstr, headonly)
311+
private def scan_check_args(pattern, headonly)
312312
unless pattern.kind_of? Regexp
313313
raise TypeError, "bad pattern argument: #{pattern.inspect}"
314314
end
315315
raise ArgumentError, 'uninitialized StringScanner object' unless @string
316+
end
317+
318+
# This method is kept very small so that it should fit within 100
319+
# AST nodes and can be split. This is done to avoid indirect calls
320+
# to TRegex.
321+
private def scan_internal(pattern, advance_pos, getstr, headonly)
322+
scan_check_args(pattern, headonly)
316323

317324
md = Truffle::RegexpOperations.match_in_region pattern, @string, pos, @string.bytesize, headonly, pos
318-
Primitive.matchdata_fixup_positions(md, pos) if md
319-
@match = md
320-
return nil unless @match
325+
if md
326+
Primitive.matchdata_fixup_positions(md, pos)
327+
@match = md
328+
scan_internal_set_pos_and_str(advance_pos, getstr, md)
329+
else
330+
@match = nil
331+
end
332+
end
321333

322-
fin = Primitive.match_data_byte_end(@match, 0)
334+
private def scan_internal_set_pos_and_str(advance_pos, getstr, md)
335+
fin = Primitive.match_data_byte_end(md, 0)
323336

324337
@prev_pos = @pos
325338
@pos = fin if advance_pos
@@ -329,6 +342,5 @@ def scan_internal(pattern, advance_pos, getstr, headonly)
329342

330343
@string.byteslice(@prev_pos, width)
331344
end
332-
private :scan_internal
333345

334346
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
slow:StringScanner has a `scan_internal` method of under 100 AST nodes

spec/truffle/strscan/strscan_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative '../../ruby/spec_helper'
2+
3+
require 'strscan'
4+
5+
describe 'StringScanner' do
6+
it 'has a `scan_internal` method of under 100 AST nodes' do
7+
ruby_exe('require "strscan"; print Truffle::Debug.ast_size(StringScanner.instance_method(:scan_internal))').to_i.should < 100
8+
end
9+
end

0 commit comments

Comments
 (0)