From 19b7d7d9d90e23634e8183b2572ae5126459354d Mon Sep 17 00:00:00 2001 From: duckietm Date: Fri, 3 Apr 2026 10:48:34 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=99=20=20more=20optimization=20mem.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/room/widgets/ChatBubbleUtilities.ts | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/api/room/widgets/ChatBubbleUtilities.ts b/src/api/room/widgets/ChatBubbleUtilities.ts index 2f6d54c..bbfbc5b 100644 --- a/src/api/room/widgets/ChatBubbleUtilities.ts +++ b/src/api/room/widgets/ChatBubbleUtilities.ts @@ -2,6 +2,8 @@ import { AvatarFigurePartType, AvatarScaleType, AvatarSetType, GetAvatarRenderMa export class ChatBubbleUtilities { + private static MAX_CACHE_SIZE: number = 200; + public static AVATAR_COLOR_CACHE: Map = new Map(); public static AVATAR_IMAGE_CACHE: Map = new Map(); public static PET_IMAGE_CACHE: Map = new Map(); @@ -9,6 +11,19 @@ export class ChatBubbleUtilities private static placeHolderImageUrl: string = ''; + private static pruneCache(cache: Map, maxSize: number = ChatBubbleUtilities.MAX_CACHE_SIZE): void + { + if(cache.size <= maxSize) return; + + const deleteCount = cache.size - maxSize; + const iterator = cache.keys(); + + for(let i = 0; i < deleteCount; i++) + { + cache.delete(iterator.next().value as string); + } + } + public static async setFigureImage(figure: string): Promise { const avatarImage = GetAvatarRenderManager().createAvatarImage(figure, AvatarScaleType.LARGE, null, { @@ -34,6 +49,9 @@ export class ChatBubbleUtilities this.AVATAR_COLOR_CACHE.set(figure, ((color && color.rgb) || 16777215)); this.AVATAR_IMAGE_CACHE.set(figure, imageUrl); + this.pruneCache(this.AVATAR_COLOR_CACHE); + this.pruneCache(this.AVATAR_IMAGE_CACHE); + avatarImage.dispose(); return imageUrl; @@ -100,7 +118,11 @@ export class ChatBubbleUtilities if(!resolvedImage) resolvedImage = await getImageUrl(imageResult); - if(resolvedImage) this.PET_IMAGE_CACHE.set(cacheKey, resolvedImage); + if(resolvedImage) + { + this.PET_IMAGE_CACHE.set(cacheKey, resolvedImage); + this.pruneCache(this.PET_IMAGE_CACHE); + } return resolvedImage; })();