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>
Signed| @@ -35,6 +35,61 @@ export const SCRIPT_TYPE_UNKNOWN = -1; | |||
| 35 | const DEFAULT_GET_REGEX_SCRIPTS_OPTIONS = Object.freeze({ allowedOnly: false }); | 35 | const DEFAULT_GET_REGEX_SCRIPTS_OPTIONS = Object.freeze({ allowedOnly: false }); |
| 36 | 36 | ||
| 37 | /** | 37 | /** |
| 38 | * Manages the compiled regex cache with LRU eviction. | ||
| 39 | */ | ||
| 40 | export class RegexProvider { | ||
| 41 | /** @type {Map<string, RegExp>} */ | ||
| 42 | #cache = new Map(); | ||
| 43 | /** @type {number} */ | ||
| 44 | #maxSize = 1000; | ||
| 45 | |||
| 46 | static instance = new RegexProvider(); | ||
| 47 | |||
| 48 | /** | ||
| 49 | * Gets a regex instance by its string representation. | ||
| 50 | * @param {string} regexString The regex string to retrieve | ||
| 51 | * @returns {RegExp?} Compiled regex or null if invalid | ||
| 52 | */ | ||
| 53 | get(regexString) { | ||
| 54 | const isCached = this.#cache.has(regexString); | ||
| 55 | const regex = isCached | ||
| 56 | ? this.#cache.get(regexString) | ||
| 57 | : regexFromString(regexString); | ||
| 58 | |||
| 59 | if (!regex) { | ||
| 60 | return null; | ||
| 61 | } | ||
| 62 | |||
| 63 | if (isCached) { | ||
| 64 | // LRU: Move to end by re-inserting | ||
| 65 | this.#cache.delete(regexString); | ||
| 66 | this.#cache.set(regexString, regex); | ||
| 67 | } else { | ||
| 68 | // Evict oldest if at capacity | ||
| 69 | if (this.#cache.size >= this.#maxSize) { | ||
| 70 | const firstKey = this.#cache.keys().next().value; | ||
| 71 | this.#cache.delete(firstKey); | ||
| 72 | } | ||
| 73 | this.#cache.set(regexString, regex); | ||
| 74 | } | ||
| 75 | |||
| 76 | // Reset lastIndex for global/sticky regexes | ||
| 77 | if (regex.global || regex.sticky) { | ||
| 78 | regex.lastIndex = 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | return regex; | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Clears the entire cache. | ||
| 86 | */ | ||
| 87 | clear() { | ||
| 88 | this.#cache.clear(); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | /** | ||
| 38 | * Retrieves the list of regex scripts by combining the scripts from the extension settings and the character data | 93 | * Retrieves the list of regex scripts by combining the scripts from the extension settings and the character data |
| 39 | * | 94 | * |
| 40 | * @param {GetRegexScriptsOptions} options Options for retrieving the regex scripts | 95 | * @param {GetRegexScriptsOptions} options Options for retrieving the regex scripts |
| @@ -353,7 +408,7 @@ export function runRegexScript(regexScript, rawString, { characterOverride } = { | |||
| 353 | } | 408 | } |
| 354 | }; | 409 | }; |
| 355 | const regexString = getRegexString(); | 410 | const regexString = getRegexString(); |
| 356 | const findRegex = regexFromString(regexString); | 411 | const findRegex = RegexProvider.instance.get(regexString); |
| 357 | 412 | ||
| 358 | // The user skill issued. Return with nothing. | 413 | // The user skill issued. Return with nothing. |
| 359 | if (!findRegex) { | 414 | if (!findRegex) { |
| @@ -8,7 +8,7 @@ import { commonEnumProviders, enumIcons } from '../../slash-commands/SlashComman | |||
| 8 | import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js'; | 8 | import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js'; |
| 9 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; | 9 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; |
| 10 | import { download, equalsIgnoreCaseAndAccents, escapeHtml, getFileText, getSortableDelay, isFalseBoolean, isTrueBoolean, regexFromString, setInfoBlock, uuidv4 } from '../../utils.js'; | 10 | import { download, equalsIgnoreCaseAndAccents, escapeHtml, getFileText, getSortableDelay, isFalseBoolean, isTrueBoolean, regexFromString, setInfoBlock, uuidv4 } from '../../utils.js'; |
| 11 | 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'; | 11 | 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'; |
| 12 | import { t } from '../../i18n.js'; | 12 | import { t } from '../../i18n.js'; |
| 13 | import { accountStorage } from '../../util/AccountStorage.js'; | 13 | import { accountStorage } from '../../util/AccountStorage.js'; |
| 14 | import { getPresetManager } from '../../preset-manager.js'; | 14 | import { getPresetManager } from '../../preset-manager.js'; |
| @@ -1628,6 +1628,8 @@ async function checkCharEmbeddedRegexScripts() { | |||
| 1628 | } | 1628 | } |
| 1629 | } | 1629 | } |
| 1630 | 1630 | ||
| 1631 | // Clear cache and reload scripts | ||
| 1632 | RegexProvider.instance.clear(); | ||
| 1631 | await loadRegexScripts(); | 1633 | await loadRegexScripts(); |
| 1632 | } | 1634 | } |
| 1633 | 1635 | ||