Skip to content

Commit 851b9d4

Browse files
authored
Merge pull request rails#54476 from skipkayhil/hm-flatten-stdparam
Simplify stdparam state to reduce retained hashes
2 parents 76d41ee + 3fac13e commit 851b9d4

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

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

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def move(t, full_string, start_index, end_index)
5959
if previous_start.nil?
6060
# In the simple case of a "default" param regex do this fast-path and add all
6161
# next states.
62-
if token_matches_default_component && states = @stdparam_states[s]
63-
states.each { |re, v| next_states << [v, nil].freeze if !v.nil? }
62+
if token_matches_default_component && std_state = @stdparam_states[s]
63+
next_states << [std_state, nil].freeze
6464
end
6565

6666
# When we have a literal string, we can just pull the next state
@@ -163,54 +163,40 @@ def visualizer(paths, title = "FSM")
163163
end
164164

165165
def []=(from, to, sym)
166-
to_mappings = states_hash_for(sym)[from] ||= {}
167166
case sym
167+
when String, Symbol
168+
to_mapping = @string_states[from] ||= {}
169+
# account for symbols in the constraints the same as strings
170+
to_mapping[sym.to_s] = to
168171
when Regexp
169-
# we must match the whole string to a token boundary
170172
if sym == DEFAULT_EXP
171-
sym = DEFAULT_EXP_ANCHORED
173+
@stdparam_states[from] = to
172174
else
173-
sym = /\A#{sym}\Z/
175+
to_mapping = @regexp_states[from] ||= {}
176+
# we must match the whole string to a token boundary
177+
to_mapping[/\A#{sym}\Z/] = to
174178
end
175-
when Symbol
176-
# account for symbols in the constraints the same as strings
177-
sym = sym.to_s
179+
else
180+
raise ArgumentError, "unknown symbol: %s" % sym.class
178181
end
179-
to_mappings[sym] = to
180182
end
181183

182184
def states
183185
ss = @string_states.keys + @string_states.values.flat_map(&:values)
184-
ps = @stdparam_states.keys + @stdparam_states.values.flat_map(&:values)
186+
ps = @stdparam_states.keys + @stdparam_states.values
185187
rs = @regexp_states.keys + @regexp_states.values.flat_map(&:values)
186188
(ss + ps + rs).uniq
187189
end
188190

189191
def transitions
190192
@string_states.flat_map { |from, hash|
191193
hash.map { |s, to| [from, s, to] }
192-
} + @stdparam_states.flat_map { |from, hash|
193-
hash.map { |s, to| [from, s, to] }
194+
} + @stdparam_states.map { |from, to|
195+
[from, DEFAULT_EXP_ANCHORED, to]
194196
} + @regexp_states.flat_map { |from, hash|
195197
hash.map { |s, to| [from, s, to] }
196198
}
197199
end
198-
199-
private
200-
def states_hash_for(sym)
201-
case sym
202-
when String, Symbol
203-
@string_states
204-
when Regexp
205-
if sym == DEFAULT_EXP
206-
@stdparam_states
207-
else
208-
@regexp_states
209-
end
210-
else
211-
raise ArgumentError, "unknown symbol: %s" % sym.class
212-
end
213-
end
214200
end
215201
end
216202
end

actionpack/lib/action_dispatch/journey/visualizer/fsm.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,10 @@ function match(input) {
105105
}
106106

107107
if(stdparam_states[state] && default_re.test(token)) {
108-
for(var key in stdparam_states[state]) {
109-
var new_state = stdparam_states[state][key];
110-
highlight_edge(state, new_state);
111-
highlight_state(new_state);
112-
new_states.push([new_state, null]);
113-
}
108+
var new_state = stdparam_states[state];
109+
highlight_edge(state, new_state);
110+
highlight_state(new_state);
111+
new_states.push([new_state, null]);
114112
}
115113
}
116114

0 commit comments

Comments
 (0)