From 835a7d77da9624b25ef85153afcb497f962f39a5 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 12 Jun 2023 23:12:36 -0400 Subject: [PATCH 1/2] Speed up INTERPOLATION_SYNTAX 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. --- lib/i18n/backend/interpolation_compiler.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/i18n/backend/interpolation_compiler.rb b/lib/i18n/backend/interpolation_compiler.rb index 29444ac3..360d44ce 100644 --- a/lib/i18n/backend/interpolation_compiler.rb +++ b/lib/i18n/backend/interpolation_compiler.rb @@ -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) @@ -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) From 89407818ee8dce1308616aed6107720966483468 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 12 Jun 2023 23:19:50 -0400 Subject: [PATCH 2/2] condense to TOKENIZER --- lib/i18n/backend/interpolation_compiler.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/i18n/backend/interpolation_compiler.rb b/lib/i18n/backend/interpolation_compiler.rb index 360d44ce..e37b6799 100644 --- a/lib/i18n/backend/interpolation_compiler.rb +++ b/lib/i18n/backend/interpolation_compiler.rb @@ -21,8 +21,7 @@ module InterpolationCompiler module Compiler extend self - TOKENIZER = /(%%?\{[^}]+\})/ - INTERPOLATION_SYNTAX_PATTERN = /(%%?)\{([^}]+)\}/ + TOKENIZER = /(%%?\{[^}]+\})/ def compile_if_an_interpolation(string) if interpolated_str?(string) @@ -37,7 +36,7 @@ def i18n_interpolate(v = {}) end def interpolated_str?(str) - str.kind_of?(::String) && str =~ INTERPOLATION_SYNTAX_PATTERN + str.kind_of?(::String) && str =~ TOKENIZER end protected @@ -48,13 +47,12 @@ def tokenize(str) def compiled_interpolation_body(str) tokenize(str).map do |token| - (matchdata = token.match(INTERPOLATION_SYNTAX_PATTERN)) ? handle_interpolation_token(token, matchdata) : escape_plain_str(token) + token.match(TOKENIZER) ? handle_interpolation_token(token) : escape_plain_str(token) end.join end - def handle_interpolation_token(interpolation, matchdata) - escaped, key = matchdata.values_at(1, 2) - escaped == '%%' ? "%{#{key}}" : compile_interpolation_token(key.to_sym) + def handle_interpolation_token(token) + token.start_with?('%%') ? token[1..] : compile_interpolation_token(token[2..-2]) end def compile_interpolation_token(key)