Skip to content

Commit dfb7f2c

Browse files
npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6wtellaho
andcommitted
perf(timeline): memoize normalized thread replies per source ref+depth
Thread rows fed MessageRow a fresh {...reply, depth} spread on every render via normalizeInlineReplyMessage. When timelineMessages churned (typing/presence) the reply data was byte-identical but the object identity changed, busting the MessageRow/Markdown memo and forcing a ~1.4ms/row markdown re-parse on threads the main timeline kept cheap by passing the raw stable ref. Cache the normalized object in a WeakMap keyed on the source reply reference (+ a per-depth inner map), mirroring videoReviewContextById in TimelineMessageList. An unrelated churn that leaves a reply object intact now reuses the same normalized reference -> memo hits; a genuinely new reply object (edit/refresh) recomputes; depth still reaches the row via the cached object. WeakMap drops stale entries when the old message set is collected. No virtualization, must-keeps + no-tearing untouched. Adds two threadPanel.test.mjs cases: identity preserved across unrelated churn (memo hit) and recompute on replaced source reply. Desktop gate green before commit: pnpm check exit 0 (tsc + biome; only the pre-existing onboarding.spec.ts unused-var warnings remain), full suite 722/722. Co-authored-by: Taylor Ho <[email protected]> Signed-off-by: Taylor Ho <[email protected]>
1 parent c1e3b17 commit dfb7f2c

2 files changed

Lines changed: 142 additions & 1 deletion

File tree

‎desktop/src/features/messages/lib/threadPanel.test.mjs‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,111 @@ test("A.3.1: channel-wide walk runs once per timelineMessages change, not per th
193193
);
194194
assert.ok(mainEntries.length > 0);
195195
});
196+
197+
// Per-id stabilization: thread rows feed `MessageRow` a depth-normalized copy
198+
// of each reply. When `timelineMessages` churns (typing/presence) but the
199+
// reply objects survive by reference, rebuilding the thread panel must hand
200+
// `MessageRow` the SAME normalized object reference so the row/markdown memo
201+
// hits — instead of a fresh `{ ...reply, depth }` spread every render.
202+
test("thread reply objects keep identity across unrelated timelineMessages churn", () => {
203+
const root = message({ id: "root", createdAt: 1 });
204+
const replyA = message({
205+
id: "a",
206+
createdAt: 2,
207+
parentId: "root",
208+
rootId: "root",
209+
depth: 1,
210+
tags: [["e", "root", "", "reply"]],
211+
});
212+
const replyB = message({
213+
id: "b",
214+
createdAt: 3,
215+
parentId: "a",
216+
rootId: "root",
217+
depth: 2,
218+
tags: [["e", "a", "", "reply"]],
219+
});
220+
221+
// First render of the thread.
222+
const first = buildThreadPanelData(
223+
[root, replyA, replyB],
224+
"root",
225+
"root",
226+
new Set(["a"]),
227+
);
228+
229+
// An unrelated channel churn produces a NEW `timelineMessages` array, but the
230+
// reply objects themselves are reused by reference (only their position in
231+
// the surrounding array changed — e.g. a presence ping or typing indicator
232+
// that the snapshot layer leaves the reply identities intact for).
233+
const churned = [
234+
message({ id: "noise", createdAt: 99 }),
235+
root,
236+
replyA,
237+
replyB,
238+
];
239+
const second = buildThreadPanelData(churned, "root", "root", new Set(["a"]));
240+
241+
const firstById = new Map(
242+
first.visibleReplies.map((entry) => [entry.message.id, entry.message]),
243+
);
244+
const secondById = new Map(
245+
second.visibleReplies.map((entry) => [entry.message.id, entry.message]),
246+
);
247+
248+
assert.ok(firstById.size > 0, "expected at least one visible reply");
249+
for (const [id, normalized] of firstById) {
250+
assert.strictEqual(
251+
secondById.get(id),
252+
normalized,
253+
`normalized reply ${id} must be the SAME object reference across an unrelated churn (memo hit)`,
254+
);
255+
// Depth must still reach the row correctly via the cached object.
256+
assert.equal(
257+
typeof normalized.depth,
258+
"number",
259+
`normalized reply ${id} must carry a numeric depth`,
260+
);
261+
}
262+
});
263+
264+
test("thread reply objects recompute when the source reply object is replaced", () => {
265+
const root = message({ id: "root", createdAt: 1 });
266+
const reply = message({
267+
id: "a",
268+
createdAt: 2,
269+
parentId: "root",
270+
rootId: "root",
271+
depth: 1,
272+
tags: [["e", "root", "", "reply"]],
273+
});
274+
275+
const first = buildThreadPanelData([root, reply], "root", "root", new Set());
276+
277+
// A genuine edit/refresh: the reply is a brand-new object (new identity).
278+
const editedReply = message({
279+
id: "a",
280+
createdAt: 2,
281+
parentId: "root",
282+
rootId: "root",
283+
depth: 1,
284+
body: "edited body",
285+
tags: [["e", "root", "", "reply"]],
286+
});
287+
const second = buildThreadPanelData(
288+
[root, editedReply],
289+
"root",
290+
"root",
291+
new Set(),
292+
);
293+
294+
const firstA = first.visibleReplies.find((e) => e.message.id === "a");
295+
const secondA = second.visibleReplies.find((e) => e.message.id === "a");
296+
assert.ok(firstA && secondA, "expected reply 'a' in both renders");
297+
assert.notStrictEqual(
298+
secondA.message,
299+
firstA.message,
300+
"a replaced source reply must produce a fresh normalized object",
301+
);
302+
assert.equal(secondA.message.body, "edited body");
303+
});

‎desktop/src/features/messages/lib/threadPanel.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,47 @@ function normalizeHeadMessage(message: TimelineMessage): TimelineMessage {
4141
};
4242
}
4343

44+
// Thread rows feed `MessageRow` a depth-normalized copy of each reply. Building
45+
// that copy fresh (`{ ...message, depth }`) on every render hands `MessageRow` a
46+
// new object identity every time `timelineMessages` churns (typing/presence),
47+
// even when the reply and its depth are byte-identical — which defeats the
48+
// row/markdown memo and forces a ~1.4ms/row re-parse on threads where the main
49+
// timeline (which passes the raw stable ref) stays cheap.
50+
//
51+
// Mirror the main list's per-id context memoization (`videoReviewContextById`):
52+
// cache the normalized object keyed on the source reply identity + depth, so an
53+
// unrelated channel churn that leaves a reply (and its tree position) intact
54+
// reuses the exact same object reference and the memo hits.
55+
//
56+
// Keyed on the source `reply` reference via a WeakMap: a new `timelineMessages`
57+
// set produces new reply objects (genuine recompute), and stale entries are
58+
// collected automatically when the old message set is dropped.
59+
const normalizedInlineReplyCache = new WeakMap<
60+
TimelineMessage,
61+
Map<number, TimelineMessage>
62+
>();
63+
4464
function normalizeInlineReplyMessage(
4565
message: TimelineMessage,
4666
depth: number,
4767
): TimelineMessage {
48-
return {
68+
let byDepth = normalizedInlineReplyCache.get(message);
69+
if (!byDepth) {
70+
byDepth = new Map<number, TimelineMessage>();
71+
normalizedInlineReplyCache.set(message, byDepth);
72+
}
73+
74+
const cached = byDepth.get(depth);
75+
if (cached) {
76+
return cached;
77+
}
78+
79+
const normalized: TimelineMessage = {
4980
...message,
5081
depth,
5182
};
83+
byDepth.set(depth, normalized);
84+
return normalized;
5285
}
5386

5487
function buildDirectChildrenByParentId(messages: TimelineMessage[]) {

0 commit comments

Comments
 (0)