From 0e9c28f9c713421c23e80a3a55d5233dd36d08de Mon Sep 17 00:00:00 2001 From: Cashew <52880648+Scrub1492@users.noreply.github.com> Date: Wed, 7 Feb 2024 17:54:49 +0700 Subject: 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 Co-authored-by: Darius Jahandarie --- ext/js/language/language-transformer.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'ext/js/language') 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 . */ +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; } -- cgit v1.2.3