Skip to content

Commit

Permalink
Transformation performance optimizations (FooSoft#645)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Darius Jahandarie <[email protected]>
  • Loading branch information
3 people authored Feb 7, 2024
1 parent 356adeb commit 0e9c28f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions ext/js/language/language-transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions types/ext/language-transformer-internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
export type Transform = {
name: string;
rules: Rule[];
suffixHeuristic: RegExp;
};

export type Rule = {
Expand Down

0 comments on commit 0e9c28f

Please sign in to comment.