performance improvements allegedly

This commit is contained in:
2026-04-07 00:55:43 -04:00
parent 65190d6799
commit ca6c3da793
4 changed files with 93 additions and 54 deletions
+36 -27
View File
@@ -27,11 +27,15 @@ export class ChatService {
#bannerUrls = new SvelteMap<string, string>();
#serverAvatarUrls = new SvelteMap<string, string>();
// Track which IDs are already being processed to avoid O(N) re-scanning
#processedAvatarIds = new Set<string>();
#processedBannerIds = new Set<string>();
#processedServerAvatarIds = new Set<string>();
constructor(initialIdentity: Identity | null) {
this.identity = initialIdentity;
this.#db = new DatabaseService();
// this.#ui = new ThemeService(); // Removed to use shared themeService
this.#nav = new NavigationService(this.#db, () => this.identity);
this.#auth = new AuthContextService(this.#db, () => this.identity);
this.#msg = new MessagingService(this.#db, this.#nav, () => this.identity);
@@ -39,74 +43,79 @@ export class ChatService {
this.#account = new AccountService();
this.#server = new ServerManagementService();
// Background effect to populate avatar cache
// Background effect to populate avatar cache - OPTIMIZED
$effect(() => {
const usersWithAvatars = this.users.filter((u) => u.avatarId);
for (const user of usersWithAvatars) {
const avatarId = user.avatarId!;
const idStr = avatarId.toString();
for (const user of this.users) {
if (!user.avatarId) continue;
const idStr = user.avatarId.toString();
// Skip if already in memory
if (this.#avatarUrls.has(idStr)) continue;
if (this.#processedAvatarIds.has(idStr)) continue;
this.#processedAvatarIds.add(idStr);
// Try to load from persistent cache or SpacetimeDB
imageCache.get(avatarId).then((cachedUrl) => {
imageCache.get(user.avatarId).then((cachedUrl) => {
if (cachedUrl) {
this.#avatarUrls.set(idStr, cachedUrl);
} else {
const img = this.#db.images.find((i) => i.id === avatarId);
const img = this.#db.images.find((i) => i.id === user.avatarId);
if (img) {
imageCache.set(img.id, img.data, img.mimeType).then((url) => {
this.#avatarUrls.set(idStr, url);
});
} else {
// If not found yet, allow retry later
this.#processedAvatarIds.delete(idStr);
}
}
});
}
});
// Background effect to populate banner cache
// Background effect to populate banner cache - OPTIMIZED
$effect(() => {
const usersWithBanners = this.users.filter((u) => u.bannerId);
for (const user of usersWithBanners) {
const bannerId = user.bannerId!;
const idStr = bannerId.toString();
for (const user of this.users) {
if (!user.bannerId) continue;
const idStr = user.bannerId.toString();
if (this.#bannerUrls.has(idStr)) continue;
if (this.#processedBannerIds.has(idStr)) continue;
this.#processedBannerIds.add(idStr);
imageCache.get(bannerId).then((cachedUrl) => {
imageCache.get(user.bannerId).then((cachedUrl) => {
if (cachedUrl) {
this.#bannerUrls.set(idStr, cachedUrl);
} else {
const img = this.#db.images.find((i) => i.id === bannerId);
const img = this.#db.images.find((i) => i.id === user.bannerId);
if (img) {
imageCache.set(img.id, img.data, img.mimeType).then((url) => {
this.#bannerUrls.set(idStr, url);
});
} else {
this.#processedBannerIds.delete(idStr);
}
}
});
}
});
// Background effect to populate server avatar cache
// Background effect to populate server avatar cache - OPTIMIZED
$effect(() => {
const serversWithAvatars = this.servers.filter((s) => s.avatarId);
for (const server of serversWithAvatars) {
const avatarId = server.avatarId!;
const idStr = avatarId.toString();
for (const server of this.servers) {
if (!server.avatarId) continue;
const idStr = server.avatarId.toString();
if (this.#serverAvatarUrls.has(idStr)) continue;
if (this.#processedServerAvatarIds.has(idStr)) continue;
this.#processedServerAvatarIds.add(idStr);
imageCache.get(avatarId).then((cachedUrl) => {
imageCache.get(server.avatarId).then((cachedUrl) => {
if (cachedUrl) {
this.#serverAvatarUrls.set(idStr, cachedUrl);
} else {
const img = this.#db.images.find((i) => i.id === avatarId);
const img = this.#db.images.find((i) => i.id === server.avatarId);
if (img) {
imageCache.set(img.id, img.data, img.mimeType).then((url) => {
this.#serverAvatarUrls.set(idStr, url);
});
} else {
this.#processedServerAvatarIds.delete(idStr);
}
}
});