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: [PATCH] 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 +++++++++-- types/ext/language-transformer-internal.d.ts | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ext/js/language/language-transformer.js b/ext/js/language/language-transformer.js index c9e261eae4..7ad1895f5f 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; } diff --git a/types/ext/language-transformer-internal.d.ts b/types/ext/language-transformer-internal.d.ts index cb0899e91f..c186eeca4b 100644 --- a/types/ext/language-transformer-internal.d.ts +++ b/types/ext/language-transformer-internal.d.ts @@ -18,6 +18,7 @@ export type Transform = { name: string; rules: Rule[]; + suffixHeuristic: RegExp; }; export type Rule = {