Improve performance

This commit is contained in:
kwaroran
2024-11-03 23:16:51 +09:00
parent b51bbad838
commit c22f6b789a
5 changed files with 71 additions and 35 deletions

View File

@@ -245,12 +245,9 @@ export async function runCharacterJS(arg:{
mode: ScriptMode|'onButtonClick'|'modifyRequestChat'
data: any
}):Promise<any>{
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)
}
}

View File

@@ -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 = ''

View File

@@ -65,9 +65,38 @@ export async function importRegex(o?:customscript[]):Promise<customscript[]>{
}
let bestMatchCache = new Map<string, string>()
let processScriptCache = new Map<string, string>()
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}
}

View File

@@ -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()
})
})