Skip to content

Commit b2c6e4b

Browse files
authored
Merge pull request #4063 from solidjs/claude/project-thread-ico60t
frames: live writes morph only what changed (scoped slot sync + For row ops)
2 parents dfb31ed + 023571d commit b2c6e4b

14 files changed

Lines changed: 1116 additions & 47 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
Frames client: a live stream's writes no longer re-walk the whole frame. After every write, the slot sync used to walk all of the frame's content and re-parse every binding marker, and each live-hole apply searched the frame for its marker. Both costs grew with everything the frame showed, which was most of the per-write client time once a chat thread had history. Now only a root morph, a reveal, or an empty write ("re-walk what you hold") walks the whole frame. Any other write reuses the last walk's result and walks only the ranges its live holes morphed. That reuse runs in the lazy bind tier; without the tier, the walk sees only comments. Live-hole markers are cached between applies. When reuse can't be exact (a reveal whose content landed after its flush, an attr patch on a consumer, a hole whose parent element is a consumer), the full walk runs as before.

‎.changeset/live-for-row-ops.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"solid-js": patch
3+
"@solidjs/web": patch
4+
---
5+
6+
Live holes: on a live response, a sweep that changed some rows of a `<For>` sends just those rows (replace, append, truncate by index), and the frames client morphs only their ranges. A client whose range doesn't show what the rows were taken against resumes its live connection, which sends the hole whole.

‎examples/server-dbmon/server.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ function encoder(req, res) {
3434
let stream;
3535
if (/\bbr\b/.test(accepts)) {
3636
res.setHeader("Content-Encoding", "br");
37-
stream = createBrotliCompress({ params: { [constants.BROTLI_PARAM_QUALITY]: 4 } });
37+
// Common streaming settings: quality 5 with a 16 MB window, so a long-
38+
// lived stream keeps finding what it already sent.
39+
stream = createBrotliCompress({
40+
params: { [constants.BROTLI_PARAM_QUALITY]: 5, [constants.BROTLI_PARAM_LGWIN]: 24 }
41+
});
3842
} else if (/\bgzip\b/.test(accepts)) {
3943
res.setHeader("Content-Encoding", "gzip");
4044
stream = createGzip();

‎packages/solid/src/server/signals.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,6 +3283,10 @@ export function mapArray<T, U>(
32833283
s.push(runWithOwner(fo, () => options.fallback!()) as U);
32843284
}
32853285
kept = sweeping && items && items.length ? { items: snaps, rows: s, pure } : null;
3286+
// In a live render the rows are tagged as rows: a live hole whose
3287+
// value this is marks each row, so a sweep can send only the rows
3288+
// that changed (#4064).
3289+
if (live && items && items.length) (s as any).$rows = true;
32863290
return s;
32873291
},
32883292
{ sync: true }

‎packages/web/frames/src/bind-tier.ts‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ import { sharedConfig } from "solid-js/internal";
7777
// The eager frames client — the SHARED instance the app runs (see above).
7878
import { isAsyncValue } from "./client.js";
7979
// Own copies: the marker constants and the dev-only shape finders.
80-
import { SLOT_MARKER, SLOT_TEXT, shapeOf, slotShapeFinding } from "./frame-client.js";
80+
import { SLOT_MARKER, SLOT_TEXT, collectSlots, shapeOf, slotShapeFinding } from "./frame-client.js";
8181

8282
// Build-time literal (see diagnostics.ts): dev-only guidance folds out of
8383
// the prod chunk (`bind.js`); the dev chunk (`bind.dev.js`, the
@@ -241,6 +241,90 @@ export function unmount(frame: object, occurrence: string): void {
241241
byFrame.get(frame)?.delete(occurrence);
242242
}
243243

244+
// === The scoped slot sync ===
245+
246+
/**
247+
* The slot sync's discovery after a write that changed the frame's DOM only
248+
* inside the ranges its live holes morphed (frame-client.ts `apply`): the
249+
* last full walk's found map with what lay in those ranges taken out, plus
250+
* a walk of the ranges alone — the map a full walk would produce, in the
251+
* same consumer order. A live stream's every write otherwise re-walked,
252+
* and re-parsed the markers of, everything the frame shows. `ranges` are
253+
* `[open, close]` for a hole, `[element]` for an attr patch; `root` the
254+
* frame's element. Returns false, leaving `found` untouched, where reuse
255+
* cannot be exact (the caller walks the whole frame): no previous walk, an
256+
* attr patch on an element that carries markers or was a consumer, or a
257+
* hole whose parent element is a consumer (a text position inside the
258+
* range would join that element's entry, whose positions outside the range
259+
* keep their place).
260+
*/
261+
export function reuse(
262+
last: Map<string, any> | undefined,
263+
ranges: any[][],
264+
found: Map<string, any> & { b?: boolean },
265+
root: Element
266+
): boolean {
267+
if (!last) return false;
268+
// In the frame, not in the document: a frame an <Errored> fallback
269+
// detached still holds what it held.
270+
const own = (n: Node) => root.contains(n);
271+
// Ranges still in the frame, outermost only: a hole re-emitted inside
272+
// another this write also morphed is walked as part of it.
273+
const live = ranges.filter(r => own(r[0]));
274+
const inRange = (n: Node, skip?: any[]) => {
275+
for (const r of live)
276+
if (
277+
r !== skip &&
278+
r[1] &&
279+
n !== r[0] &&
280+
n !== r[1] &&
281+
// DOCUMENT_POSITION_FOLLOWING (4) / _PRECEDING (2).
282+
r[0].compareDocumentPosition(n) & 4 &&
283+
r[1].compareDocumentPosition(n) & 2
284+
)
285+
return true;
286+
return false;
287+
};
288+
const parents = new Set<Node>();
289+
for (const r of live) {
290+
if (r[1]) parents.add(r[0].parentNode);
291+
else if (slotPositions(r[0])) return false;
292+
else parents.add(r[0]);
293+
}
294+
for (const value of last.values())
295+
if (Array.isArray(value)) for (const c of value) if (parents.has(c.element)) return false;
296+
for (const [occurrence, value] of last) {
297+
if (!Array.isArray(value)) {
298+
if (own(value) && !inRange(value)) found.set(occurrence, value);
299+
continue;
300+
}
301+
const kept: any[] = [];
302+
for (const c of value)
303+
if (own(c.element) && !inRange(c.element))
304+
kept.push({ element: c.element, positions: c.positions.slice() });
305+
if (kept.length) found.set(occurrence, kept);
306+
}
307+
if ((last as any).b) found.b = true;
308+
const sizes = new Map<string, number>();
309+
for (const [occurrence, value] of found)
310+
if (Array.isArray(value)) sizes.set(occurrence, value.length);
311+
for (const r of live)
312+
if (r[1] && !inRange(r[0], r)) collectSlots(r[0].parentNode, found, found, r[0], r[1]);
313+
// Consumers a range walk added join a kept list at their document
314+
// position — an element by itself, a text-only one by its first marker,
315+
// the point a full walk would have pushed it.
316+
for (const [occurrence, value] of found)
317+
if (Array.isArray(value) && value.length > 1 && sizes.get(occurrence) !== value.length)
318+
value.sort((a, b) =>
319+
(a.positions[0].start || a.element).compareDocumentPosition(
320+
b.positions[0].start || b.element
321+
) & 4
322+
? -1
323+
: 1
324+
);
325+
return true;
326+
}
327+
244328
// === The morph's owned-position arms ===
245329

246330
/** The positions of an incoming element a client fill writes (see

0 commit comments

Comments
 (0)