diff options
author | Cashew <52880648+Scrub1492@users.noreply.github.com> | 2024-02-07 17:54:49 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-07 10:54:49 +0000 |
commit | 0e9c28f9c713421c23e80a3a55d5233dd36d08de (patch) | |
tree | eb1074122211b325fc98cdec49c53bd078d34a94 /ext | |
parent | 356adeb2dc482de3eee9af8ea116095c3a2d0d42 (diff) |
Transformation performance optimizations (#645)
* perform transformation bounds
* fix wrong continue
* fix wrong undefined check
* regex performance
* suffixHeuristic
* escape RegExp
* fix escapeRegExp
* move destructuring after suffixHeuristic check
---------
Co-authored-by: Stefan Vukovic <stefanvukovic44@gmail.com>
Co-authored-by: Darius Jahandarie <djahandarie@gmail.com>
Diffstat (limited to 'ext')
-rw-r--r-- | ext/js/language/language-transformer.js | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/ext/js/language/language-transformer.js b/ext/js/language/language-transformer.js index c9e261ea..7ad1895f 100644 --- a/ext/js/language/language-transformer.js +++ b/ext/js/language/language-transformer.js @@ -15,6 +15,8 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +import {escapeRegExp} from '../core/utilities.js'; + export class LanguageTransformer { constructor() { /** @type {number} */ @@ -65,7 +67,9 @@ export class LanguageTransformer { conditionsOut: conditionFlagsOut }); } - transforms2.push({name, rules: rules2}); + const suffixes = rules.map((rule) => rule.suffixIn); + const suffixHeuristic = new RegExp(`(${suffixes.map((suffix) => escapeRegExp(suffix)).join('|')})$`); + transforms2.push({name, rules: rules2, suffixHeuristic}); } this._nextFlagIndex = nextFlagIndex; @@ -133,7 +137,10 @@ export class LanguageTransformer { const results = [this._createTransformedText(sourceText, 0, [])]; for (let i = 0; i < results.length; ++i) { const {text, conditions, trace} = results[i]; - for (const {name, rules} of this._transforms) { + for (const transform of this._transforms) { + if (!transform.suffixHeuristic.test(text)) { continue; } + + const {name, rules} = transform; for (let j = 0, jj = rules.length; j < jj; ++j) { const rule = rules[j]; if (!LanguageTransformer.conditionsMatch(conditions, rule.conditionsIn)) { continue; } |