From 9628ce7662a127fd6561c45b574e5f16047b52fd Mon Sep 17 00:00:00 2001 From: npub1cc3ha7z055mu0rwwu7806t2wt8mj3pvu0uv5mfp2c50dahaqhczshdalg6 Date: Wed, 8 Jul 2026 10:25:24 -0400 Subject: [PATCH] perf(desktop): drop per-row agent-pubkey rescan, use the set passed from parent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Finding [L4] MessageRow resolvedAgentPubkeys (Tier 2): every mounted row re-scanned the full `profiles` lookup to rebuild the agent-pubkey Set, so the same O(profiles) work ran once per row and re-ran on every profile-lookup change. ChannelScreen already computes this exact Set once (now from the stabilised lookup) and passes it down as `agentPubkeys`, already normalised. Consume the passed Set directly, falling back to a module-level stable empty Set so rows without one keep a constant reference (a fresh `new Set()` per render would defeat the row memo). Widen `getConfigNudgeAuthorPubkey`'s parameter to `ReadonlySet` — it only reads via `.has()`, and this matches how the `agentPubkeys` prop is already typed upstream. Co-authored-by: Tyler Longwell Signed-off-by: Tyler Longwell --- .../src/features/messages/ui/MessageRow.tsx | 20 +++++++++---------- .../messages/ui/configNudgeAuthPubkey.ts | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/desktop/src/features/messages/ui/MessageRow.tsx b/desktop/src/features/messages/ui/MessageRow.tsx index ac1cd93991..e253a05c15 100644 --- a/desktop/src/features/messages/ui/MessageRow.tsx +++ b/desktop/src/features/messages/ui/MessageRow.tsx @@ -43,6 +43,10 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "@/shared/ui/tooltip"; const DiffMessage = React.lazy(() => import("./DiffMessage")); const DiffMessageExpanded = React.lazy(() => import("./DiffMessageExpanded")); +/** Stable empty fallback so rows without an agent-pubkey set keep a constant + * reference (a fresh `new Set()` per render would defeat downstream memos). */ +const EMPTY_AGENT_PUBKEYS: ReadonlySet = new Set(); + export type ThreadDepthGuideAction = { active?: boolean; depth: number; @@ -157,17 +161,11 @@ export const MessageRow = React.memo( () => resolveMentionPubkeysByName(message.tags, profiles), [profiles, message.tags], ); - const resolvedAgentPubkeys = React.useMemo(() => { - const pubkeys = new Set(agentPubkeys ?? []); - - for (const [pubkey, profile] of Object.entries(profiles ?? {})) { - if (profile.isAgent) { - pubkeys.add(normalizePubkey(pubkey)); - } - } - - return pubkeys; - }, [agentPubkeys, profiles]); + // The agent-pubkey set is computed once by the parent (ChannelScreen) + // from the same profile lookup and passed down already normalised — no + // per-row rescan of `profiles` (that duplicated the parent's work in every + // mounted row and re-ran on each profile-lookup change). + const resolvedAgentPubkeys = agentPubkeys ?? EMPTY_AGENT_PUBKEYS; const profilePopoverRole = message.role === "bot" || (message.pubkey && diff --git a/desktop/src/features/messages/ui/configNudgeAuthPubkey.ts b/desktop/src/features/messages/ui/configNudgeAuthPubkey.ts index 65582f9650..1866e59040 100644 --- a/desktop/src/features/messages/ui/configNudgeAuthPubkey.ts +++ b/desktop/src/features/messages/ui/configNudgeAuthPubkey.ts @@ -18,7 +18,7 @@ import type { TimelineMessage } from "@/features/messages/types"; */ export function getConfigNudgeAuthorPubkey( message: Pick, - resolvedAgentPubkeys: Set, + resolvedAgentPubkeys: ReadonlySet, ): string | undefined { if ( message.kind === KIND_STREAM_MESSAGE &&