Skip to content

Commit b656418

Browse files
brenelzryansolid
andauthored
perf(web): skip scope probe and descriptor copies for plain direct-call carriers (#4651)
Co-authored-by: Ryan Carniato <[email protected]>
1 parent 335364b commit b656418

4 files changed

Lines changed: 323 additions & 45 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 enters the request scope or copies property descriptors for each plain object and array in its result. The walk that binds deferred bodies now skips `bindDeferredBody`'s scoped promise probe for a plain object or array that has no own iterator or `Symbol.toStringTag` hook, reads each slot's descriptor on its own, and allocates a frame only for a container whose slots hold an object. Descriptors are still snapshotted before any authored code can run. A call returning 1,000 plain rows walks in 0.11 ms instead of 0.55 ms.

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

Lines changed: 127 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -919,13 +919,15 @@ 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, prototype, keys, descriptors) {
922+
constructor(value, prototype, keys, i, first) {
923923
this.value = value;
924924
this.prototype = prototype;
925925
this.next = null;
926926
this.keys = keys;
927-
this.descriptors = descriptors;
928-
this.i = 0;
927+
this.i = i;
928+
this.first = first;
929+
this.descriptors = null;
930+
this.snapshotted = false;
929931
this.changed = false;
930932
}
931933
}
@@ -934,53 +936,137 @@ class ScopeFrame {
934936
* hold nothing to bind and hand the author's container back unbuilt. */
935937
function frameShell(frame) {
936938
return (frame.next ??= frame.keys
937-
? Object.create(frame.prototype, frame.descriptors)
939+
? Object.create(
940+
frame.prototype,
941+
frame.descriptors ?? Object.getOwnPropertyDescriptors(frame.value)
942+
)
938943
: frame.value.slice());
939944
}
940945

946+
// writable data only: accessors are not invoked, non-writable slots stay as authored
947+
function writableValue(descriptor) {
948+
return descriptor !== undefined && "value" in descriptor && descriptor.writable === true
949+
? descriptor.value
950+
: undefined;
951+
}
952+
953+
function carrierSlot(frame, key) {
954+
return writableValue(
955+
frame.descriptors ? frame.descriptors[key] : Object.getOwnPropertyDescriptor(frame.value, key)
956+
);
957+
}
958+
959+
// Authored code may run from here on, so the object frames on the stack take the
960+
// entry-time descriptor snapshot their slots and shells read. Frames below a
961+
// snapshotted one were already on the stack when it was taken.
962+
function snapshotFrames(stack) {
963+
for (let k = stack.length - 1; k >= 0 && !stack[k].snapshotted; k--) {
964+
const frame = stack[k];
965+
frame.snapshotted = true;
966+
if (frame.keys) frame.descriptors = Object.getOwnPropertyDescriptors(frame.value);
967+
}
968+
}
969+
970+
function isLeaf(value) {
971+
return value === null || (typeof value !== "object" && typeof value !== "function");
972+
}
973+
974+
// without these overrides, bindDeferredBody's probes cannot run authored code on a plain carrier
975+
function sharedPrototypesInert() {
976+
const { toStringTag, asyncIterator, iterator, hasInstance } = Symbol;
977+
return !(
978+
Object.hasOwn(Promise, hasInstance) ||
979+
(typeof ReadableStream !== "undefined" && Object.hasOwn(ReadableStream, hasInstance)) ||
980+
Object.hasOwn(Object.prototype, toStringTag) ||
981+
Object.hasOwn(Object.prototype, asyncIterator) ||
982+
Object.hasOwn(Object.prototype, iterator) ||
983+
Object.hasOwn(Array.prototype, toStringTag) ||
984+
Object.hasOwn(Array.prototype, asyncIterator)
985+
);
986+
}
987+
988+
// own hooks are tested without reading them; the reads then only reach a proxy's get trap
989+
function isInertCarrier(value, prototype, isArray) {
990+
const { toStringTag, asyncIterator, iterator } = Symbol;
991+
return (
992+
(isArray
993+
? prototype === Array.prototype
994+
: prototype === Object.prototype || prototype === null) &&
995+
!Object.hasOwn(value, toStringTag) &&
996+
!Object.hasOwn(value, asyncIterator) &&
997+
(isArray || !Object.hasOwn(value, iterator)) &&
998+
typeof value[asyncIterator] !== "function" &&
999+
(isArray || typeof value[iterator] !== "function")
1000+
);
1001+
}
1002+
9411003
/** Resolve one nested value for the direct road's carrier descent: leaves,
9421004
* already-walked entries and deferred bodies answer immediately; a plain
9431005
* object or array answers a frame for the driver, pre-registered so a cycle
944-
* builds and resolves to its shell. */
1006+
* builds and resolves to its shell; an all-leaf carrier gets no frame. */
9451007
function enterScopeCarrier(value, state) {
946-
if (value === null || (typeof value !== "object" && typeof value !== "function")) return value;
947-
if (state.seen.has(value)) {
1008+
if (isLeaf(value)) return value;
1009+
// entries are never undefined, so one lookup answers both questions
1010+
const seen = state.seen.get(value);
1011+
if (seen !== undefined) {
9481012
state.cyclic.add(value);
949-
const seen = state.seen.get(value);
9501013
return seen instanceof ScopeFrame ? frameShell(seen) : seen;
9511014
}
952-
const bound = bindDeferredBody(value, state.scope);
953-
if (bound !== value) {
954-
state.seen.set(value, bound);
955-
return bound;
1015+
let isArray = false;
1016+
let prototype;
1017+
if (typeof value === "object") {
1018+
isArray = Array.isArray(value);
1019+
prototype = Object.getPrototypeOf(value);
1020+
}
1021+
if (!state.inert || prototype === undefined || !isInertCarrier(value, prototype, isArray)) {
1022+
snapshotFrames(state.stack);
1023+
const bound = bindDeferredBody(value, state.scope);
1024+
if (bound !== value) {
1025+
state.seen.set(value, bound);
1026+
return bound;
1027+
}
1028+
if (typeof value !== "object") return value;
9561029
}
957-
if (typeof value !== "object") return value;
9581030
// Frozen carriers (and every slot of one) are out of the carrier set:
9591031
// rebuilding around them would hand the caller a shape the author sealed.
960-
if (Array.isArray(value)) {
1032+
if (isArray) {
9611033
if (Object.isFrozen(value)) {
9621034
state.seen.set(value, value);
9631035
return value;
9641036
}
965-
const frame = new ScopeFrame(value, null, null, null);
1037+
let i = 0;
1038+
let first;
1039+
while (i < value.length && isLeaf((first = value[i]))) i++;
1040+
if (i === value.length) {
1041+
state.seen.set(value, value);
1042+
return value;
1043+
}
1044+
const frame = new ScopeFrame(value, null, null, i, first);
9661045
state.seen.set(value, frame);
9671046
return frame;
9681047
}
969-
const prototype = Object.getPrototypeOf(value);
970-
if ((prototype !== Object.prototype && prototype !== null) || Object.isFrozen(value)) {
1048+
if (prototype !== Object.prototype && prototype !== null) {
9711049
state.seen.set(value, value);
9721050
return value;
9731051
}
974-
// The shell carries the original descriptors whole — hidden slots,
975-
// accessors and non-writable data stay exactly as authored (accessors are
976-
// the caller's to invoke, non-writable slots are out of the carrier set);
977-
// only writable enumerable data slots are candidates for binding.
978-
const frame = new ScopeFrame(
979-
value,
980-
prototype,
981-
Object.keys(value),
982-
Object.getOwnPropertyDescriptors(value)
983-
);
1052+
// only enumerable slots are candidates; the shell copies every descriptor
1053+
const keys = Object.keys(value);
1054+
let i = 0;
1055+
let first;
1056+
while (
1057+
i < keys.length &&
1058+
isLeaf((first = writableValue(Object.getOwnPropertyDescriptor(value, keys[i]))))
1059+
)
1060+
i++;
1061+
if (i === keys.length) {
1062+
state.seen.set(value, value);
1063+
return value;
1064+
}
1065+
if (Object.isFrozen(value)) {
1066+
state.seen.set(value, value);
1067+
return value;
1068+
}
1069+
const frame = new ScopeFrame(value, prototype, keys, i, first);
9841070
state.seen.set(value, frame);
9851071
return frame;
9861072
}
@@ -989,10 +1075,17 @@ function enterScopeCarrier(value, state) {
9891075
// (#3160): a deep-but-legal result must not overflow the stack and surface
9901076
// as a phantom error. The driver mirrors guardFailures' frame loop.
9911077
function scopeDeferredContents(root, scope) {
992-
const state = { seen: new Map(), cyclic: new Set(), scope };
1078+
const stack = [];
1079+
const state = {
1080+
seen: new Map(),
1081+
cyclic: new Set(),
1082+
scope,
1083+
inert: sharedPrototypesInert(),
1084+
stack
1085+
};
9931086
const entered = enterScopeCarrier(root, state);
9941087
if (!(entered instanceof ScopeFrame)) return entered;
995-
const stack = [entered];
1088+
stack.push(entered);
9961089
let delivered = NOTHING;
9971090
for (;;) {
9981091
const top = stack[stack.length - 1];
@@ -1001,20 +1094,11 @@ function scopeDeferredContents(root, scope) {
10011094
while (top.i < length) {
10021095
const i = top.i;
10031096
let original;
1004-
let writable = true;
1005-
if (top.keys) {
1006-
const descriptor = top.descriptors[top.keys[i]];
1007-
// accessors are not invoked, and non-writable slots keep their
1008-
// authored value (out of the carrier set) — both already ride the
1009-
// shell's copied descriptors
1010-
writable = "value" in descriptor && descriptor.writable === true;
1011-
original = writable ? descriptor.value : undefined;
1097+
if (top.first !== NOTHING) {
1098+
original = top.first;
1099+
top.first = NOTHING;
10121100
} else {
1013-
original = top.value[i];
1014-
}
1015-
if (!writable) {
1016-
top.i++;
1017-
continue;
1101+
original = top.keys ? carrierSlot(top, top.keys[i]) : top.value[i];
10181102
}
10191103
let bound;
10201104
if (delivered !== NOTHING) {
@@ -1055,7 +1139,7 @@ function scopeDeferredContents(root, scope) {
10551139
}
10561140

10571141
function bindDeferredBody(value, scope) {
1058-
if (value === null || (typeof value !== "object" && typeof value !== "function")) return value;
1142+
if (isLeaf(value)) return value;
10591143

10601144
const promised = scope(() => nativePromise(value));
10611145
if (promised) {

‎packages/web/test/server/direct-call-result.bench.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66
// - plain rows: nothing to bind anywhere in the graph
77
// - one deferred body: the same rows plus a cursor, so the root rebuilds
8+
// - nested rows: each row also holds an array, so every row is descended
89

910
import { AsyncLocalStorage } from "node:async_hooks";
1011
import { bench } from "vitest";
@@ -20,6 +21,8 @@ const rows = Array.from({ length: 1000 }, (_, i) => ({
2021
name: `row ${i}`
2122
}));
2223

24+
const nestedRows = rows.map(row => ({ ...row, tags: ["a", "b"] }));
25+
2326
async function* cursor() {
2427
yield "one";
2528
}
@@ -33,6 +36,11 @@ const deferred = createServerReference({
3336
fn: () => ({ rows, more: cursor() })
3437
} as any) as () => { rows: typeof rows };
3538

39+
const nested = createServerReference({
40+
id: "bench-direct-nested",
41+
fn: () => nestedRows
42+
} as any) as () => typeof nestedRows;
43+
3644
const event = createRequestEvent(new Request("https://app.example/page"));
3745

3846
bench("direct call, 1,000 plain rows", () => {
@@ -42,3 +50,7 @@ bench("direct call, 1,000 plain rows", () => {
4250
bench("direct call, 1,000 rows and one deferred body", () => {
4351
requestContext.run(event, () => deferred());
4452
});
53+
54+
bench("direct call, 1,000 rows each holding an array", () => {
55+
requestContext.run(event, () => nested());
56+
});

0 commit comments

Comments
 (0)