From 509489441200f21d4b7dc2610ea0b2ef2b16a36d Mon Sep 17 00:00:00 2001 From: Claiyc Date: Tue, 13 Dec 2022 19:38:49 +0100 Subject: [PATCH 1/3] remove substring caching --- src/proc/regex/RegexHandler.ts | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) 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(); From 315f1d757318cd47906775e58c2ef13385ee9087 Mon Sep 17 00:00:00 2001 From: Claiyc Date: Tue, 13 Dec 2022 19:44:06 +0100 Subject: [PATCH 2/3] add caching of generated matches --- src/proc/regex/Regex.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/proc/regex/Regex.ts b/src/proc/regex/Regex.ts index 1648afe..c18b64c 100644 --- a/src/proc/regex/Regex.ts +++ b/src/proc/regex/Regex.ts @@ -13,6 +13,8 @@ export abstract class Regex { rating: number; match: { index: number; element: string }; }; + protected lastRegex: RegExp; // used for checking whether regex has changed -> regenerating matches + protected generatedMatches: string[]; // generated matches protected constructor() { this.id = 0; @@ -23,6 +25,8 @@ export abstract class Regex { this.matchesNum = 10000; this.substrings = []; this.lastBestMatch = { rating: -1, match: { index: -1, element: '' } }; + this.lastRegex = new RegExp(''); + this.generatedMatches = []; } public getId(): number { @@ -195,9 +199,13 @@ export abstract class Regex { }); break; case Matching.APPROX: + if (this.generatedMatches.length === 0 || this.lastRegex.toString() !== this.regex.toString()) { + this.generatedMatches = this.genMatches(); + this.lastRegex = this.regex; + } bestMatch = RegexHandler.approxMatching( this.substrings, - this.genMatches() + this.generatedMatches ); break; } From d38d5936f3687b4a5c4ef5c36d346749cbc56b63 Mon Sep 17 00:00:00 2001 From: Claiyc Date: Tue, 13 Dec 2022 20:08:16 +0100 Subject: [PATCH 3/3] regenerate matches if matchesNum changed --- src/proc/regex/Regex.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/proc/regex/Regex.ts b/src/proc/regex/Regex.ts index c18b64c..97f6df2 100644 --- a/src/proc/regex/Regex.ts +++ b/src/proc/regex/Regex.ts @@ -14,6 +14,7 @@ export abstract class Regex { 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() { @@ -26,6 +27,7 @@ export abstract class Regex { this.substrings = []; this.lastBestMatch = { rating: -1, match: { index: -1, element: '' } }; this.lastRegex = new RegExp(''); + this.lastMatchesNum = this.matchesNum; this.generatedMatches = []; } @@ -199,9 +201,10 @@ export abstract class Regex { }); break; case Matching.APPROX: - if (this.generatedMatches.length === 0 || this.lastRegex.toString() !== this.regex.toString()) { + 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,