Skip to content

Commit 46b7642

Browse files
committed
Replace each_slice by a while loop
Unfortunately `each_slice` performs pretty poorly because it isn't specialized for `Array`.
1 parent 69bfc52 commit 46b7642

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

actionpack/lib/action_dispatch/journey/gtg/simulator.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,16 @@ def memos(string)
5151
end
5252

5353
acceptance_states = []
54-
state.each_slice(2) do |s, idx|
55-
acceptance_states.concat(tt.memo(s)) if idx.nil? && tt.accepting?(s)
54+
states_count = state.size
55+
i = 0
56+
while i < states_count
57+
if state[i + 1].nil?
58+
s = state[i]
59+
if tt.accepting?(s)
60+
acceptance_states.concat(tt.memo(s))
61+
end
62+
end
63+
i += 2
5664
end
5765

5866
acceptance_states.empty? ? yield : acceptance_states

actionpack/lib/action_dispatch/journey/gtg/transition_table.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ def move(t, full_string, token, start_index, token_matches_default)
5252

5353
next_states = []
5454

55-
t.each_slice(2) { |s, previous_start|
55+
transitions_count = t.size
56+
i = 0
57+
while i < transitions_count
58+
s = t[i]
59+
previous_start = t[i + 1]
5660
if previous_start.nil?
5761
# In the simple case of a "default" param regex do this fast-path and add all
5862
# next states.
@@ -90,7 +94,9 @@ def move(t, full_string, token, start_index, token_matches_default)
9094
# need to remember where we started as well so we can take bigger slices.
9195
next_states << s << slice_start
9296
end
93-
}
97+
98+
i += 2
99+
end
94100

95101
next_states
96102
end

0 commit comments

Comments
 (0)