diff --git a/public/scripts/extensions/regex/engine.js b/public/scripts/extensions/regex/engine.js index 028477be2..53e5ce296 100644 --- a/public/scripts/extensions/regex/engine.js +++ b/public/scripts/extensions/regex/engine.js @@ -34,6 +34,61 @@ export const SCRIPT_TYPE_UNKNOWN = -1; */ const DEFAULT_GET_REGEX_SCRIPTS_OPTIONS = Object.freeze({ allowedOnly: false }); +/** + * Manages the compiled regex cache with LRU eviction. + */ +export class RegexProvider { + /** @type {Map} */ + #cache = new Map(); + /** @type {number} */ + #maxSize = 1000; + + static instance = new RegexProvider(); + + /** + * Gets a regex instance by its string representation. + * @param {string} regexString The regex string to retrieve + * @returns {RegExp?} Compiled regex or null if invalid + */ + get(regexString) { + const isCached = this.#cache.has(regexString); + const regex = isCached + ? this.#cache.get(regexString) + : regexFromString(regexString); + + if (!regex) { + return null; + } + + if (isCached) { + // LRU: Move to end by re-inserting + this.#cache.delete(regexString); + this.#cache.set(regexString, regex); + } else { + // Evict oldest if at capacity + if (this.#cache.size >= this.#maxSize) { + const firstKey = this.#cache.keys().next().value; + this.#cache.delete(firstKey); + } + this.#cache.set(regexString, regex); + } + + // Reset lastIndex for global/sticky regexes + if (regex.global || regex.sticky) { + regex.lastIndex = 0; + } + + return regex; + } + + /** + * Clears the entire cache. + */ + clear() { + this.#cache.clear(); + } +} + /** * Retrieves the list of regex scripts by combining the scripts from the extension settings and the character data * @@ -353,7 +408,7 @@ export function runRegexScript(regexScript, rawString, { characterOverride } = { } }; const regexString = getRegexString(); - const findRegex = regexFromString(regexString); + const findRegex = RegexProvider.instance.get(regexString); // The user skill issued. Return with nothing. if (!findRegex) { diff --git a/public/scripts/extensions/regex/index.js b/public/scripts/extensions/regex/index.js index d490cd476..4a52fa178 100644 --- a/public/scripts/extensions/regex/index.js +++ b/public/scripts/extensions/regex/index.js @@ -8,7 +8,7 @@ import { commonEnumProviders, enumIcons } from '../../slash-commands/SlashComman import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js'; import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; import { download, equalsIgnoreCaseAndAccents, escapeHtml, getFileText, getSortableDelay, isFalseBoolean, isTrueBoolean, regexFromString, setInfoBlock, uuidv4 } from '../../utils.js'; -import { allowPresetScripts, allowScopedScripts, disallowPresetScripts, disallowScopedScripts, getCurrentPresetAPI, getCurrentPresetName, getRegexScripts, getScriptsByType, isPresetScriptsAllowed, isScopedScriptsAllowed, regex_placement, runRegexScript, saveScriptsByType, SCRIPT_TYPE_UNKNOWN, SCRIPT_TYPES, substitute_find_regex } from './engine.js'; +import { allowPresetScripts, allowScopedScripts, disallowPresetScripts, disallowScopedScripts, getCurrentPresetAPI, getCurrentPresetName, getRegexScripts, getScriptsByType, isPresetScriptsAllowed, isScopedScriptsAllowed, regex_placement, RegexProvider, runRegexScript, saveScriptsByType, SCRIPT_TYPE_UNKNOWN, SCRIPT_TYPES, substitute_find_regex } from './engine.js'; import { t } from '../../i18n.js'; import { accountStorage } from '../../util/AccountStorage.js'; import { getPresetManager } from '../../preset-manager.js'; @@ -1628,6 +1628,8 @@ async function checkCharEmbeddedRegexScripts() { } } + // Clear cache and reload scripts + RegexProvider.instance.clear(); await loadRegexScripts(); }