Skip to content

Commit deb3e6a

Browse files
wesbillmanBrain
andauthored
perf(desktop): instant channel switching — non-blocking first paint, persisted snapshots (#1452)
Signed-off-by: Wes <[email protected]> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
1 parent a9e752e commit deb3e6a

8 files changed

Lines changed: 531 additions & 30 deletions

File tree

‎desktop/src/features/messages/hooks.ts‎

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ import type { Channel, Identity, RelayEvent } from "@/shared/api/types";
3333
import { applyEditTagOverlay } from "@/features/messages/lib/applyEditTagOverlay.mjs";
3434
import { backfillAuxForMessages } from "@/features/messages/lib/auxBackfill";
3535
import { countTopLevelTimelineRows } from "@/features/messages/lib/formatTimelineMessages";
36+
import {
37+
mergeHistoryOverSnapshot,
38+
readMessageSnapshot,
39+
writeMessageSnapshot,
40+
} from "@/features/messages/lib/messageSnapshot";
3641
import {
3742
MIN_TOP_LEVEL_ROWS_PER_FETCH,
3843
pageOlderMessagesUntilRowFloor,
3944
} from "@/features/messages/lib/pageOlderMessages";
45+
import { useWorkspaces } from "@/features/workspaces/useWorkspaces";
4046
import {
4147
KIND_STREAM_MESSAGE,
4248
KIND_SYSTEM_MESSAGE,
@@ -171,10 +177,24 @@ export function createOptimisticMessage(
171177
export function useChannelMessagesQuery(channel: Channel | null) {
172178
const queryClient = useQueryClient();
173179
const queryKey = channelMessagesKey(channel?.id ?? "none");
180+
const { activeWorkspace } = useWorkspaces();
181+
const relayUrl = activeWorkspace?.relayUrl ?? null;
174182

175-
return useQuery({
183+
const query = useQuery({
176184
enabled: channel !== null && channel.channelType !== "forum",
177-
placeholderData: () => queryClient.getQueryData<RelayEvent[]>(queryKey),
185+
// Paint instantly from the in-memory cache, or — after a restart / gc —
186+
// from the persisted per-channel snapshot, then revalidate behind it.
187+
placeholderData: () => {
188+
const cached = queryClient.getQueryData<RelayEvent[]>(queryKey);
189+
if (cached && cached.length > 0) {
190+
return cached;
191+
}
192+
if (!channel || !relayUrl) {
193+
return undefined;
194+
}
195+
const snapshot = readMessageSnapshot(relayUrl, channel.id);
196+
return snapshot ? normalizeTimelineMessages(snapshot) : undefined;
197+
},
178198
queryKey,
179199
queryFn: async () => {
180200
if (!channel) {
@@ -185,36 +205,64 @@ export function useChannelMessagesQuery(channel: Channel | null) {
185205
channel.id,
186206
CHANNEL_HISTORY_LIMIT,
187207
);
188-
const currentMessages =
189-
queryClient.getQueryData<RelayEvent[]>(queryKey) ?? [];
190-
const mergedHistory = mergeTimelineHistoryMessages(
191-
currentMessages,
192-
history,
193-
);
208+
// Merge over the cache, or over the persisted snapshot when cold; a
209+
// cold snapshot load widens the aux backfill to the merged timeline so
210+
// tombstones/edits for snapshot-only rows are fetched (see helper doc).
211+
const cached = queryClient.getQueryData<RelayEvent[]>(queryKey);
212+
const { merged: mergedHistory, auxBackfillWindow } =
213+
mergeHistoryOverSnapshot({
214+
cached,
215+
snapshot:
216+
!cached && relayUrl
217+
? readMessageSnapshot(relayUrl, channel.id)
218+
: null,
219+
history,
220+
});
194221

195222
// Paint messages immediately; backfill their reactions/edits/deletions
196223
// by `#e` in the background (it self-merges into the same cache key).
197-
void backfillAuxForMessages(queryClient, channel.id, history);
224+
void backfillAuxForMessages(queryClient, channel.id, auxBackfillWindow);
198225

199-
// Seed the cache, then — only if the cold window renders thinner than a
200-
// normal scroll page — top it up to the same visible-row floor. A
201-
// reply-heavy channel's 300-message cold load can be ~12 rows; a normal
202-
// channel already clears the floor and skips the extra fetch entirely.
226+
// Seed the cache and paint immediately; if the cold window renders
227+
// thinner than a normal scroll page (reply-heavy channels), top it up
228+
// in the background — it self-merges into the same cache key.
203229
queryClient.setQueryData<RelayEvent[]>(queryKey, mergedHistory);
204230
if (
205231
countTopLevelTimelineRows(mergedHistory) < MIN_TOP_LEVEL_ROWS_PER_FETCH
206232
) {
207-
await pageOlderMessagesUntilRowFloor(
233+
void pageOlderMessagesUntilRowFloor(
208234
queryClient,
209235
channel.id,
210236
() => true,
211-
);
237+
).catch((error) => {
238+
console.error("Failed to top up channel history", channel.id, error);
239+
});
212240
}
213241
return queryClient.getQueryData<RelayEvent[]>(queryKey) ?? mergedHistory;
214242
},
215243
staleTime: 5 * 60 * 1_000,
216-
gcTime: 5 * 60 * 1_000,
244+
// Long in-memory retention: a channel revisited within the hour paints
245+
// from cache with zero relay round trips; the persisted snapshot covers
246+
// restarts beyond it.
247+
gcTime: 60 * 60 * 1_000,
248+
});
249+
250+
// Persist the newest slice after each settled update so the next cold open
251+
// (restart, gc) paints from the snapshot. Placeholder frames are skipped —
252+
// they are what the snapshot painted, not new information.
253+
const persistSnapshot = useEffectEvent((events: RelayEvent[]) => {
254+
if (relayUrl && channel) {
255+
writeMessageSnapshot(relayUrl, channel.id, events);
256+
}
217257
});
258+
const settledData = query.isPlaceholderData ? undefined : query.data;
259+
useEffect(() => {
260+
if (settledData && settledData.length > 0) {
261+
persistSnapshot(settledData);
262+
}
263+
}, [settledData]);
264+
265+
return query;
218266
}
219267

220268
export function useChannelSubscription(channel: Channel | null) {
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import {
5+
mergeHistoryOverSnapshot,
6+
messageSnapshotKey,
7+
readMessageSnapshot,
8+
removeMessageSnapshotsForRelay,
9+
writeMessageSnapshot,
10+
} from "./messageSnapshot.ts";
11+
12+
if (typeof globalThis.window === "undefined") {
13+
const storage = new Map();
14+
globalThis.window = {
15+
localStorage: {
16+
getItem: (key) => storage.get(key) ?? null,
17+
setItem: (key, value) => storage.set(key, value),
18+
removeItem: (key) => storage.delete(key),
19+
key: (index) => [...storage.keys()][index] ?? null,
20+
get length() {
21+
return storage.size;
22+
},
23+
},
24+
};
25+
}
26+
27+
function makeEvent(overrides = {}) {
28+
return {
29+
id: `event-${Math.random().toString(36).slice(2)}`,
30+
pubkey: "pubkey-1",
31+
created_at: 1_700_000_000,
32+
kind: 9,
33+
tags: [["h", "chan-1"]],
34+
content: "hello",
35+
sig: "sig",
36+
...overrides,
37+
};
38+
}
39+
40+
const RELAY = "wss://relay.example.com";
41+
42+
function clearRelay(relayUrl = RELAY) {
43+
removeMessageSnapshotsForRelay(relayUrl);
44+
}
45+
46+
test("messageSnapshotKey: normalizes trailing slash and case", () => {
47+
assert.equal(
48+
messageSnapshotKey("WSS://Relay.Example.com/", "chan-1"),
49+
messageSnapshotKey("wss://relay.example.com", "chan-1"),
50+
);
51+
});
52+
53+
test("read after write returns the persisted events", () => {
54+
clearRelay();
55+
const events = [makeEvent({ id: "a" }), makeEvent({ id: "b" })];
56+
writeMessageSnapshot(RELAY, "chan-1", events);
57+
assert.deepEqual(readMessageSnapshot(RELAY, "chan-1"), events);
58+
});
59+
60+
test("read for an unknown channel returns null", () => {
61+
clearRelay();
62+
assert.equal(readMessageSnapshot(RELAY, "chan-never"), null);
63+
});
64+
65+
test("read returns null for malformed JSON", () => {
66+
window.localStorage.setItem(
67+
messageSnapshotKey(RELAY, "chan-bad"),
68+
"not-json{{{",
69+
);
70+
assert.equal(readMessageSnapshot(RELAY, "chan-bad"), null);
71+
});
72+
73+
test("read returns null for a wrong-version payload", () => {
74+
window.localStorage.setItem(
75+
messageSnapshotKey(RELAY, "chan-v2"),
76+
JSON.stringify({ version: 2, updatedAt: 1, events: [makeEvent()] }),
77+
);
78+
assert.equal(readMessageSnapshot(RELAY, "chan-v2"), null);
79+
});
80+
81+
test("pending optimistic events are not persisted", () => {
82+
clearRelay();
83+
const settled = makeEvent({ id: "settled" });
84+
writeMessageSnapshot(RELAY, "chan-1", [
85+
settled,
86+
makeEvent({ id: "optimistic", pending: true }),
87+
]);
88+
assert.deepEqual(readMessageSnapshot(RELAY, "chan-1"), [settled]);
89+
});
90+
91+
test("write with only pending events persists nothing", () => {
92+
clearRelay();
93+
writeMessageSnapshot(RELAY, "chan-1", [makeEvent({ pending: true })]);
94+
assert.equal(readMessageSnapshot(RELAY, "chan-1"), null);
95+
});
96+
97+
test("snapshot keeps only the newest slice of a long timeline", () => {
98+
clearRelay();
99+
const events = Array.from({ length: 200 }, (_, i) =>
100+
makeEvent({ id: `event-${i}`, created_at: 1_700_000_000 + i }),
101+
);
102+
writeMessageSnapshot(RELAY, "chan-1", events);
103+
const persisted = readMessageSnapshot(RELAY, "chan-1");
104+
assert.equal(persisted.length, 80);
105+
assert.equal(persisted[persisted.length - 1].id, "event-199");
106+
assert.equal(persisted[0].id, "event-120");
107+
});
108+
109+
test("per-relay channel cap evicts the least recently written snapshot", () => {
110+
clearRelay();
111+
for (let i = 0; i < 21; i++) {
112+
writeMessageSnapshot(RELAY, `chan-${i}`, [makeEvent({ id: `e-${i}` })]);
113+
}
114+
// chan-0 was written first (oldest updatedAt tie broken by insertion) —
115+
// with 21 channels, at least one of the earliest must be evicted and the
116+
// newest retained.
117+
assert.notEqual(readMessageSnapshot(RELAY, "chan-20"), null);
118+
const retained = Array.from({ length: 21 }, (_, i) =>
119+
readMessageSnapshot(RELAY, `chan-${i}`),
120+
).filter((snapshot) => snapshot !== null);
121+
assert.equal(retained.length, 20);
122+
});
123+
124+
test("remove clears every snapshot for that relay only", () => {
125+
clearRelay();
126+
clearRelay("wss://other.example.com");
127+
writeMessageSnapshot(RELAY, "chan-1", [makeEvent({ id: "keep-other" })]);
128+
writeMessageSnapshot("wss://other.example.com", "chan-1", [
129+
makeEvent({ id: "other" }),
130+
]);
131+
removeMessageSnapshotsForRelay(RELAY);
132+
assert.equal(readMessageSnapshot(RELAY, "chan-1"), null);
133+
assert.notEqual(
134+
readMessageSnapshot("wss://other.example.com", "chan-1"),
135+
null,
136+
);
137+
});
138+
139+
test("write is tolerant of storage failures", () => {
140+
const original = window.localStorage.setItem;
141+
window.localStorage.setItem = () => {
142+
throw new Error("quota exceeded");
143+
};
144+
try {
145+
assert.doesNotThrow(() =>
146+
writeMessageSnapshot(RELAY, "chan-1", [makeEvent()]),
147+
);
148+
} finally {
149+
window.localStorage.setItem = original;
150+
}
151+
});
152+
153+
test("cold snapshot load: merge keeps snapshot-only rows and widens aux backfill to them", () => {
154+
const snapshotOnly = makeEvent({ id: "ghost", created_at: 1_700_000_000 });
155+
const fresh = makeEvent({ id: "fresh", created_at: 1_700_000_100 });
156+
const { merged, auxBackfillWindow } = mergeHistoryOverSnapshot({
157+
cached: undefined,
158+
snapshot: [snapshotOnly],
159+
history: [fresh],
160+
});
161+
assert.deepEqual(
162+
merged.map((event) => event.id),
163+
["ghost", "fresh"],
164+
);
165+
assert.ok(auxBackfillWindow.some((event) => event.id === "ghost"));
166+
assert.ok(auxBackfillWindow.some((event) => event.id === "fresh"));
167+
});
168+
169+
test("warm load: aux backfill stays scoped to the fresh window", () => {
170+
const cached = makeEvent({ id: "cached", created_at: 1_700_000_000 });
171+
const fresh = makeEvent({ id: "fresh", created_at: 1_700_000_100 });
172+
const { merged, auxBackfillWindow } = mergeHistoryOverSnapshot({
173+
cached: [cached],
174+
snapshot: [makeEvent({ id: "stale-snapshot" })],
175+
history: [fresh],
176+
});
177+
assert.ok(merged.some((event) => event.id === "cached"));
178+
assert.deepEqual(
179+
auxBackfillWindow.map((event) => event.id),
180+
["fresh"],
181+
);
182+
});
183+
184+
test("cold load without a snapshot backfills the fresh window only", () => {
185+
const fresh = makeEvent({ id: "fresh" });
186+
const { merged, auxBackfillWindow } = mergeHistoryOverSnapshot({
187+
cached: undefined,
188+
snapshot: null,
189+
history: [fresh],
190+
});
191+
assert.deepEqual(
192+
merged.map((event) => event.id),
193+
["fresh"],
194+
);
195+
assert.deepEqual(
196+
auxBackfillWindow.map((event) => event.id),
197+
["fresh"],
198+
);
199+
});

0 commit comments

Comments
 (0)