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

Commit 2e48c83

Browse files
committed
perf(json): release replacer membership buckets (#519)
Allocate the invocation-local PropertyList membership buckets from the active precise realm accounted backing allocator and deinitialize them once ordered-key construction completes. Key slices and the ordered allowlist retain their existing arena lifetime for serialization. This prevents warm repeated JSON.stringify calls from retaining one hash table per invocation while preserving the linear membership algorithm. Arena-only interpreters keep their established allocator lifetime; no process-global or cross-realm state is introduced. Focused gate passes normally and under TSan; full unit gate is 1,545 passed, 1 expected skip, 0 failed/leaked; ReleaseFast JSON.stringify test262 remains 66/66, flips 0 cases and regressions 0.
1 parent c823cfb commit 2e48c83

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎src/builtins.zig‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,9 @@ pub fn jsonStringify(ctx: *anyopaque, this: Value, args: []const Value) HostErro
26412641
// PropertyList: read via LengthOfArrayLike + Get(replacer, ToString(i))
26422642
// so a Proxy/array-like replacer's traps run (and abrupts propagate).
26432643
var allow: std.ArrayListUnmanaged([]const u8) = .empty;
2644+
const membership_allocator = gc_mod.temporaryAllocator(a);
26442645
var allow_seen: std.StringHashMapUnmanaged(void) = .empty;
2646+
defer allow_seen.deinit(membership_allocator);
26452647
const rlen = interpreter.toLen(try self.toNumberV(try self.getProperty(replacer, "length")));
26462648
var idx: usize = 0;
26472649
while (idx < rlen) : (idx += 1) {
@@ -2663,7 +2665,7 @@ pub fn jsonStringify(ctx: *anyopaque, this: Value, args: []const Value) HostErro
26632665
}
26642666
if (k) |key| {
26652667
const storage_key = try value.encodeStringKey(a, key);
2666-
const seen = try allow_seen.getOrPut(a, storage_key);
2668+
const seen = try allow_seen.getOrPut(membership_allocator, storage_key);
26672669
if (!seen.found_existing) try allow.append(a, storage_key);
26682670
}
26692671
}

‎src/gc.zig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4235,6 +4235,15 @@ pub inline fn allocationsAreManaged() bool {
42354235
return active_heap != null;
42364236
}
42374237

4238+
/// Freeable allocator for invocation-local indexes whose keys/values are owned
4239+
/// elsewhere. Precise realms use the same accounted backing allocator as their
4240+
/// managed cells; arena-only interpreters fall back to their existing lifetime.
4241+
pub fn temporaryAllocator(fallback: std.mem.Allocator) std.mem.Allocator {
4242+
if (active_heap == null) return fallback;
4243+
const realm = active_realm_context orelse return fallback;
4244+
return realm.gpa;
4245+
}
4246+
42384247
/// Relocation-stable identity for a live cell in the current precise heap.
42394248
/// Map/Set hashing calls this only from a mutator-owned operation; moving
42404249
/// collection is world-stopped and concurrent marking never reclaims cells.

0 commit comments

Comments
 (0)