Merge pull request #2101 from PR0M3TH3AN/perf-profile-cache-optimization-1904566817068160104

⚡ Optimize ProfileCache memory clearing to O(1)
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2026-02-05 14:25:02 -05:00
commit 9caef9f63b
1 file changed
+28 -20
+28 -20
View File
@@ -26,7 +26,7 @@ function sanitizeProfileString(value) {
class ProfileCache { class ProfileCache {
constructor() { constructor() {
this.activePubkey = null; this.activePubkey = null;
this.memoryCache = new Map(); // For runtime decrypted data: pubkey:section -> data this.memoryCache = new Map(); // For runtime decrypted data: pubkey -> Map(section -> data)
this.listeners = new Set(); this.listeners = new Set();
} }
@@ -240,9 +240,11 @@ class ProfileCache {
getProfileData(pubkey, section) { getProfileData(pubkey, section) {
// 1. Check memory cache (runtime data) // 1. Check memory cache (runtime data)
const memKey = `${pubkey}:${section}`; if (this.memoryCache.has(pubkey)) {
if (this.memoryCache.has(memKey)) { const userCache = this.memoryCache.get(pubkey);
return this.memoryCache.get(memKey); if (userCache.has(section)) {
return userCache.get(section);
}
} }
// 2. Load from persistence // 2. Load from persistence
@@ -273,7 +275,10 @@ class ProfileCache {
} }
// Populate memory cache to avoid repeat parsing // Populate memory cache to avoid repeat parsing
this.memoryCache.set(memKey, parsed); if (!this.memoryCache.has(pubkey)) {
this.memoryCache.set(pubkey, new Map());
}
this.memoryCache.get(pubkey).set(section, parsed);
return parsed; return parsed;
} }
} catch (error) { } catch (error) {
@@ -302,39 +307,42 @@ class ProfileCache {
} }
// Update memory cache // Update memory cache
const memKey = `${pubkey}:${section}`; if (!this.memoryCache.has(pubkey)) {
this.memoryCache.set(memKey, data); this.memoryCache.set(pubkey, new Map());
}
this.memoryCache.get(pubkey).set(section, data);
this.emit("update", { pubkey, section, data }); this.emit("update", { pubkey, section, data });
this.emit("partition-updated", { pubkey, key: section }); this.emit("partition-updated", { pubkey, key: section });
} }
setMemoryDataForPubkey(pubkey, section, data) { setMemoryDataForPubkey(pubkey, section, data) {
const memKey = `${pubkey}:${section}`; if (!this.memoryCache.has(pubkey)) {
this.memoryCache.set(memKey, data); this.memoryCache.set(pubkey, new Map());
}
this.memoryCache.get(pubkey).set(section, data);
this.emit("update", { pubkey, section, data }); this.emit("update", { pubkey, section, data });
} }
setMemoryData(section, data) { setMemoryData(section, data) {
if (!this.activePubkey) return; if (!this.activePubkey) return;
const key = `${this.activePubkey}:${section}`; if (!this.memoryCache.has(this.activePubkey)) {
this.memoryCache.set(key, data); this.memoryCache.set(this.activePubkey, new Map());
}
this.memoryCache.get(this.activePubkey).set(section, data);
} }
getMemoryData(section) { getMemoryData(section) {
if (!this.activePubkey) return null; if (!this.activePubkey) return null;
const key = `${this.activePubkey}:${section}`; if (this.memoryCache.has(this.activePubkey)) {
return this.memoryCache.get(key); return this.memoryCache.get(this.activePubkey).get(section);
}
return undefined;
} }
clearMemoryCache(pubkey) { clearMemoryCache(pubkey) {
// Clear all entries starting with pubkey: this.memoryCache.delete(pubkey);
const prefix = `${pubkey}:`;
for (const key of this.memoryCache.keys()) {
if (key.startsWith(prefix)) {
this.memoryCache.delete(key);
}
}
} }
clearSignerRuntime(pubkey) { clearSignerRuntime(pubkey) {