Skip to content

Commit

Permalink
Speed up INTERPOLATION_SYNTAX
Browse files Browse the repository at this point in the history
same as tokenizer change:
from regex101.com pcre2 debugger:

```ruby

/(%)?(%\{([^\}]+)\})/ =~ '%{{'*9999)+'}'

/(%)?(%\{([^\}]+)\})/ ==> 199,984 steps
/(%%?)\{([^\}]+)\}/   ==> 129,989 steps

/(%%?\{[^\}]+\})/     ==>  99,992 steps
```

So the extra capture group is the main hit.
  • Loading branch information
kbrock committed Jun 13, 2023
1 parent c5f9f49 commit 835a7d7
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/i18n/backend/interpolation_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Compiler
extend self

TOKENIZER = /(%%?\{[^}]+\})/
INTERPOLATION_SYNTAX_PATTERN = /(%)?(%\{([^\}]+)\})/
INTERPOLATION_SYNTAX_PATTERN = /(%%?)\{([^}]+)\}/

def compile_if_an_interpolation(string)
if interpolated_str?(string)
Expand Down Expand Up @@ -53,8 +53,8 @@ def compiled_interpolation_body(str)
end

def handle_interpolation_token(interpolation, matchdata)
escaped, pattern, key = matchdata.values_at(1, 2, 3)
escaped ? pattern : compile_interpolation_token(key.to_sym)
escaped, key = matchdata.values_at(1, 2)
escaped == '%%' ? "%{#{key}}" : compile_interpolation_token(key.to_sym)
end

def compile_interpolation_token(key)
Expand Down

0 comments on commit 835a7d7

Please sign in to comment.