From df4f83eaa7e5a5bf8e8a80fdd8989265e91df73a Mon Sep 17 00:00:00 2001 From: bangonicdd <157843588+bangonicdd2@users.noreply.github.com> Date: Wed, 2 Apr 2025 14:30:23 +0900 Subject: [PATCH 1/4] refactor: regex caching --- src/ts/process/scripts.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index 872779ee..5c48f46c 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -68,12 +68,20 @@ export async function importRegex(o?:customscript[]):Promise{ let bestMatchCache = new Map() let processScriptCache = new Map() -function cacheScript(scripts:customscript[], data:string, result:string, mode:ScriptMode){ - let hash = data + '|||' + mode + '|||' - for(const script of scripts){ - hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}` +function generateScriptCacheKey(scripts: customscript[], data: string, mode: ScriptMode, chatID = -1, cbsConditions: CbsConditions = {}) { + let hash = data + '|||' + mode + '|||'; + for (const script of scripts) { + if(script.type !== mode){ + continue + } + hash += `${script.flag?.includes('') ? + risuChatParser(script.in, { chatID: chatID, cbsConditions }) : + script.in}|||${risuChatParser(script.out, { chatID: chatID, cbsConditions})}|||${script.flag ?? 0}|||${script.ableFlag ? 1 : 0}`; } + return hash; +} +function cacheScript(hash:string, result:string){ processScriptCache.set(hash, result) if(processScriptCache.size > 500){ @@ -82,12 +90,7 @@ function cacheScript(scripts:customscript[], data:string, result:string, mode:Sc } -function getScriptCache(scripts:customscript[], data:string, mode:ScriptMode){ - let hash = data + '|||' + mode + '|||' - for(const script of scripts){ - hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}` - } - +function getScriptCache(hash:string){ return processScriptCache.get(hash) } @@ -98,12 +101,13 @@ export function resetScriptCache(){ export async function processScriptFull(char:character|groupChat|simpleCharacterArgument, data:string, mode:ScriptMode, chatID = -1, cbsConditions:CbsConditions = {}){ let db = getDatabase() const originalData = data - const cached = getScriptCache((db.presetRegex ?? []).concat(char.customscript), originalData, mode) + const scripts = (db.presetRegex ?? []).concat(char.customscript).concat(getModuleRegexScripts()) + const hash = generateScriptCacheKey(scripts, data, mode, chatID, cbsConditions) + const cached = getScriptCache(hash) if(cached){ return {data: cached, emoChanged: false} } let emoChanged = false - const scripts = (db.presetRegex ?? []).concat(char.customscript).concat(getModuleRegexScripts()) data = await runLuaEditTrigger(char, mode, data) if(mode === 'editdisplay'){ @@ -136,7 +140,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter } if(scripts.length === 0){ - cacheScript(scripts, originalData, data, mode) + cacheScript(hash, data) return {data, emoChanged} } function executeScript(pscript:pScript){ @@ -372,7 +376,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter } } - cacheScript(scripts, originalData, data, mode) + cacheScript(hash, data) return {data, emoChanged} } From 1867ea473dc57ae83bf9dfeade56e3f66ebc6b03 Mon Sep 17 00:00:00 2001 From: bangonicdd <157843588+bangonicdd2@users.noreply.github.com> Date: Thu, 3 Apr 2025 11:52:50 +0900 Subject: [PATCH 2/4] fix: increase regex cache size --- src/ts/process/scripts.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index 5c48f46c..d571a415 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -76,7 +76,7 @@ function generateScriptCacheKey(scripts: customscript[], data: string, mode: Scr } hash += `${script.flag?.includes('') ? risuChatParser(script.in, { chatID: chatID, cbsConditions }) : - script.in}|||${risuChatParser(script.out, { chatID: chatID, cbsConditions})}|||${script.flag ?? 0}|||${script.ableFlag ? 1 : 0}`; + script.in}|||${risuChatParser(script.out, { chatID: chatID, cbsConditions})}|||${script.flag ?? ''}|||${script.ableFlag ? 1 : 0}`; } return hash; } @@ -84,7 +84,7 @@ function generateScriptCacheKey(scripts: customscript[], data: string, mode: Scr function cacheScript(hash:string, result:string){ processScriptCache.set(hash, result) - if(processScriptCache.size > 500){ + if(processScriptCache.size > 1000){ processScriptCache.delete(processScriptCache.keys().next().value) } @@ -121,7 +121,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter displayData: data }) - data = d.displayData ?? data + data = d?.displayData ?? data console.log('Trigger time', performance.now() - perf) } catch(e){ @@ -341,6 +341,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter if(db.dynamicAssets && (char.type === 'simple' || char.type === 'character') && char.additionalAssets && char.additionalAssets.length > 0){ if(!db.dynamicAssetsEditDisplay && mode === 'editdisplay'){ + cacheScript(hash, data) return {data, emoChanged} } const assetNames = char.additionalAssets.map((v) => v[0]) From 20e3e9297d2eb78b69bc56a8bf537a12010fc9fa Mon Sep 17 00:00:00 2001 From: bangonicdd <157843588+bangonicdd2@users.noreply.github.com> Date: Sat, 5 Apr 2025 04:59:46 +0900 Subject: [PATCH 3/4] fix: change cache timing after lua trigger --- src/ts/process/scripts.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index d571a415..ccc79f4f 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -102,11 +102,6 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter let db = getDatabase() const originalData = data const scripts = (db.presetRegex ?? []).concat(char.customscript).concat(getModuleRegexScripts()) - const hash = generateScriptCacheKey(scripts, data, mode, chatID, cbsConditions) - const cached = getScriptCache(hash) - if(cached){ - return {data: cached, emoChanged: false} - } let emoChanged = false data = await runLuaEditTrigger(char, mode, data) @@ -138,6 +133,13 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter } } } + + data = risuChatParser(data, { chatID: chatID, cbsConditions }) + const hash = generateScriptCacheKey(scripts, data, mode, chatID, cbsConditions) + const cached = getScriptCache(hash) + if(cached){ + return {data: cached, emoChanged: false} + } if(scripts.length === 0){ cacheScript(hash, data) @@ -146,6 +148,10 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter function executeScript(pscript:pScript){ const script = pscript.script + if(script.in === '' && script.out !== ''){ + return + } + if(script.type === mode){ let outScript2 = script.out.replaceAll("$n", "\n") From 2f425b708494df567363c17d3c6e8fb0ed4b63da Mon Sep 17 00:00:00 2001 From: bangonicdd <157843588+bangonicdd2@users.noreply.github.com> Date: Sat, 5 Apr 2025 05:38:41 +0900 Subject: [PATCH 4/4] fix: code readability slightly --- src/ts/process/scripts.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index ccc79f4f..3c95ab81 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -101,7 +101,6 @@ export function resetScriptCache(){ export async function processScriptFull(char:character|groupChat|simpleCharacterArgument, data:string, mode:ScriptMode, chatID = -1, cbsConditions:CbsConditions = {}){ let db = getDatabase() const originalData = data - const scripts = (db.presetRegex ?? []).concat(char.customscript).concat(getModuleRegexScripts()) let emoChanged = false data = await runLuaEditTrigger(char, mode, data) @@ -135,6 +134,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter } data = risuChatParser(data, { chatID: chatID, cbsConditions }) + const scripts = (db.presetRegex ?? []).concat(char.customscript).concat(getModuleRegexScripts()) const hash = generateScriptCacheKey(scripts, data, mode, chatID, cbsConditions) const cached = getScriptCache(hash) if(cached){ @@ -148,7 +148,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter function executeScript(pscript:pScript){ const script = pscript.script - if(script.in === '' && script.out !== ''){ + if(script.in === ''){ return }