From c22f6b789aca466cb2cea63973ff57fbbdc273a7 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sun, 3 Nov 2024 23:16:51 +0900 Subject: [PATCH] Improve performance --- src/lib/ChatScreens/Chat.svelte | 38 ++++++++++++++++----------------- src/ts/plugins/embedscript.ts | 10 ++++----- src/ts/process/modules.ts | 19 +++++++++-------- src/ts/process/scripts.ts | 32 +++++++++++++++++++++++++++ src/ts/stores.svelte.ts | 7 ++++-- 5 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/lib/ChatScreens/Chat.svelte b/src/lib/ChatScreens/Chat.svelte index 0e7b0b98..ba1d9bec 100644 --- a/src/lib/ChatScreens/Chat.svelte +++ b/src/lib/ChatScreens/Chat.svelte @@ -6,7 +6,7 @@ import { language } from "../../lang"; import { type MessageGenerationInfo } from "../../ts/storage/database.svelte"; import { DBState } from 'src/ts/stores.svelte'; - import { HideIconStore, ReloadGUIPointer, selectedCharID } from "../../ts/stores.svelte"; + import { HideIconStore, ReloadGUIPointer, selIdState } from "../../ts/stores.svelte"; import { translateHTML } from "../../ts/translator/translator"; import { risuChatParser } from "src/ts/process/scripts"; import { get, type Unsubscriber } from "svelte/store"; @@ -18,6 +18,7 @@ import { ColorSchemeTypeStore } from "src/ts/gui/colorscheme"; import { ConnectionOpenStore } from "src/ts/sync/multiuser"; import { onDestroy, onMount } from "svelte"; + import { PerformanceDebugger } from "src/ts/globalApi.svelte"; let translating = $state(false) let editMode = $state(false) let statusMessage:string = $state('') @@ -55,12 +56,12 @@ let msgDisplay = $state('') let translated = $state(DBState.db.autoTranslate) - let role = $derived(DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx]?.role) + let role = $derived(DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx]?.role) async function rm(e:MouseEvent, rec?:boolean){ if(e.shiftKey){ - let msg = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message + let msg = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message msg = msg.slice(0, idx) - DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message = msg + DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message = msg return } @@ -68,32 +69,32 @@ if(rm){ if(DBState.db.instantRemove || rec){ const r = await alertConfirm(language.instantRemoveConfirm) - let msg = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message + let msg = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message if(!r){ msg = msg.slice(0, idx) } else{ msg.splice(idx, 1) } - DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message = msg + DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message = msg } else{ - let msg = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message + let msg = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message msg.splice(idx, 1) - DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message = msg + DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message = msg } } } async function edit(){ - DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].data = message + DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].data = message } function getCbsCondition(){ try{ const cbsConditions:CbsConditions = { firstmsg: firstMessage ?? false, - chatRole: DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage]?.message?.[idx]?.role ?? null, + chatRole: DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage]?.message?.[idx]?.role ?? null, } return cbsConditions } @@ -106,6 +107,7 @@ } function displaya(message:string){ + const perf = performance.now() msgDisplay = risuChatParser(message, {chara: name, chatID: idx, rmVar: true, visualize: true, cbsConditions: getCbsCondition()}) } @@ -135,7 +137,6 @@ lastChatId = chatID translateText = false try { - console.log('Checking autoTranslate') if(DBState.db.autoTranslate){ translateText = true } @@ -147,7 +148,6 @@ console.error(error) } } - console.log(`Translattext: ${translateText}, autoTranslate: ${DBState.db.autoTranslate}`) if(translateText){ if(!DBState.db.legacyTranslation){ const marked = await ParseMarkdown(data, charArg, 'pretranslate', chatID, getCbsCondition()) @@ -284,7 +284,7 @@ {/if} {#if idx > -1} - {#if DBState.db.characters[$selectedCharID].type !== 'group' && DBState.db.characters[$selectedCharID].ttsMode !== 'none' && (DBState.db.characters[$selectedCharID].ttsMode)} + {#if DBState.db.characters[selIdState.selId].type !== 'group' && DBState.db.characters[selIdState.selId].ttsMode !== 'none' && (DBState.db.characters[selIdState.selId].ttsMode)} {:else if !blankMessage && !$HideIconStore} diff --git a/src/ts/plugins/embedscript.ts b/src/ts/plugins/embedscript.ts index 8b260b8c..a2db413d 100644 --- a/src/ts/plugins/embedscript.ts +++ b/src/ts/plugins/embedscript.ts @@ -245,12 +245,9 @@ export async function runCharacterJS(arg:{ mode: ScriptMode|'onButtonClick'|'modifyRequestChat' data: any }):Promise{ + const perf = performance.now() try { - if(arg.code === null){ - const db = getDatabase() - const selectedChar = get(selectedCharID) - arg.code = db.characters[selectedChar].virtualscript - } + arg.code = arg.code ?? '' const codes = { "editinput": 'editInput', "editoutput": 'editOutput', @@ -320,5 +317,8 @@ export async function runCharacterJS(arg:{ } return arg.data } + finally{ + console.log('runCharacterJS',performance.now() - perf) + } } \ No newline at end of file diff --git a/src/ts/process/modules.ts b/src/ts/process/modules.ts index f430ec5b..6f21d032 100644 --- a/src/ts/process/modules.ts +++ b/src/ts/process/modules.ts @@ -8,7 +8,8 @@ import { convertExternalLorebook } from "./lorebook.svelte" import { decodeRPack, encodeRPack } from "../rpack/rpack_bg" import { convertImage } from "../parser.svelte" import { Capacitor } from "@capacitor/core" -import { DBState, HideIconStore, moduleBackgroundEmbedding } from "../stores.svelte" +import { HideIconStore, moduleBackgroundEmbedding, ReloadGUIPointer } from "../stores.svelte" +import {get} from "svelte/store" export interface RisuModule{ name: string @@ -395,18 +396,18 @@ export async function applyModule() { alertNormal(language.successApplyModule) } -let lastGlobalEnabledModules: string[] = [] -let lastChatEnabledModules: string[] = [] +let lastModuleIds:string = '' export function moduleUpdate(){ - if(!Array.isArray(lastGlobalEnabledModules)){ - lastGlobalEnabledModules = [] - } - if(!Array.isArray(lastChatEnabledModules)){ - lastChatEnabledModules = [] - } + const m = getModules() + + const ids = m.map((m) => m.id).join('-') + if(lastModuleIds !== ids){ + ReloadGUIPointer.set(get(ReloadGUIPointer) + 1) + lastModuleIds = ids + } let moduleHideIcon = false let backgroundEmbedding = '' diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index f69b8489..f7e7e2e5 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -65,9 +65,38 @@ export async function importRegex(o?:customscript[]):Promise{ } let bestMatchCache = new Map() +let processScriptCache = new Map() + +function cacheScript(scripts:customscript[], data:string, result:string){ + let hash = data + '|||' + for(const script of scripts){ + hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}` + } + + processScriptCache.set(hash, result) + +} + +function getScriptCache(scripts:customscript[], data:string){ + let hash = data + '|||' + for(const script of scripts){ + hash += `${script.in}|||${script.out}|||${script.flag}|||${script.ableFlag}|||${script.type}` + } + + return processScriptCache.get(hash) +} + +export function resetScriptCache(){ + processScriptCache = new Map() +} 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.globalscript ?? []).concat(char.customscript), originalData) + if(cached){ + return {data: cached, emoChanged: false} + } let emoChanged = false const scripts = (db.globalscript ?? []).concat(char.customscript).concat(getModuleRegexScripts()) data = await runCharacterJS({ @@ -77,6 +106,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter }) data = await runLuaEditTrigger(char, mode, data) if(scripts.length === 0){ + cacheScript(scripts, originalData, data) return {data, emoChanged} } function executeScript(pscript:pScript){ @@ -311,6 +341,8 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter } } + cacheScript(scripts, originalData, data) + return {data, emoChanged} } diff --git a/src/ts/stores.svelte.ts b/src/ts/stores.svelte.ts index 9edec75c..fd4fa156 100644 --- a/src/ts/stores.svelte.ts +++ b/src/ts/stores.svelte.ts @@ -3,6 +3,7 @@ import type { character, Database, groupChat } from "./storage/database.svelte"; import type { simpleCharacterArgument } from "./parser.svelte"; import type { alertData } from "./alert"; import { getModules, moduleUpdate } from "./process/modules"; +import { resetScriptCache } from "./process/scripts"; function updateSize(){ SizeStore.set({ @@ -95,6 +96,10 @@ export const DBState = $state({ export const disableHighlight = writable(true) +ReloadGUIPointer.subscribe(() => { + resetScriptCache() +}) + $effect.root(() => { selectedCharID.subscribe((v) => { selIdState.selId = v @@ -106,8 +111,6 @@ $effect.root(() => { DBState?.db?.characters?.[selIdState.selId]?.chats?.[DBState?.db?.characters?.[selIdState.selId]?.chatPage]?.modules?.length DBState?.db?.characters?.[selIdState.selId]?.hideChatIcon DBState?.db?.moduleIntergration - ReloadGUIPointer.set(get(ReloadGUIPointer) + 1) moduleUpdate() - }) }) \ No newline at end of file