Regex cache (#4858)

feat: optimize regex extension performance

- Implemented caching mechanism for regex scripts to improve message rendering speed.
- Added automatic cache invalidation on script updates.
- Optimized regex compilation to reduce CPU usage.

---------

Co-authored-by: GhostXia <33112711+GhostXia@users.noreply.github.com>
This commit is contained in:
Cohee
2025-12-04 22:21:13 +02:00
committed by GitHub
co-authored by GhostXia
parent bd1583faa4
commit 0225562f4e
2 changed files with 59 additions and 2 deletions
+56 -1
View File
@@ -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<string, RegExp>} */
#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) {
+3 -1
View File
@@ -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();
}