Skip to content

Commit 122797e

Browse files
authored
perf(web): build direct-call carrier shells only when a value is bound (#4333)
* perf(web): build direct-call carrier shells only when a value is bound * test(web): alias server-functions in the server bench config
1 parent 1f558e5 commit 122797e

5 files changed

Lines changed: 137 additions & 15 deletions

File tree

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+
A direct server-function call no longer copies every plain object and array in its result. The carrier walk that binds deferred bodies to the call's request scope now builds a container's shallow-rebuilt shell only when a value inside it is bound or a cycle reaches it. Before, it built one for every container up front and discarded it when nothing changed. A call returning 1,000 plain rows walks in 0.56 ms instead of 0.91 ms.

‎packages/web/server-functions/src/server.ts‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -919,25 +919,35 @@ function scopeDeferredResult(value, scope) {
919919
/** One carrier mid-walk on the direct road's descent. Module-private, so a
920920
* user value can never satisfy the driver's `instanceof` dispatch. */
921921
class ScopeFrame {
922-
constructor(value, next, keys, descriptors) {
922+
constructor(value, prototype, keys, descriptors) {
923923
this.value = value;
924-
this.next = next;
924+
this.prototype = prototype;
925+
this.next = null;
925926
this.keys = keys;
926927
this.descriptors = descriptors;
927928
this.i = 0;
928929
this.changed = false;
929930
}
930931
}
931932

933+
/** The frame's shallow-rebuilt shell, built on first need: most carriers
934+
* hold nothing to bind and hand the author's container back unbuilt. */
935+
function frameShell(frame) {
936+
return (frame.next ??= frame.keys
937+
? Object.create(frame.prototype, frame.descriptors)
938+
: frame.value.slice());
939+
}
940+
932941
/** Resolve one nested value for the direct road's carrier descent: leaves,
933942
* already-walked entries and deferred bodies answer immediately; a plain
934-
* object or array answers a frame for the driver, its shallow-rebuilt shell
935-
* pre-registered so cycles resolve to the shell being built. */
943+
* object or array answers a frame for the driver, pre-registered so a cycle
944+
* builds and resolves to its shell. */
936945
function enterScopeCarrier(value, state) {
937946
if (value === null || (typeof value !== "object" && typeof value !== "function")) return value;
938947
if (state.seen.has(value)) {
939948
state.cyclic.add(value);
940-
return state.seen.get(value);
949+
const seen = state.seen.get(value);
950+
return seen instanceof ScopeFrame ? frameShell(seen) : seen;
941951
}
942952
const bound = bindDeferredBody(value, state.scope);
943953
if (bound !== value) {
@@ -952,9 +962,9 @@ function enterScopeCarrier(value, state) {
952962
state.seen.set(value, value);
953963
return value;
954964
}
955-
const next = value.slice();
956-
state.seen.set(value, next);
957-
return new ScopeFrame(value, next, null, null);
965+
const frame = new ScopeFrame(value, null, null, null);
966+
state.seen.set(value, frame);
967+
return frame;
958968
}
959969
const prototype = Object.getPrototypeOf(value);
960970
if ((prototype !== Object.prototype && prototype !== null) || Object.isFrozen(value)) {
@@ -965,10 +975,14 @@ function enterScopeCarrier(value, state) {
965975
// accessors and non-writable data stay exactly as authored (accessors are
966976
// the caller's to invoke, non-writable slots are out of the carrier set);
967977
// only writable enumerable data slots are candidates for binding.
968-
const descriptors = Object.getOwnPropertyDescriptors(value);
969-
const next = Object.create(prototype, descriptors);
970-
state.seen.set(value, next);
971-
return new ScopeFrame(value, next, Object.keys(value), descriptors);
978+
const frame = new ScopeFrame(
979+
value,
980+
prototype,
981+
Object.keys(value),
982+
Object.getOwnPropertyDescriptors(value)
983+
);
984+
state.seen.set(value, frame);
985+
return frame;
972986
}
973987

974988
// The descent is ITERATIVE for the same reason the codec road's walk is
@@ -1014,8 +1028,8 @@ function scopeDeferredContents(root, scope) {
10141028
}
10151029
}
10161030
if (bound !== original) {
1017-
if (top.keys) top.next[top.keys[i]] = bound;
1018-
else top.next[i] = bound;
1031+
if (top.keys) frameShell(top)[top.keys[i]] = bound;
1032+
else frameShell(top)[i] = bound;
10191033
top.changed = true;
10201034
}
10211035
top.i++;
@@ -1029,7 +1043,8 @@ function scopeDeferredContents(root, scope) {
10291043
// took the shell; otherwise the caller keeps the author's container
10301044
let out;
10311045
if (top.changed || state.cyclic.has(top.value)) {
1032-
out = top.next;
1046+
out = frameShell(top);
1047+
state.seen.set(top.value, out);
10331048
} else {
10341049
state.seen.set(top.value, top.value);
10351050
out = top.value;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Tier-1 SSR-lane bench for a direct server-function call's result walk: the
2+
// call an SSR `query()` makes, returning 1,000 plain rows. The direct road
3+
// walks the result to bind deferred bodies to the call's request scope; a
4+
// result with nothing deferred should hand its containers back unbuilt.
5+
//
6+
// - plain rows: nothing to bind anywhere in the graph
7+
// - one deferred body: the same rows plus a cursor, so the root rebuilds
8+
9+
import { AsyncLocalStorage } from "node:async_hooks";
10+
import { bench } from "vitest";
11+
import { createRequestEvent } from "@solidjs/web";
12+
import { createServerReference } from "@solidjs/web/server-functions/server";
13+
14+
const RequestContext = Symbol.for("solid.RequestContext");
15+
const requestContext = new AsyncLocalStorage<any>();
16+
(globalThis as any)[RequestContext] = requestContext;
17+
18+
const rows = Array.from({ length: 1000 }, (_, i) => ({
19+
id: `00000000-0000-4000-8000-${String(i).padStart(12, "0")}`,
20+
name: `row ${i}`
21+
}));
22+
23+
async function* cursor() {
24+
yield "one";
25+
}
26+
27+
const plain = createServerReference({
28+
id: "bench-direct-plain",
29+
fn: () => rows
30+
} as any) as () => typeof rows;
31+
const deferred = createServerReference({
32+
id: "bench-direct-deferred",
33+
fn: () => ({ rows, more: cursor() })
34+
} as any) as () => { rows: typeof rows };
35+
36+
const event = createRequestEvent(new Request("https://app.example/page"));
37+
38+
bench("direct call, 1,000 plain rows", () => {
39+
requestContext.run(event, () => plain());
40+
});
41+
42+
bench("direct call, 1,000 rows and one deferred body", () => {
43+
requestContext.run(event, () => deferred());
44+
});

‎packages/web/test/server/server-functions-nested-deferred-scope.spec.tsx‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,59 @@ describe("nested deferred work on the direct SSR road (#3241)", () => {
219219
expect(result).toBe(marker);
220220
});
221221

222+
it("resolves cycles within one carrier when nothing deferred is inside", async () => {
223+
const marker: any = { rows: [{ id: 1 }], meta: { total: 1 } };
224+
marker.self = marker;
225+
marker.rows.push(marker.rows);
226+
const reference = createServerReference({
227+
id: "nested-direct-cyclic-plain",
228+
fn: () => marker
229+
} as any) as () => typeof marker;
230+
231+
const result = await requestContext.run(eventFor("RENDER"), async () => reference());
232+
233+
// a cycle rebuilds the containers it passes through; the cycles then
234+
// point into the rebuilt graph, and untouched leaves keep identity
235+
expect(result.self).toBe(result);
236+
expect(result.rows[1]).toBe(result.rows);
237+
expect(result.rows[0]).toBe(marker.rows[0]);
238+
expect(result.meta).toBe(marker.meta);
239+
});
240+
241+
it("resolves a cycle to the rebuilt carrier when a body below is bound", async () => {
242+
const seen: string[] = [];
243+
let returned: any;
244+
const reference = createServerReference({
245+
id: "nested-direct-cyclic-bound",
246+
fn: () => {
247+
async function* rows() {
248+
seen.push(`resume:${(getRequestEvent() as any)?.derived === true ? "call" : "ambient"}`);
249+
yield "one";
250+
}
251+
(getRequestEvent() as any).derived = true;
252+
returned = { rows: rows(), list: [] as any[] };
253+
returned.self = returned;
254+
returned.list.push(returned.list, returned);
255+
return returned;
256+
}
257+
} as any) as () => any;
258+
259+
const result = await requestContext.run(eventFor("RENDER"), async () => {
260+
const value = reference();
261+
await drain(value.rows);
262+
return value;
263+
});
264+
265+
expect(seen).toEqual(["resume:call"]);
266+
// the carrier was rebuilt around the bound body, and every cycle in it
267+
// resolves to the rebuilt shells, never back to the author's containers
268+
expect(result).not.toBe(returned);
269+
expect(result.self).toBe(result);
270+
expect(result.list[1]).toBe(result);
271+
expect(result.list[0]).toBe(result.list);
272+
expect(returned.self).toBe(returned);
273+
});
274+
222275
it("pins Set members and frozen slots as out of the carrier set", async () => {
223276
// RULED OUT OF SCOPE (#3241): the supported carriers are plain objects
224277
// and arrays only. A generator reached through a Set member or a

‎packages/web/vite.config.server-bench.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export default defineConfig({
4040
resolve: {
4141
conditions: ["node"],
4242
alias: {
43+
// as in vite.config.server.mjs: the server-function runtime and the
44+
// codec subpaths it imports, ahead of the bare "@solidjs/web" alias
45+
"@solidjs/web/server-functions/server": resolve(rootDir, "server-functions/dist/server.js"),
46+
"@solidjs/web/serialization/decode": resolve(rootDir, "serialization/dist/decode.js"),
47+
"@solidjs/web/serialization": resolve(rootDir, "serialization/dist/serialization.js"),
4348
"@solidjs/web": resolve(rootDir, "src/index.server.ts"),
4449
// Before "solid-js": a string alias prefix-matches its subpaths.
4550
"solid-js/internal": resolve(rootDir, "../solid/src/internal.ts"),

0 commit comments

Comments
 (0)