perf: parallelize watch history chunk decryption

Refactored the watch history decryption loop to use Promise.all for
concurrently decrypting all chunks. This significantly reduces the
total time spent waiting for decryption, especially when dealing with
multiple history chunks. Each decryption remains isolated in a
try...catch block to ensure that a single failure does not block the
rest of the batch.

Performance results (simulated):
- Sequential: ~1011ms (20 chunks, 50ms delay)
- Parallel: ~55ms (20 chunks, 50ms delay)
- Speedup: ~18x

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-26 17:21:44 +00:00
1 parent c5388fac76
commit bcaae09e8b
1 file changed
+20 -8
+20 -8
View File
@@ -2052,24 +2052,36 @@ class WatchHistoryManager {
const chunkEvents = Array.isArray(results)
? results.flat().filter((event) => event && typeof event === "object")
: [];
for (const event of chunkEvents) {
const ciphertext = typeof event.content === "string" ? event.content : "";
const decryptionPromises = chunkEvents.map(async (event) => {
const ciphertext =
typeof event.content === "string" ? event.content : "";
if (!ciphertext) {
continue;
return null;
}
let plaintext = "";
if (typeof decryptSigner.nip04Decrypt === "function") {
try {
plaintext = await decryptSigner.nip04Decrypt(actorKey, ciphertext);
plaintext = await decryptSigner.nip04Decrypt(
actorKey,
ciphertext,
);
} catch (error) {
devLogger.warn("[nostr] Failed to decrypt watch history chunk:", error);
devLogger.warn(
"[nostr] Failed to decrypt watch history chunk:",
error,
);
}
}
if (!plaintext) {
continue;
return null;
}
const parsed = parseWatchHistoryPayload(plaintext);
if (Array.isArray(parsed.items) && parsed.items.length) {
return parseWatchHistoryPayload(plaintext);
});
const decryptedPayloads = await Promise.all(decryptionPromises);
for (const parsed of decryptedPayloads) {
if (parsed && Array.isArray(parsed.items) && parsed.items.length) {
decryptedItems.push(...parsed.items);
}
}