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

Commit 8e211b2

Browse files
committed
perf(gc): skip root environment owner scans (#597)
Route Context-embedded Environment stores through the ownerless write barrier while retaining exact owner-aware barriers for managed Environment cells. The external-root path still remembers young targets and shades children published after incremental root tracing. Against exact parent c544df8, 21 paired production cold-lifecycle samples improve from a 27.878 ms median to 13.114 ms (-52.96%) with checksum 1 unchanged. Configuration removes exactly 11,400 slab ownership acquisitions (13,770 -> 2,370), and fixed five-second profiles reduce headerForPayload top-of-stack samples from 1,951 to 17. test262 flips: 0 across global-code (42 -> 42), statements/let (145 -> 145), and statements/const (136 -> 136). Full units: 1,712 passed, 1 skipped, 0 failed, 0 leaked. Focused incremental, nursery, hook ownership, and TSan checks pass.
1 parent c544df8 commit 8e211b2

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/context.zig‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26265,6 +26265,31 @@ test "enable_gc incremental: insertion write barrier keeps a reparented object a
2626526265
try std.testing.expectEqual(@as(f64, 777), tag.asNum());
2626626266
}
2626726267

26268+
test "enable_gc incremental: embedded root Environment shades a late child" {
26269+
const ctx = try Context.createWith(std.testing.allocator, .{ .enable_gc = true, .enable_jit = false });
26270+
defer ctx.destroy();
26271+
ctx.collectGarbage();
26272+
26273+
const gc_saved = gc_mod.setActiveHeap(ctx.gc);
26274+
defer _ = gc_mod.setActiveHeap(gc_saved);
26275+
const heap = ctx.gc.?;
26276+
const child = try gc_mod.allocObj(ctx.arena());
26277+
child.* = .{};
26278+
try child.setOwn(ctx.gpa, ctx.root_shape, "marker", Value.num(42));
26279+
26280+
// Drain the initial root snapshot before publishing the previously-white
26281+
// child through the Context-embedded Environment. This root has no managed
26282+
// owner header, so its ownerless barrier must shade the child directly.
26283+
heap.startMarking();
26284+
_ = heap.markStep(0);
26285+
try ctx.env.put("lateRootChild", Value.obj(child));
26286+
heap.finishMarking();
26287+
26288+
try std.testing.expect(heap.cellMetadata(child) != null);
26289+
const retained = ctx.env.get("lateRootChild") orelse return error.TestUnexpectedResult;
26290+
try std.testing.expectEqual(@as(f64, 42), retained.asObj().getOwn("marker").?.asNum());
26291+
}
26292+
2626826293
test "enable_gc concurrent (M3): a marker thread races a mutator appending into a rooted array" {
2626926294
// Phase 7 / M3, the GC half: prove the collector can mark *concurrently with
2627026295
// a live mutator* against real engine `Object` graphs — the WebKit-Riptide

‎src/interpreter.zig‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,12 @@ pub const Environment = struct {
753753
if (self.gc_managed)
754754
gc_mod.barrierValueFromManaged(self, v)
755755
else
756-
gc_mod.barrierValueFrom(self, v);
756+
// The realm root is embedded in Context, not in a GC cell. Passing
757+
// its address as an owner makes the tolerant barrier exhaust the
758+
// slab/index lookup on every root binding write. The ownerless path
759+
// retains the exact young target and shades it during incremental
760+
// marking, which is the required barrier for an external root.
761+
gc_mod.barrierValue(v);
757762
}
758763

759764
/// Define (or overwrite) a binding in *this* scope (used by let/const).
@@ -1016,6 +1021,34 @@ pub const Environment = struct {
10161021
}
10171022
};
10181023

1024+
test "Environment barriers distinguish embedded roots from managed owners" {
1025+
const Provider = struct {
1026+
calls: usize = 0,
1027+
last_owner: ?*anyopaque = undefined,
1028+
1029+
fn barrier(raw: *anyopaque, owner: ?*anyopaque, _: ?*anyopaque) void {
1030+
const self: *@This() = @ptrCast(@alignCast(raw));
1031+
self.calls += 1;
1032+
self.last_owner = owner;
1033+
}
1034+
};
1035+
1036+
var provider: Provider = .{};
1037+
const previous = gc_runtime.setBarrier(&provider, Provider.barrier, null, null);
1038+
defer _ = gc_runtime.setBarrier(previous[0], previous[1], previous[2], previous[3]);
1039+
1040+
var child = value.Object{};
1041+
var embedded = Environment{ .arena = std.testing.allocator };
1042+
embedded.barrierStoredValue(Value.obj(&child));
1043+
try std.testing.expectEqual(@as(usize, 1), provider.calls);
1044+
try std.testing.expect(provider.last_owner == null);
1045+
1046+
var managed = Environment{ .arena = std.testing.allocator, .gc_managed = true };
1047+
managed.barrierStoredValue(Value.obj(&child));
1048+
try std.testing.expectEqual(@as(usize, 2), provider.calls);
1049+
try std.testing.expectEqual(@intFromPtr(&managed), @intFromPtr(provider.last_owner.?));
1050+
}
1051+
10191052
test "Environment private activation reads elide locks until capture" {
10201053
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
10211054
defer arena.deinit();

0 commit comments

Comments
 (0)