Skip to content

Commit

Permalink
Reduce allocations and retentions in Journey TransitionTable
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
byroot committed Aug 3, 2021
1 parent c3664b0 commit e2a83cd
Showing 1 changed file with 6 additions and 2 deletions.
Expand Up @@ -11,7 +11,7 @@ class TransitionTable # :nodoc:
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 = {}
Expand Down Expand Up @@ -165,7 +165,11 @@ def []=(from, to, sym)
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
Expand Down

0 comments on commit e2a83cd

Please sign in to comment.