From ff41ff10611504259c3d82a08869bfcd4a59dad9 Mon Sep 17 00:00:00 2001 From: Rye Date: Mon, 4 May 2026 06:40:25 +0200 Subject: [PATCH] perf: simple caching (might need some later adjustments) --- src/app/utils/msc4459helper.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/app/utils/msc4459helper.ts b/src/app/utils/msc4459helper.ts index 04a1c06d9..79b809522 100644 --- a/src/app/utils/msc4459helper.ts +++ b/src/app/utils/msc4459helper.ts @@ -9,6 +9,17 @@ import { getViaServers } from '$plugins/via-servers'; import { getMxIdServer } from './mxIdHelper'; import { isRoomPrivate } from './roomVisibility'; +/** + * lookup table for global mxc => MSC4459ImagePackReference + * TODO this is far from a perfect solution + */ +const globalLookupTable = new Map(); +/** + * lookup table for room local mxc => MSC4459ImagePackReference + * TODO this is far from a perfect solution + */ +const roomLookupTable = new Map>(); + export function getImagePackReferencesForMxcWrappedInMap( mxcUrl: string, matrixClient: MatrixClient, @@ -62,7 +73,8 @@ export function getImagePackReferencesForMxc( room: Room ): MSC4459ImagePackReference { if (!mxcUrl.startsWith('mxc')) return {}; - const globalImgPacks: ImagePack[] = getGlobalImagePacks(matrixClient); + if (roomLookupTable.get(room.roomId)?.has(mxcUrl)) + return roomLookupTable.get(room.roomId)!.get(mxcUrl)!; const roomLocalImgPacks: ImagePack[] = getRoomImagePacks(room); const roomLocalMatch = getImagePackReferencesForMxcInternal( mxcUrl, @@ -71,8 +83,17 @@ export function getImagePackReferencesForMxc( imageUsage, true ); + if (roomLocalMatch) { + const roomLookupTabRes = + roomLookupTable.get(room.roomId) ?? new Map(); + roomLookupTabRes.set(mxcUrl, roomLocalMatch); + roomLookupTable.set(room.roomId, roomLookupTabRes); + } // prefer room local match as they're probably often more relevant if (roomLocalMatch) return roomLocalMatch; + // simple caching + if (globalLookupTable.has(mxcUrl)) return globalLookupTable.get(mxcUrl)!; + const globalImgPacks: ImagePack[] = getGlobalImagePacks(matrixClient); const globalMatch = getImagePackReferencesForMxcInternal( mxcUrl, matrixClient, @@ -80,6 +101,7 @@ export function getImagePackReferencesForMxc( imageUsage, false ); + if (globalMatch) globalLookupTable.set(mxcUrl, globalMatch); return globalMatch ?? {}; }