1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reduce allocations and retentions in Journey TransitionTable

`DEFAULT_EXP` is by far the most common regexp being anchored.
By special casing it, we can re-use the existing `DEFAULT_EXP_ANCHORED`
regexp, and avoid keeping lots of copies of it in memory. Every time
we hit, we also avoid having to compile the regexp, so it's faster.

We also replace the `Regexp.new(/\A#{sym}\Z/)` pattern by just
`/\A#{sym}\Z/`, as it was compiling and instantiating an extra
regexp for no good reason.
This commit is contained in:
Jean Boussier 2021-08-03 12:53:54 +02:00
parent c3664b0c2b
commit e2a83cd9bd

View file

@ -11,7 +11,7 @@ module ActionDispatch
attr_reader :memos
DEFAULT_EXP = /[^.\/?]+/
DEFAULT_EXP_ANCHORED = Regexp.new(/\A#{DEFAULT_EXP}\Z/)
DEFAULT_EXP_ANCHORED = /\A#{DEFAULT_EXP}\Z/
def initialize
@stdparam_states = {}
@ -165,7 +165,11 @@ module ActionDispatch
case sym
when Regexp
# we must match the whole string to a token boundary
sym = Regexp.new(/\A#{sym}\Z/)
if sym == DEFAULT_EXP
sym = DEFAULT_EXP_ANCHORED
else
sym = /\A#{sym}\Z/
end
when Symbol
# account for symbols in the constraints the same as strings
sym = sym.to_s