From 62244ffeae129207cd2b677f8310aab5e2c1a1e9 Mon Sep 17 00:00:00 2001 From: Claiyc <58397976+Claiyc@users.noreply.github.com> Date: Tue, 13 Dec 2022 20:19:24 +0100 Subject: [PATCH] fix(regex): remove substring caching & add genMatches caching (#68) --- src/proc/regex/Regex.ts | 13 ++++++++++++- src/proc/regex/RegexHandler.ts | 32 ++++++++++++++------------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/proc/regex/Regex.ts b/src/proc/regex/Regex.ts index 1648afe..97f6df2 100644 --- a/src/proc/regex/Regex.ts +++ b/src/proc/regex/Regex.ts @@ -13,6 +13,9 @@ export abstract class Regex { rating: number; match: { index: number; element: string }; }; + protected lastRegex: RegExp; // used for checking whether regex has changed -> regenerating matches + protected lastMatchesNum: number; // used for checking whether matchesNum has changed -> regenerating matches + protected generatedMatches: string[]; // generated matches protected constructor() { this.id = 0; @@ -23,6 +26,9 @@ export abstract class Regex { this.matchesNum = 10000; this.substrings = []; this.lastBestMatch = { rating: -1, match: { index: -1, element: '' } }; + this.lastRegex = new RegExp(''); + this.lastMatchesNum = this.matchesNum; + this.generatedMatches = []; } public getId(): number { @@ -195,9 +201,14 @@ export abstract class Regex { }); break; case Matching.APPROX: + if (this.generatedMatches.length === 0 || this.lastRegex.toString() !== this.regex.toString() || this.matchesNum !== this.lastMatchesNum) { + this.generatedMatches = this.genMatches(); + this.lastRegex = this.regex; + this.lastMatchesNum = this.matchesNum; + } bestMatch = RegexHandler.approxMatching( this.substrings, - this.genMatches() + this.generatedMatches ); break; } diff --git a/src/proc/regex/RegexHandler.ts b/src/proc/regex/RegexHandler.ts index 23be3e6..b07c096 100644 --- a/src/proc/regex/RegexHandler.ts +++ b/src/proc/regex/RegexHandler.ts @@ -44,14 +44,12 @@ export class RegexHandler { // set required substrings for constraint regex & apply similarity conversion for (let i = 0; i < constraintRegex.length; i++) { - if (constraintRegex[i].getSubstrings().length === 0) { // if substrings are not already set - if (constraintRegex[i].getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) { - constraintRegex[i].setSubstrings(allSubstrings.slice()); // clone array - } else if (constraintRegex[i].getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) { - constraintRegex[i].setSubstrings(spacesSubstrings.slice()); // clone array - } else { - constraintRegex[i].genSubstrings(data); - } + if (constraintRegex[i].getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) { + constraintRegex[i].setSubstrings(allSubstrings.slice()); // clone array + } else if (constraintRegex[i].getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) { + constraintRegex[i].setSubstrings(spacesSubstrings.slice()); // clone array + } else { + constraintRegex[i].genSubstrings(data); } if (constraintRegex[i].getSlicing() === Slicing.SUBSTR) { allSubstrings = constraintRegex[i].getSubstrings(); // cache substrings @@ -89,18 +87,16 @@ export class RegexHandler { } // set required substrings for value regex & apply similarity conversion - if (valueRegex.getSubstrings().length === 0) { // if substrings are not already set - if (highestLowIndex === 0 && lowestHighIndex === data.length) { // if no constraint regex - if (valueRegex.getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) { - valueRegex.setSubstrings(allSubstrings.slice()); // clone array - } else if (valueRegex.getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) { - valueRegex.setSubstrings(spacesSubstrings.slice()); // clone array - } else { - valueRegex.genSubstrings(data); - } + if (highestLowIndex === 0 && lowestHighIndex === data.length) { // if no constraint regex + if (valueRegex.getSlicing() === Slicing.SUBSTR && allSubstrings.length !== 0) { + valueRegex.setSubstrings(allSubstrings.slice()); // clone array + } else if (valueRegex.getSlicing() === Slicing.SPACES && spacesSubstrings.length !== 0) { + valueRegex.setSubstrings(spacesSubstrings.slice()); // clone array } else { - valueRegex.genSubstrings(data.slice(highestLowIndex, lowestHighIndex)); + valueRegex.genSubstrings(data); } + } else { + valueRegex.genSubstrings(data.slice(highestLowIndex, lowestHighIndex)); } valueRegex.applySimilarity();