perf(messages): execute LID lookups in parallel for faster message delivery
PROBLEM: Even after making LID mapping operations async in messages-recv.ts, inbound messages still experienced 3-8 second delays. Analysis showed normalizeMessageJids() was performing TWO sequential await calls: 1. await resolveLidToPn(message.key.remoteJid) 2. await resolveLidToPn(message.key.participant) Each lookup could take 50-200ms, resulting in 100-400ms total delay BEFORE delivering the message to the user. ROOT CAUSE: Sequential awaits in normalizeMessageJids() (lines 134-142) were blocking message delivery unnecessarily since the two lookups are completely independent operations. SOLUTION: Changed to execute both LID→PN lookups in parallel using Promise.all: BEFORE (Sequential): - await resolveLidToPn(remoteJid) // 100ms - await resolveLidToPn(participant) // 100ms - Total: 200ms blocking time AFTER (Parallel): - Promise.all([resolve remote, resolve participant]) - Total: max(100ms, 100ms) = 100ms blocking time IMPACT: - ✅ Reduces normalizeMessageJids latency by ~50% - ✅ Combined with async LID mapping, should eliminate most delays - ✅ No functional changes, only execution order optimization - ✅ Maintains all error handling and logging Tested: - Build completes successfully - No breaking changes to function signature or behavior https://claude.ai/code/session_0149ZKk2ygmKCJTGu39Mr8oH
This commit is contained in:
@@ -131,12 +131,16 @@ export const normalizeMessageJids = async (
|
||||
return jid
|
||||
}
|
||||
|
||||
const resolvedRemoteJid = await resolveLidToPn(message.key.remoteJid)
|
||||
// Execute both lookups in parallel instead of sequentially to reduce latency
|
||||
const [resolvedRemoteJid, resolvedParticipant] = await Promise.all([
|
||||
resolveLidToPn(message.key.remoteJid),
|
||||
resolveLidToPn(message.key.participant)
|
||||
])
|
||||
|
||||
if (resolvedRemoteJid) {
|
||||
message.key.remoteJid = resolvedRemoteJid
|
||||
}
|
||||
|
||||
const resolvedParticipant = await resolveLidToPn(message.key.participant)
|
||||
if (resolvedParticipant) {
|
||||
message.key.participant = resolvedParticipant
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user