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

@@ -6,7 +6,7 @@
import { language } from "../../lang"; import { language } from "../../lang";
import { type MessageGenerationInfo } from "../../ts/storage/database.svelte"; import { type MessageGenerationInfo } from "../../ts/storage/database.svelte";
import { DBState } from 'src/ts/stores.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 { translateHTML } from "../../ts/translator/translator";
import { risuChatParser } from "src/ts/process/scripts"; import { risuChatParser } from "src/ts/process/scripts";
import { get, type Unsubscriber } from "svelte/store"; import { get, type Unsubscriber } from "svelte/store";
@@ -18,6 +18,7 @@
import { ColorSchemeTypeStore } from "src/ts/gui/colorscheme"; import { ColorSchemeTypeStore } from "src/ts/gui/colorscheme";
import { ConnectionOpenStore } from "src/ts/sync/multiuser"; import { ConnectionOpenStore } from "src/ts/sync/multiuser";
import { onDestroy, onMount } from "svelte"; import { onDestroy, onMount } from "svelte";
import { PerformanceDebugger } from "src/ts/globalApi.svelte";
let translating = $state(false) let translating = $state(false)
let editMode = $state(false) let editMode = $state(false)
let statusMessage:string = $state('') let statusMessage:string = $state('')
@@ -55,12 +56,12 @@
let msgDisplay = $state('') let msgDisplay = $state('')
let translated = $state(DBState.db.autoTranslate) 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){ async function rm(e:MouseEvent, rec?:boolean){
if(e.shiftKey){ 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) 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 return
} }
@@ -68,32 +69,32 @@
if(rm){ if(rm){
if(DBState.db.instantRemove || rec){ if(DBState.db.instantRemove || rec){
const r = await alertConfirm(language.instantRemoveConfirm) 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){ if(!r){
msg = msg.slice(0, idx) msg = msg.slice(0, idx)
} }
else{ else{
msg.splice(idx, 1) 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{ 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) 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(){ 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(){ function getCbsCondition(){
try{ try{
const cbsConditions:CbsConditions = { const cbsConditions:CbsConditions = {
firstmsg: firstMessage ?? false, 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 return cbsConditions
} }
@@ -106,6 +107,7 @@
} }
function displaya(message:string){ function displaya(message:string){
const perf = performance.now()
msgDisplay = risuChatParser(message, {chara: name, chatID: idx, rmVar: true, visualize: true, cbsConditions: getCbsCondition()}) msgDisplay = risuChatParser(message, {chara: name, chatID: idx, rmVar: true, visualize: true, cbsConditions: getCbsCondition()})
} }
@@ -135,7 +137,6 @@
lastChatId = chatID lastChatId = chatID
translateText = false translateText = false
try { try {
console.log('Checking autoTranslate')
if(DBState.db.autoTranslate){ if(DBState.db.autoTranslate){
translateText = true translateText = true
} }
@@ -147,7 +148,6 @@
console.error(error) console.error(error)
} }
} }
console.log(`Translattext: ${translateText}, autoTranslate: ${DBState.db.autoTranslate}`)
if(translateText){ if(translateText){
if(!DBState.db.legacyTranslation){ if(!DBState.db.legacyTranslation){
const marked = await ParseMarkdown(data, charArg, 'pretranslate', chatID, getCbsCondition()) const marked = await ParseMarkdown(data, charArg, 'pretranslate', chatID, getCbsCondition())
@@ -284,7 +284,7 @@
</button> </button>
{/if} {/if}
{#if idx > -1} {#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)}
<button class="ml-2 hover:text-blue-500 transition-colors" onclick={()=>{ <button class="ml-2 hover:text-blue-500 transition-colors" onclick={()=>{
return sayTTS(null, message) return sayTTS(null, message)
}}> }}>
@@ -335,7 +335,7 @@
{#snippet icon(options:{rounded?:boolean,styleFix?:string} = {})} {#snippet icon(options:{rounded?:boolean,styleFix?:string} = {})}
{#if !blankMessage && !$HideIconStore} {#if !blankMessage && !$HideIconStore}
{#if DBState.db.characters[$selectedCharID]?.chaId === "§playground"} {#if DBState.db.characters[selIdState.selId]?.chaId === "§playground"}
<div class="shadow-lg border-textcolor2 border flex justify-center items-center text-textcolor2" style={options?.styleFix ?? `height:${DBState.db.iconsize * 3.5 / 100}rem;width:${DBState.db.iconsize * 3.5 / 100}rem;min-width:${DBState.db.iconsize * 3.5 / 100}rem`} <div class="shadow-lg border-textcolor2 border flex justify-center items-center text-textcolor2" style={options?.styleFix ?? `height:${DBState.db.iconsize * 3.5 / 100}rem;width:${DBState.db.iconsize * 3.5 / 100}rem;min-width:${DBState.db.iconsize * 3.5 / 100}rem`}
class:rounded-md={options?.rounded} class:rounded-full={options?.rounded}> class:rounded-md={options?.rounded} class:rounded-full={options?.rounded}>
{#if name === 'assistant'} {#if name === 'assistant'}
@@ -516,7 +516,7 @@
class:rounded-tr-none={role === 'user'} class:rounded-tr-none={role === 'user'}
> >
<p class="text-gray-800">{@render textBox()}</p> <p class="text-gray-800">{@render textBox()}</p>
{#if DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx]?.time} {#if DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx]?.time}
<span class="text-xs text-textcolor2 mt-1 block"> <span class="text-xs text-textcolor2 mt-1 block">
{new Intl.DateTimeFormat(undefined, { {new Intl.DateTimeFormat(undefined, {
hour: '2-digit', hour: '2-digit',
@@ -525,7 +525,7 @@
month: '2-digit', month: '2-digit',
day: '2-digit', day: '2-digit',
hour12: false hour12: false
}).format(DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].time)} }).format(DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].time)}
</span> </span>
{/if} {/if}
</div> </div>
@@ -563,12 +563,12 @@
{@render icon({rounded: DBState.db.roundIcons})} {@render icon({rounded: DBState.db.roundIcons})}
<span class="flex flex-col ml-4 w-full max-w-full min-w-0 text-black"> <span class="flex flex-col ml-4 w-full max-w-full min-w-0 text-black">
<div class="flexium items-center chat-width"> <div class="flexium items-center chat-width">
{#if DBState.db.characters[$selectedCharID]?.chaId === "§playground" && !blankMessage} {#if DBState.db.characters[selIdState.selId]?.chaId === "§playground" && !blankMessage}
<span class="chat-width text-xl border-darkborderc flex items-center"> <span class="chat-width text-xl border-darkborderc flex items-center">
<span>{name === 'assistant' ? 'Assistant' : 'User'}</span> <span>{name === 'assistant' ? 'Assistant' : 'User'}</span>
<button class="ml-2 text-textcolor2 hover:text-textcolor" onclick={() => { <button class="ml-2 text-textcolor2 hover:text-textcolor" onclick={() => {
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].role = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage].message[idx].role === 'char' ? 'user' : 'char' DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].role = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage].message[idx].role === 'char' ? 'user' : 'char'
DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage] = DBState.db.characters[$selectedCharID].chats[DBState.db.characters[$selectedCharID].chatPage] DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage] = DBState.db.characters[selIdState.selId].chats[DBState.db.characters[selIdState.selId].chatPage]
}}><ArrowLeftRightIcon size="18" /></button> }}><ArrowLeftRightIcon size="18" /></button>
</span> </span>
{:else if !blankMessage && !$HideIconStore} {:else if !blankMessage && !$HideIconStore}

View File

@@ -245,12 +245,9 @@ export async function runCharacterJS(arg:{
mode: ScriptMode|'onButtonClick'|'modifyRequestChat' mode: ScriptMode|'onButtonClick'|'modifyRequestChat'
data: any data: any
}):Promise<any>{ }):Promise<any>{
const perf = performance.now()
try { try {
if(arg.code === null){ arg.code = arg.code ?? ''
const db = getDatabase()
const selectedChar = get(selectedCharID)
arg.code = db.characters[selectedChar].virtualscript
}
const codes = { const codes = {
"editinput": 'editInput', "editinput": 'editInput',
"editoutput": 'editOutput', "editoutput": 'editOutput',
@@ -320,5 +317,8 @@ export async function runCharacterJS(arg:{
} }
return arg.data 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 { decodeRPack, encodeRPack } from "../rpack/rpack_bg"
import { convertImage } from "../parser.svelte" import { convertImage } from "../parser.svelte"
import { Capacitor } from "@capacitor/core" 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{ export interface RisuModule{
name: string name: string
@@ -395,19 +396,19 @@ export async function applyModule() {
alertNormal(language.successApplyModule) alertNormal(language.successApplyModule)
} }
let lastGlobalEnabledModules: string[] = [] let lastModuleIds:string = ''
let lastChatEnabledModules: string[] = []
export function moduleUpdate(){ export function moduleUpdate(){
if(!Array.isArray(lastGlobalEnabledModules)){
lastGlobalEnabledModules = []
}
if(!Array.isArray(lastChatEnabledModules)){
lastChatEnabledModules = []
}
const m = getModules() 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 moduleHideIcon = false
let backgroundEmbedding = '' let backgroundEmbedding = ''
m.forEach((module) => { m.forEach((module) => {

View File

@@ -65,9 +65,38 @@ export async function importRegex(o?:customscript[]):Promise<customscript[]>{
} }
let bestMatchCache = new Map<string, string>() 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 = {}){ export async function processScriptFull(char:character|groupChat|simpleCharacterArgument, data:string, mode:ScriptMode, chatID = -1, cbsConditions:CbsConditions = {}){
let db = getDatabase() 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 let emoChanged = false
const scripts = (db.globalscript ?? []).concat(char.customscript).concat(getModuleRegexScripts()) const scripts = (db.globalscript ?? []).concat(char.customscript).concat(getModuleRegexScripts())
data = await runCharacterJS({ data = await runCharacterJS({
@@ -77,6 +106,7 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
}) })
data = await runLuaEditTrigger(char, mode, data) data = await runLuaEditTrigger(char, mode, data)
if(scripts.length === 0){ if(scripts.length === 0){
cacheScript(scripts, originalData, data)
return {data, emoChanged} return {data, emoChanged}
} }
function executeScript(pscript:pScript){ function executeScript(pscript:pScript){
@@ -311,6 +341,8 @@ export async function processScriptFull(char:character|groupChat|simpleCharacter
} }
} }
cacheScript(scripts, originalData, data)
return {data, emoChanged} 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 { simpleCharacterArgument } from "./parser.svelte";
import type { alertData } from "./alert"; import type { alertData } from "./alert";
import { getModules, moduleUpdate } from "./process/modules"; import { getModules, moduleUpdate } from "./process/modules";
import { resetScriptCache } from "./process/scripts";
function updateSize(){ function updateSize(){
SizeStore.set({ SizeStore.set({
@@ -95,6 +96,10 @@ export const DBState = $state({
export const disableHighlight = writable(true) export const disableHighlight = writable(true)
ReloadGUIPointer.subscribe(() => {
resetScriptCache()
})
$effect.root(() => { $effect.root(() => {
selectedCharID.subscribe((v) => { selectedCharID.subscribe((v) => {
selIdState.selId = 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]?.chats?.[DBState?.db?.characters?.[selIdState.selId]?.chatPage]?.modules?.length
DBState?.db?.characters?.[selIdState.selId]?.hideChatIcon DBState?.db?.characters?.[selIdState.selId]?.hideChatIcon
DBState?.db?.moduleIntergration DBState?.db?.moduleIntergration
ReloadGUIPointer.set(get(ReloadGUIPointer) + 1)
moduleUpdate() moduleUpdate()
}) })
}) })