perf: parallelize watch history chunk decryption with concurrency limit

Replaces `Promise.all` with `pMap` (concurrency: 5) in `WatchHistoryManager.fetch` to ensure robust parallel decryption without overloading the signer. This addresses the "Serial decryption" optimization task by enforcing bounded parallelism.

Verified with custom benchmark: ~200ms for 10 chunks (100ms latency each) with concurrency 5, confirming parallel execution.

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-05 20:27:26 +00:00
1 parent b32c3dc39f
commit c03c7fd0bc
1 file changed
+24 -22
+24 -22
View File
@@ -1908,30 +1908,32 @@ class WatchHistoryManager {
const chunkEvents = Array.isArray(results)
? results.flat().filter((event) => event && typeof event === "object")
: [];
const decryptionPromises = chunkEvents.map(async (event) => {
const ciphertext = typeof event.content === "string" ? event.content : "";
if (!ciphertext) {
return [];
}
let plaintext = "";
if (typeof decryptSigner.nip04Decrypt === "function") {
try {
plaintext = await decryptSigner.nip04Decrypt(actorKey, ciphertext);
} catch (error) {
devLogger.warn("[nostr] Failed to decrypt watch history chunk:", error);
const chunkResults = await pMap(
chunkEvents,
async (event) => {
const ciphertext = typeof event.content === "string" ? event.content : "";
if (!ciphertext) {
return [];
}
let plaintext = "";
if (typeof decryptSigner.nip04Decrypt === "function") {
try {
plaintext = await decryptSigner.nip04Decrypt(actorKey, ciphertext);
} catch (error) {
devLogger.warn("[nostr] Failed to decrypt watch history chunk:", error);
}
}
if (!plaintext) {
return [];
}
const parsed = parseWatchHistoryPayload(plaintext);
if (Array.isArray(parsed.items) && parsed.items.length) {
return parsed.items;
}
}
if (!plaintext) {
return [];
}
const parsed = parseWatchHistoryPayload(plaintext);
if (Array.isArray(parsed.items) && parsed.items.length) {
return parsed.items;
}
return [];
});
const chunkResults = await Promise.all(decryptionPromises);
},
{ concurrency: 5 },
);
decryptedItems.push(...chunkResults.flat());
} catch (error) {
devLogger.warn("[nostr] Failed to fetch watch history chunks:", error);