镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 3ec9847

Browse files
committed
perf(json): stream nested stringify output (#558)
Serialize object members directly into the invocation output buffer and roll back to an exact mark when a property is omitted. This removes the per-ancestor descendant copy while preserving comma, indentation, getter, toJSON, replacer, Proxy, and omission ordering. Back the unpublished output with the Context temporary allocator, then copy only the completed result into managed storage. Allocation failure now releases partial output exactly, leaves the assignment target unpublished, and stays within the configured heap limit. Test262 impact: test/built-ins/JSON/stringify remains 66 -> 66 passing (0 flips, 0 failures). Validation: JSON units 11/11 in Debug and TSan; bounded precise-GC OOM witness; forced tree-walker/required-bytecode and moving-compaction witnesses; full units 1632 passed, 1 expected skip, 0 failed, 0 leaked; all six benchmark smoke checksums preserved. Issue: #558
1 parent ac9dc52 commit 3ec9847

3 files changed

Lines changed: 100 additions & 32 deletions

File tree

‎src/builtins.zig‎

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,9 +2616,13 @@ pub fn jsonStringify(ctx: *anyopaque, this: Value, args: []const Value) HostErro
26162616
self.env = env;
26172617
};
26182618
const a = self.arena;
2619-
const cycle_allocator = gc_mod.temporaryAllocator(a);
2620-
var st = Stringifier{ .self = self, .cycle_allocator = cycle_allocator };
2621-
defer st.active.deinit(a, cycle_allocator);
2619+
const temporary_allocator = gc_mod.temporaryAllocator(a);
2620+
var st = Stringifier{
2621+
.self = self,
2622+
.cycle_allocator = temporary_allocator,
2623+
.output_allocator = temporary_allocator,
2624+
};
2625+
defer st.active.deinit(a, temporary_allocator);
26222626

26232627
// arg 1 — replacer: a callable (transform) or an array (property allowlist).
26242628
const replacer = arg(args, 1);
@@ -2698,15 +2702,19 @@ pub fn jsonStringify(ctx: *anyopaque, this: Value, args: []const Value) HostErro
26982702
const holder = (try self.newObject()).asObj();
26992703
try holder.setOwn(self.arena, self.root_shape, "", arg(args, 0));
27002704
var buf: std.ArrayListUnmanaged(u8) = .empty;
2705+
defer buf.deinit(temporary_allocator);
27012706
if (!try st.serialize(&buf, Value.obj(holder), "")) return Value.undef();
2702-
return try Value.strOwned(a, try buf.toOwnedSlice(a));
2707+
if (temporary_allocator.ptr == a.ptr and temporary_allocator.vtable == a.vtable)
2708+
return try Value.strOwned(a, try buf.toOwnedSlice(a));
2709+
return try Value.strAlloc(a, buf.items);
27032710
}
27042711

27052712
/// Carries the `JSON.stringify` options (replacer / allowlist / indent gap)
27062713
/// and the cycle-detection stack across the recursive serialization.
27072714
const Stringifier = struct {
27082715
self: *Interpreter,
27092716
cycle_allocator: std.mem.Allocator,
2717+
output_allocator: std.mem.Allocator,
27102718
replacer_fn: ?Value = null,
27112719
allow: ?[]const []const u8 = null,
27122720
gap: []const u8 = "",
@@ -2725,6 +2733,7 @@ const Stringifier = struct {
27252733
defer self.depth -= 1;
27262734
try self.stackGuard();
27272735
const a = self.arena;
2736+
const output_allocator = st.output_allocator;
27282737
var v = try self.getProperty(holder, key);
27292738
if (v.isObject() and !v.asObj().is_symbol) {
27302739
const tj = try self.getProperty(v, "toJSON");
@@ -2736,7 +2745,7 @@ const Stringifier = struct {
27362745
// A JSON.rawJSON object emits its validated text verbatim.
27372746
if (v.isObject() and v.asObj().behavior.is_raw_json) {
27382747
const raw = (v.asObj().getOwn("rawJSON") orelse Value.str("")).asStr();
2739-
try buf.appendSlice(a, raw);
2748+
try buf.appendSlice(output_allocator, raw);
27402749
return true;
27412750
}
27422751
// SerializeJSONProperty: a [[NumberData]] wrapper → ToNumber, a
@@ -2762,13 +2771,13 @@ const Stringifier = struct {
27622771
return self.throwError("TypeError", "Do not know how to serialize a BigInt");
27632772
switch (v.kind()) {
27642773
.undefined => return false,
2765-
.null => try buf.appendSlice(a, "null"),
2766-
.boolean => try buf.appendSlice(a, if (v.asBool()) "true" else "false"),
2774+
.null => try buf.appendSlice(output_allocator, "null"),
2775+
.boolean => try buf.appendSlice(output_allocator, if (v.asBool()) "true" else "false"),
27672776
.number => {
27682777
const n = v.asNum();
2769-
try buf.appendSlice(a, if (std.math.isNan(n) or std.math.isInf(n)) "null" else try value.numberToString(a, n));
2778+
try buf.appendSlice(output_allocator, if (std.math.isNan(n) or std.math.isInf(n)) "null" else try value.numberToString(a, n));
27702779
},
2771-
.string => try writeJsonString(a, buf, try v.asWtf8(a)),
2780+
.string => try writeJsonString(output_allocator, buf, try v.asWtf8(a)),
27722781
.object => {
27732782
const o = v.asObj();
27742783
if (o.isCallableObject() or o.is_symbol) return false; // functions/symbols omitted
@@ -2800,34 +2809,36 @@ const Stringifier = struct {
28002809

28012810
fn serializeArray(st: *Stringifier, buf: *std.ArrayListUnmanaged(u8), holder: Value, shape: *value.Object) HostError!void {
28022811
const a = st.self.arena;
2812+
const output_allocator = st.output_allocator;
28032813
// SerializeJSONArray reads the length via LengthOfArrayLike (Get) — for a
28042814
// Proxy that runs the "length" trap (and propagates an abrupt completion).
28052815
const len: usize = if (holder.isObject() and (holder.asObj().proxyHandler() != null or holder.asObj().proxy_revoked))
28062816
interpreter.toLen(try st.self.toNumberV(try st.self.getProperty(holder, "length")))
28072817
else
28082818
shape.arrayLength();
28092819
if (len == 0) {
2810-
try buf.appendSlice(a, "[]");
2820+
try buf.appendSlice(output_allocator, "[]");
28112821
return;
28122822
}
28132823
const outer = st.indent.items.len;
28142824
try st.indent.appendSlice(a, st.gap);
2815-
try buf.append(a, '[');
2825+
try buf.append(output_allocator, '[');
28162826
var i: usize = 0;
28172827
while (i < len) : (i += 1) {
2818-
if (i != 0) try buf.append(a, ',');
2828+
if (i != 0) try buf.append(output_allocator, ',');
28192829
try st.newlineIndent(buf);
28202830
const key = try std.fmt.allocPrint(a, "{d}", .{i});
2821-
if (!try st.serialize(buf, holder, key)) try buf.appendSlice(a, "null");
2831+
if (!try st.serialize(buf, holder, key)) try buf.appendSlice(output_allocator, "null");
28222832
}
28232833
st.indent.shrinkRetainingCapacity(outer);
28242834
try st.newlineIndent(buf);
2825-
try buf.append(a, ']');
2835+
try buf.append(output_allocator, ']');
28262836
}
28272837

28282838
fn serializeObject(st: *Stringifier, buf: *std.ArrayListUnmanaged(u8), v: Value, shape: *value.Object) HostError!void {
28292839
const self = st.self;
28302840
const a = self.arena;
2841+
const output_allocator = st.output_allocator;
28312842
const keys = if (st.allow) |al| al else try st.jsonObjectKeys(v.asObj(), shape);
28322843
// A Proxy's enumerability comes from [[GetOwnProperty]] (the
28332844
// getOwnPropertyDescriptor trap), which EnumerableOwnPropertyNames runs
@@ -2836,8 +2847,8 @@ const Stringifier = struct {
28362847
const is_proxy = v.asObj().proxyHandler() != null or v.asObj().proxy_revoked;
28372848
const outer = st.indent.items.len;
28382849
try st.indent.appendSlice(a, st.gap);
2839-
var tmp: std.ArrayListUnmanaged(u8) = .empty;
28402850
var count: usize = 0;
2851+
try buf.append(output_allocator, '{');
28412852
for (keys) |k| {
28422853
if (jsonHiddenKey(k)) continue;
28432854
if (st.allow == null) {
@@ -2847,25 +2858,28 @@ const Stringifier = struct {
28472858
} else shape.getAttr(k).enumerable;
28482859
if (!enumerable) continue;
28492860
}
2850-
var member: std.ArrayListUnmanaged(u8) = .empty;
2851-
if (!try st.serialize(&member, v, k)) continue; // omitted property
2852-
if (count != 0) try tmp.append(a, ',');
2853-
try st.newlineIndentTo(&tmp, st.indent.items);
2854-
try writeJsonString(a, &tmp, value.decodeStringKey(k));
2855-
try tmp.append(a, ':');
2856-
if (st.gap.len != 0) try tmp.append(a, ' ');
2857-
try tmp.appendSlice(a, member.items);
2861+
// Serialize directly into the authoritative output. A temporary
2862+
// member buffer recopies a successful descendant suffix at every
2863+
// ancestor, making a depth-n chain quadratic in output bytes. The
2864+
// mark retains exact omission semantics without publishing a comma
2865+
// or key when SerializeJSONProperty returns undefined.
2866+
const mark = buf.items.len;
2867+
if (count != 0) try buf.append(output_allocator, ',');
2868+
try st.newlineIndent(buf);
2869+
try writeJsonString(output_allocator, buf, value.decodeStringKey(k));
2870+
try buf.append(output_allocator, ':');
2871+
if (st.gap.len != 0) try buf.append(output_allocator, ' ');
2872+
if (!try st.serialize(buf, v, k)) {
2873+
buf.shrinkRetainingCapacity(mark);
2874+
continue;
2875+
}
28582876
count += 1;
28592877
}
28602878
st.indent.shrinkRetainingCapacity(outer);
2861-
if (count == 0) {
2862-
try buf.appendSlice(a, "{}");
2863-
return;
2879+
if (count != 0) {
2880+
try st.newlineIndent(buf);
28642881
}
2865-
try buf.append(a, '{');
2866-
try buf.appendSlice(a, tmp.items);
2867-
try st.newlineIndent(buf);
2868-
try buf.append(a, '}');
2882+
try buf.append(output_allocator, '}');
28692883
}
28702884

28712885
fn jsonObjectKeys(st: *Stringifier, object: *value.Object, shape: *value.Object) HostError![]const []const u8 {
@@ -2886,8 +2900,8 @@ const Stringifier = struct {
28862900
}
28872901
fn newlineIndentTo(st: *Stringifier, buf: *std.ArrayListUnmanaged(u8), ind: []const u8) HostError!void {
28882902
if (st.gap.len == 0) return;
2889-
try buf.append(st.self.arena, '\n');
2890-
try buf.appendSlice(st.self.arena, ind);
2903+
try buf.append(st.output_allocator, '\n');
2904+
try buf.appendSlice(st.output_allocator, ind);
28912905
}
28922906
};
28932907

‎src/context.zig‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11495,6 +11495,37 @@ test "enable_gc: JSON stringify active index survives a callback collection requ
1149511495
)).asBool());
1149611496
}
1149711497

11498+
test "heap_limit_bytes: JSON stringify direct object output fails atomically" {
11499+
const ctx = try Context.createWithTestingOptions(std.testing.allocator, .{
11500+
.enable_gc = true,
11501+
.heap_limit_bytes = 8 * 1024 * 1024,
11502+
});
11503+
defer ctx.destroy();
11504+
11505+
const result = try ctx.evaluate(
11506+
\\let jsonChunk = "\u0000";
11507+
\\for (let i = 0; i < 18; i++) jsonChunk += jsonChunk;
11508+
\\let jsonSource = { omittedFirst: undefined };
11509+
\\for (let i = 0; i < 8; i++) jsonSource["field" + i] = jsonChunk;
11510+
\\jsonSource.omittedLast = function () {};
11511+
\\let jsonCallbacks = 0;
11512+
\\globalThis.jsonPublished = "sentinel";
11513+
\\let jsonCaught = false;
11514+
\\try {
11515+
\\ jsonPublished = JSON.stringify(jsonSource, function (key, value) {
11516+
\\ jsonCallbacks++;
11517+
\\ return value;
11518+
\\ });
11519+
\\} catch (error) {
11520+
\\ jsonCaught = error.name === "OutOfMemoryError";
11521+
\\}
11522+
\\jsonCaught && jsonCallbacks > 1 && jsonPublished === "sentinel";
11523+
);
11524+
try std.testing.expect(result.asBool());
11525+
const stats = ctx.heapBudgetStats().?;
11526+
try std.testing.expect(stats.peak_bytes <= stats.limit_bytes);
11527+
}
11528+
1149811529
test "RegExp constructor honors IsRegExp and flags rules" {
1149911530
try std.testing.expect((try evalIn(
1150011531
\\var re = /foo/my;

‎src/interpreter.zig‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50343,6 +50343,29 @@ test "interpreter JSON, Object, Number builtins" {
5034350343
const a = arena.allocator();
5034450344
try std.testing.expectEqualStrings("{\"a\":1,\"b\":[2,3]}", (try evalSource(a, "JSON.stringify({ a: 1, b: [2, 3] })")).asStr());
5034550345
try std.testing.expectEqualStrings("[1,\"x\",true,null]", (try evalSource(a, "JSON.stringify([1, 'x', true, null])")).asStr());
50346+
try std.testing.expectEqualStrings("{\"a\":1,\"b\":2}", (try evalSource(a,
50347+
\\JSON.stringify({ first: undefined, a: 1, middle: function () {}, b: 2, last: Symbol("last") })
50348+
)).asStr());
50349+
try std.testing.expectEqualStrings(
50350+
"{\n \"a\": {\n \"b\": 1\n },\n \"c\": [\n 2\n ]\n}",
50351+
(try evalSource(a, "JSON.stringify({ a: { b: 1 }, c: [2] }, null, 2)")).asStr(),
50352+
);
50353+
try std.testing.expect((try evalSource(a,
50354+
\\let stringifyTrace = [];
50355+
\\let stringifyObserved = {
50356+
\\ get a() {
50357+
\\ stringifyTrace.push("get-a");
50358+
\\ return { toJSON() { stringifyTrace.push("toJSON-a"); return 1; } };
50359+
\\ },
50360+
\\ get b() { stringifyTrace.push("get-b"); return 2; }
50361+
\\};
50362+
\\let stringifyOutput = JSON.stringify(stringifyObserved, function (key, current) {
50363+
\\ stringifyTrace.push("replacer-" + key);
50364+
\\ return key === "b" ? undefined : current;
50365+
\\});
50366+
\\stringifyOutput === '{"a":1}' &&
50367+
\\ stringifyTrace.join(",") === "replacer-,get-a,toJSON-a,replacer-a,get-b,replacer-b"
50368+
)).asBool());
5034650369
try std.testing.expect((try evalSource(a,
5034750370
\\let shared = { value: 1 };
5034850371
\\let repeated = JSON.stringify([shared, shared]) === '[{"value":1},{"value":1}]';

0 commit comments

Comments
 (0)