perf(json): encode streamed JSON iteratively (#1322)
The streamed JSON encoder delegated every token through one recursive generator per nesting level and yielded each token separately. Writing analyze-javascript-application output for Typora (873 MB) spent 22 of 66 s there. Encode with an explicit stack into bounded 16 KiB parts: identical bytes, 3-6x faster encoding, and the same laziness, error and surrogate-pair guarantees. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d83cbd2abf
commit
56598beb4e
@@ -7,6 +7,16 @@ import {
|
|||||||
jsonParts,
|
jsonParts,
|
||||||
} from "./jsonSerialization.js";
|
} from "./jsonSerialization.js";
|
||||||
|
|
||||||
|
/** Compact and pretty streamed JSON must equal native JSON. */
|
||||||
|
const expectNativeJson = (value: unknown): void => {
|
||||||
|
expect([...bufferedJsonParts(jsonParts(value))].join("")).toBe(
|
||||||
|
JSON.stringify(value),
|
||||||
|
);
|
||||||
|
expect([...jsonParts(value, true)].join("")).toBe(
|
||||||
|
JSON.stringify(value, null, 2),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
describe("streamed JSON representation", () => {
|
describe("streamed JSON representation", () => {
|
||||||
it.each([
|
it.each([
|
||||||
null,
|
null,
|
||||||
@@ -15,14 +25,7 @@ describe("streamed JSON representation", () => {
|
|||||||
[1, false, null, 'quotes: " / slash: \\ / newline: \n'],
|
[1, false, null, 'quotes: " / slash: \\ / newline: \n'],
|
||||||
{ nested: { value: "中文 😀 \ud800" }, items: [1, 2] },
|
{ nested: { value: "中文 😀 \ud800" }, items: [1, 2] },
|
||||||
JSON.parse('{"__proto__":{"value":1},"constructor":2,"10":3,"2":4}'),
|
JSON.parse('{"__proto__":{"value":1},"constructor":2,"10":3,"2":4}'),
|
||||||
])("matches native JSON for ordinary values: %j", (value: unknown) => {
|
])("matches native JSON for ordinary values: %j", expectNativeJson);
|
||||||
expect([...bufferedJsonParts(jsonParts(value))].join("")).toBe(
|
|
||||||
JSON.stringify(value),
|
|
||||||
);
|
|
||||||
expect([...jsonParts(value, true)].join("")).toBe(
|
|
||||||
JSON.stringify(value, null, 2),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("preserves surrogate pairs across string and buffer boundaries", () => {
|
it("preserves surrogate pairs across string and buffer boundaries", () => {
|
||||||
const value = { text: `${"x".repeat(8190)}😀${'中文\n"'.repeat(20000)}` };
|
const value = { text: `${"x".repeat(8190)}😀${'中文\n"'.repeat(20000)}` };
|
||||||
@@ -59,3 +62,55 @@ describe("streamed JSON representation", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("iterative JSON encoding", () => {
|
||||||
|
it("matches native JSON for long keys, omitted members, and deep nesting", () => {
|
||||||
|
let deep: unknown = { leaf: [1, "two", null] };
|
||||||
|
for (let level = 0; level < 500; level += 1)
|
||||||
|
deep = level % 2 === 0 ? [deep, level] : { [`k${String(level)}`]: deep };
|
||||||
|
const value = {
|
||||||
|
[`${"k".repeat(9000)}😀`]: { nested: ["x".repeat(20000), {}, []] },
|
||||||
|
omitted: undefined,
|
||||||
|
method: () => 1,
|
||||||
|
symbol: Symbol("ignored"),
|
||||||
|
deep,
|
||||||
|
};
|
||||||
|
expectNativeJson(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects values native JSON would not encode as ordinary JSON", () => {
|
||||||
|
expect(() => [...jsonParts([1, undefined])]).toThrow(
|
||||||
|
"ordinary JSON values",
|
||||||
|
);
|
||||||
|
expect(() => [...jsonParts({ value: Number.NaN })]).toThrow(
|
||||||
|
"ordinary JSON values",
|
||||||
|
);
|
||||||
|
expect(() => [...jsonParts({ value: new Date(0) })]).toThrow(
|
||||||
|
"ordinary JSON objects",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("yields bounded parts before reading later members", () => {
|
||||||
|
const value: Record<string, unknown> = { first: "x".repeat(100_000) };
|
||||||
|
Object.defineProperty(value, "later", {
|
||||||
|
enumerable: true,
|
||||||
|
get() {
|
||||||
|
throw new Error("later members must not be read yet");
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const parts = jsonParts(value);
|
||||||
|
const first = parts.next();
|
||||||
|
expect(first.done).toBe(false);
|
||||||
|
expect(first.value?.length).toBeLessThanOrEqual(64 * 1024);
|
||||||
|
// Many small members are grouped into parts instead of one per token.
|
||||||
|
const small = Object.fromEntries(
|
||||||
|
Array.from({ length: 10_000 }, (_, index) => [
|
||||||
|
`k${String(index)}`,
|
||||||
|
index,
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
const smallParts = [...jsonParts(small)];
|
||||||
|
expect(smallParts.join("")).toBe(JSON.stringify(small));
|
||||||
|
expect(smallParts.length).toBeLessThan(20);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
+130
-60
@@ -1,11 +1,11 @@
|
|||||||
/** Encode ordinary JSON as bounded string parts without a document-sized allocation. */
|
/** Encode ordinary JSON as bounded string parts without a document-sized allocation. */
|
||||||
export function* jsonParts(value: unknown, pretty = false): Generator<string> {
|
export function* jsonParts(value: unknown, pretty = false): Generator<string> {
|
||||||
yield* encodeValue(value, pretty, 0, new Set(), false);
|
yield* encodeValue(value, pretty, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Encode validated JSON with canonical key ordering without allocating the whole document. */
|
/** Encode validated JSON with canonical key ordering without allocating the whole document. */
|
||||||
export function* canonicalJsonParts(value: unknown): Generator<string> {
|
export function* canonicalJsonParts(value: unknown): Generator<string> {
|
||||||
yield* encodeValue(value, false, 0, new Set(), true);
|
yield* encodeValue(value, false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Buffer JSON parts while preserving complete UTF-16 surrogate pairs. */
|
/** Buffer JSON parts while preserving complete UTF-16 surrogate pairs. */
|
||||||
@@ -46,73 +46,143 @@ function* encodeString(value: string): Generator<string> {
|
|||||||
yield '"';
|
yield '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
function* encodeValue(
|
/** Encoded text accumulated before it is yielded as one part. */
|
||||||
|
const PART_CHARACTERS = 16 * 1024;
|
||||||
|
|
||||||
|
const indentation: string[] = [];
|
||||||
|
const indent = (depth: number): string =>
|
||||||
|
(indentation[depth] ??= `\n${" ".repeat(depth)}`);
|
||||||
|
|
||||||
|
interface ContainerFrame {
|
||||||
|
readonly value: object;
|
||||||
|
readonly array: boolean;
|
||||||
|
readonly keys: readonly string[] | undefined;
|
||||||
|
readonly count: number;
|
||||||
|
readonly depth: number;
|
||||||
|
index: number;
|
||||||
|
written: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Encoded text of a scalar, or undefined for a container or a long string. */
|
||||||
|
const scalarText = (value: unknown): string | undefined => {
|
||||||
|
if (typeof value === "string")
|
||||||
|
return value.length <= 8192 ? JSON.stringify(value) : undefined;
|
||||||
|
if (value === null) return "null";
|
||||||
|
if (typeof value === "boolean") return String(value);
|
||||||
|
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
const openContainer = (
|
||||||
value: unknown,
|
value: unknown,
|
||||||
pretty: boolean,
|
|
||||||
depth: number,
|
depth: number,
|
||||||
ancestors: Set<object>,
|
ancestors: Set<object>,
|
||||||
canonical: boolean,
|
canonical: boolean,
|
||||||
): Generator<string> {
|
): ContainerFrame => {
|
||||||
if (typeof value === "string") {
|
|
||||||
yield* encodeString(value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (value === null || typeof value === "boolean") {
|
|
||||||
yield value === null ? "null" : String(value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (typeof value === "number" && Number.isFinite(value)) {
|
|
||||||
yield String(value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (typeof value !== "object" || value === null)
|
if (typeof value !== "object" || value === null)
|
||||||
throw new TypeError("JSON output requires ordinary JSON values");
|
throw new TypeError("JSON output requires ordinary JSON values");
|
||||||
if (ancestors.has(value))
|
if (ancestors.has(value))
|
||||||
throw new TypeError("JSON output contains a circular reference");
|
throw new TypeError("JSON output contains a circular reference");
|
||||||
if (
|
const array = Array.isArray(value);
|
||||||
!Array.isArray(value) &&
|
if (!array && Object.prototype.toString.call(value) !== "[object Object]")
|
||||||
Object.prototype.toString.call(value) !== "[object Object]"
|
|
||||||
)
|
|
||||||
throw new TypeError("JSON output requires ordinary JSON objects");
|
throw new TypeError("JSON output requires ordinary JSON objects");
|
||||||
ancestors.add(value);
|
ancestors.add(value);
|
||||||
try {
|
const keys = array ? undefined : Object.keys(value);
|
||||||
const array = Array.isArray(value);
|
if (canonical) keys?.sort();
|
||||||
const keys = array ? undefined : Object.keys(value);
|
const count = array ? value.length : (keys?.length ?? 0);
|
||||||
if (canonical) keys?.sort();
|
return { value, array, keys, count, depth, index: 0, written: 0 };
|
||||||
const count = array ? value.length : (keys?.length ?? 0);
|
};
|
||||||
let written = 0;
|
|
||||||
yield array ? "[" : "{";
|
/** The next written member of a container, skipping values JSON omits. */
|
||||||
for (let index = 0; index < count; index += 1) {
|
const nextMember = (
|
||||||
let item: unknown;
|
frame: ContainerFrame,
|
||||||
let key: string | undefined;
|
): { readonly key: string | undefined; readonly item: unknown } | undefined => {
|
||||||
if (array) {
|
while (frame.index < frame.count) {
|
||||||
item = value[index];
|
const index = frame.index;
|
||||||
} else {
|
frame.index += 1;
|
||||||
key = keys?.[index];
|
if (frame.array)
|
||||||
if (key === undefined)
|
return { key: undefined, item: Reflect.get(frame.value, index) };
|
||||||
throw new TypeError("JSON object changed during serialization");
|
const key = frame.keys?.[index];
|
||||||
item = Reflect.get(value, key);
|
if (key === undefined)
|
||||||
// Incur's filter can select inherited methods. Match native JSON's
|
throw new TypeError("JSON object changed during serialization");
|
||||||
// omission without eagerly reading the rest of a bounded result.
|
const item: unknown = Reflect.get(frame.value, key);
|
||||||
if (
|
// Incur's filter can select inherited methods. Match native JSON's
|
||||||
item === undefined ||
|
// omission without eagerly reading the rest of a bounded result.
|
||||||
typeof item === "function" ||
|
if (
|
||||||
typeof item === "symbol"
|
item !== undefined &&
|
||||||
)
|
typeof item !== "function" &&
|
||||||
continue;
|
typeof item !== "symbol"
|
||||||
}
|
)
|
||||||
if (written > 0) yield ",";
|
return { key, item };
|
||||||
if (pretty) yield `\n${" ".repeat(depth + 1)}`;
|
|
||||||
if (key !== undefined) {
|
|
||||||
yield* encodeString(key);
|
|
||||||
yield pretty ? ": " : ":";
|
|
||||||
}
|
|
||||||
yield* encodeValue(item, pretty, depth + 1, ancestors, canonical);
|
|
||||||
written += 1;
|
|
||||||
}
|
|
||||||
if (pretty && written > 0) yield `\n${" ".repeat(depth)}`;
|
|
||||||
yield array ? "]" : "}";
|
|
||||||
} finally {
|
|
||||||
ancestors.delete(value);
|
|
||||||
}
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode iteratively into bounded parts. Recursive `yield*` delegation passed
|
||||||
|
* every token through one generator frame per nesting level, which dominated
|
||||||
|
* the cost of writing large nested results.
|
||||||
|
*/
|
||||||
|
function* encodeValue(
|
||||||
|
root: unknown,
|
||||||
|
pretty: boolean,
|
||||||
|
canonical: boolean,
|
||||||
|
): Generator<string> {
|
||||||
|
const ancestors = new Set<object>();
|
||||||
|
const stack: ContainerFrame[] = [];
|
||||||
|
let buffer = "";
|
||||||
|
let pending: unknown = root;
|
||||||
|
let hasPending = true;
|
||||||
|
while (true) {
|
||||||
|
if (hasPending) {
|
||||||
|
hasPending = false;
|
||||||
|
const text = scalarText(pending);
|
||||||
|
if (text !== undefined) buffer += text;
|
||||||
|
else if (typeof pending === "string") {
|
||||||
|
// A long string is split into surrogate-safe parts of its own.
|
||||||
|
if (buffer !== "") yield buffer;
|
||||||
|
buffer = "";
|
||||||
|
yield* encodeString(pending);
|
||||||
|
} else {
|
||||||
|
const frame = openContainer(
|
||||||
|
pending,
|
||||||
|
stack.length,
|
||||||
|
ancestors,
|
||||||
|
canonical,
|
||||||
|
);
|
||||||
|
stack.push(frame);
|
||||||
|
buffer += frame.array ? "[" : "{";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (buffer.length >= PART_CHARACTERS) {
|
||||||
|
yield buffer;
|
||||||
|
buffer = "";
|
||||||
|
}
|
||||||
|
const frame = stack.at(-1);
|
||||||
|
if (frame === undefined) break;
|
||||||
|
const member = nextMember(frame);
|
||||||
|
if (member === undefined) {
|
||||||
|
if (pretty && frame.written > 0) buffer += indent(frame.depth);
|
||||||
|
buffer += frame.array ? "]" : "}";
|
||||||
|
ancestors.delete(frame.value);
|
||||||
|
stack.pop();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (frame.written > 0) buffer += ",";
|
||||||
|
if (pretty) buffer += indent(frame.depth + 1);
|
||||||
|
if (member.key !== undefined) {
|
||||||
|
const key = scalarText(member.key);
|
||||||
|
if (key !== undefined) buffer += key;
|
||||||
|
else {
|
||||||
|
if (buffer !== "") yield buffer;
|
||||||
|
buffer = "";
|
||||||
|
yield* encodeString(member.key);
|
||||||
|
}
|
||||||
|
buffer += pretty ? ": " : ":";
|
||||||
|
}
|
||||||
|
frame.written += 1;
|
||||||
|
pending = member.item;
|
||||||
|
hasPending = true;
|
||||||
|
}
|
||||||
|
if (buffer !== "") yield buffer;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user