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

Commit 6a6b508

Browse files
committed
perf(environment): elide private activation read locks (#533)
Mark fresh tree-walker call environments and their uncaptured nested scopes as mutator-private. Binding reads elide the mutex only while that proof holds; closure creation revokes the entire captured chain before publication, while every write remains synchronized with the concurrent marker. The representative transition workload keeps exact checksums while invocation Environment lock acquisitions fall 376876 -> 36299 at 4 lanes and 753608 -> 72531 at 8 lanes. This counter reduction is diagnostic, not a public throughput claim. Validation: full units 1587 passed / 1 skipped / 0 failed / 0 leaked; focused closure and concurrent-GC TSan clean; threadfuzz 400 programs / 0 failures. test262 test/language stays 23648 pass / 0 fail, so this flips 0 test262 cases.
1 parent 3d3b9ad commit 6a6b508

1 file changed

Lines changed: 107 additions & 43 deletions

File tree

‎src/interpreter.zig‎

Lines changed: 107 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ pub const Environment = struct {
578578
/// for a block `{…}` scope. `var`/function declarations hoist to the nearest
579579
/// variable environment, while `let`/`const`/`class` bind in the block.
580580
fn_scope: bool = false,
581+
/// True while this declarative Environment belongs exclusively to one
582+
/// running function activation. The concurrent GC marker may inspect it,
583+
/// but only the owning mutator can read or write its bindings. Binding
584+
/// writes still take `binding_lock` against the marker; binding reads can
585+
/// skip it because the marker never mutates the tables. Closure capture
586+
/// clears this flag before the Environment can be published to another
587+
/// mutator. Root and module environments never opt into this proof; nested
588+
/// scopes inherit it only from a still-private activation.
589+
private_activation: bool = false,
581590
/// True for the *function body* block — a real block env (so it can hold the
582591
/// body's top-level `let`/`const`) but treated like the variable scope for
583592
/// function-declaration hoisting and Annex B B.3.3 analysis: a function
@@ -598,9 +607,10 @@ pub const Environment = struct {
598607
/// Phase 7 / M3: serializes the binding storage a concurrent GC marker reads
599608
/// in `traceEnv` (`vars` values, `disposables`, `aliases`) against the
600609
/// mutator's binding writes (`put`/`assign`/`putAlias`, `using` registration).
601-
/// Like `Object.property_lock`: writers take it; lookups (`get`/`isConst`/…),
602-
/// which only read, do not. Uncontended when the GC is off or not marking
603-
/// concurrently (a single atomic CAS, mirroring the Object element locks).
610+
/// Like `Object.property_lock`: writers and shared/captured lookups take it.
611+
/// Reads from an uncaptured private activation may elide it because only the
612+
/// owning mutator can change those tables and the concurrent marker is
613+
/// read-only. The uncontended shared path is a single atomic CAS.
604614
binding_lock: std.atomic.Mutex = .unlocked,
605615

606616
/// Binding locks are only needed when a context runs the GC marker
@@ -627,6 +637,18 @@ pub const Environment = struct {
627637
self.binding_lock.unlock();
628638
}
629639

640+
/// Acquire the binding lock for a table read unless this is an uncaptured,
641+
/// mutator-private activation. Returns whether the caller must unlock.
642+
fn lockBindingsForRead(self: *Environment) bool {
643+
if (self.private_activation and !self.captured) return false;
644+
self.lockBindings();
645+
return true;
646+
}
647+
648+
fn unlockBindingsForRead(self: *Environment, locked: bool) void {
649+
if (locked) self.unlockBindings();
650+
}
651+
630652
/// Define (or overwrite) a binding in *this* scope (used by let/const).
631653
pub fn put(self: *Environment, name: []const u8, v: Value) EvalError!void {
632654
gc_mod.barrierValueFrom(self, v); // binding stored into this GC-cell environment
@@ -697,13 +719,13 @@ pub const Environment = struct {
697719
pub fn isFnName(self: *Environment, name: []const u8) bool {
698720
var env: ?*Environment = self;
699721
while (env) |e| {
700-
e.lockBindings(); // see `get`: per-env read lock vs concurrent binding writes
722+
const locked = e.lockBindingsForRead();
701723
if (e.vars.contains(name)) {
702724
const r = e.fn_names.contains(name);
703-
e.unlockBindings();
725+
e.unlockBindingsForRead(locked);
704726
return r;
705727
}
706-
e.unlockBindings();
728+
e.unlockBindingsForRead(locked);
707729
env = e.parent;
708730
}
709731
return false;
@@ -714,13 +736,13 @@ pub const Environment = struct {
714736
pub fn isConst(self: *Environment, name: []const u8) ?bool {
715737
var env: ?*Environment = self;
716738
while (env) |e| {
717-
e.lockBindings();
739+
const locked = e.lockBindingsForRead();
718740
if (e.vars.contains(name)) {
719741
const r = e.consts.contains(name);
720-
e.unlockBindings();
742+
e.unlockBindingsForRead(locked);
721743
return r;
722744
}
723-
e.unlockBindings();
745+
e.unlockBindingsForRead(locked);
724746
env = e.parent;
725747
}
726748
return null;
@@ -736,6 +758,10 @@ pub const Environment = struct {
736758
var e: ?*Environment = self;
737759
while (e) |env| : (e = env.parent) {
738760
if (env.captured) break;
761+
// This executes in the defining mutator before the new closure can
762+
// be published. Once cleared, every future read uses the same lock
763+
// as a concurrent mutator's structural writes.
764+
env.private_activation = false;
739765
env.captured = true;
740766
}
741767
}
@@ -761,7 +787,8 @@ pub const Environment = struct {
761787
// lookup here races the grow — the systemic no-GIL Environment race
762788
// (Linux tsan-threadfuzz: globalDefine put vs assign getPtr). `ptr`
763789
// stays valid for the write because no rehash can occur under the
764-
// lock. Flag-gated, so the default engine stays lock-free.
790+
// lock. Private activations still take it for writes because the
791+
// concurrent marker can read their tables.
765792
e.lockBindings();
766793
if (e.vars.getPtr(name)) |ptr| {
767794
gc_mod.barrierValueFrom(e, v); // reassigned binding in a GC-cell env
@@ -795,16 +822,16 @@ pub const Environment = struct {
795822
pub fn isAlias(self: *Environment, name: []const u8) bool {
796823
var env: ?*Environment = self;
797824
while (env) |e| {
798-
e.lockBindings();
825+
const locked = e.lockBindingsForRead();
799826
if (e.aliases.contains(name)) {
800-
e.unlockBindings();
827+
e.unlockBindingsForRead(locked);
801828
return true;
802829
}
803830
if (e.vars.contains(name)) {
804-
e.unlockBindings();
831+
e.unlockBindingsForRead(locked);
805832
return false;
806833
}
807-
e.unlockBindings();
834+
e.unlockBindingsForRead(locked);
808835
env = e.parent;
809836
}
810837
return false;
@@ -815,23 +842,22 @@ pub const Environment = struct {
815842
pub fn get(self: *Environment, name: []const u8) ?Value {
816843
var env: ?*Environment = self;
817844
while (env) |e| {
818-
// Read each scope's binding tables under its own `binding_lock` so a
819-
// concurrent `put`/`putAlias` rehash on a shared env (post-GIL) can't
820-
// tear the read or free the table under us. Locked per-env, never
821-
// nested (the alias recursion runs after unlock), so no deadlock; and
822-
// `get` is never called while a `binding_lock` is held.
823-
e.lockBindings();
845+
// Shared/captured scopes read under `binding_lock` so a concurrent
846+
// `put`/`putAlias` rehash cannot tear the read or free the table.
847+
// Uncaptured private activations have one mutator and may read
848+
// lock-free; their writes still lock against the concurrent marker.
849+
const locked = e.lockBindingsForRead();
824850
if (e.aliases.count() != 0) {
825851
if (e.aliases.get(name)) |a| {
826-
e.unlockBindings();
852+
e.unlockBindingsForRead(locked);
827853
return a.env.get(a.name);
828854
}
829855
}
830856
if (e.vars.get(name)) |v| {
831-
e.unlockBindings();
857+
e.unlockBindingsForRead(locked);
832858
return v;
833859
}
834-
e.unlockBindings();
860+
e.unlockBindingsForRead(locked);
835861
env = e.parent;
836862
}
837863
return null;
@@ -842,8 +868,8 @@ pub const Environment = struct {
842868
/// an outer scope or module alias, so the full parent-chain/alias walk only
843869
/// adds lock/hash churn on hot no-GIL readers.
844870
pub fn getLocal(self: *Environment, name: []const u8) ?Value {
845-
self.lockBindings();
846-
defer self.unlockBindings();
871+
const locked = self.lockBindingsForRead();
872+
defer self.unlockBindingsForRead(locked);
847873
return self.vars.get(name);
848874
}
849875

@@ -883,6 +909,39 @@ pub const Environment = struct {
883909
}
884910
};
885911

912+
test "Environment private activation reads elide locks until capture" {
913+
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
914+
defer arena.deinit();
915+
916+
var parent = Environment{
917+
.arena = arena.allocator(),
918+
.gc_managed = true,
919+
.fn_scope = true,
920+
.private_activation = true,
921+
};
922+
try parent.put("outer", Value.num(11));
923+
var child = Environment{
924+
.arena = arena.allocator(),
925+
.parent = &parent,
926+
.gc_managed = true,
927+
.private_activation = true,
928+
};
929+
try child.put("local", Value.num(7));
930+
931+
jsthread.resetContentionStats();
932+
defer jsthread.disableContentionStats();
933+
try std.testing.expectEqual(@as(?Value, Value.num(7)), child.get("local"));
934+
try std.testing.expectEqual(@as(?Value, Value.num(11)), child.get("outer"));
935+
try std.testing.expectEqual(@as(u64, 0), jsthread.contentionStats().env_lock_acquires);
936+
937+
child.markCaptured();
938+
try std.testing.expect(!child.private_activation);
939+
try std.testing.expect(!parent.private_activation);
940+
try std.testing.expectEqual(@as(?Value, Value.num(7)), child.get("local"));
941+
try std.testing.expectEqual(@as(?Value, Value.num(11)), child.get("outer"));
942+
try std.testing.expectEqual(@as(u64, 3), jsthread.contentionStats().env_lock_acquires);
943+
}
944+
886945
/// The shared, mutable `this` binding of a derived constructor activation. A
887946
/// derived ctor starts uninitialized and is bound exactly once by `super()`;
888947
/// arrows defined lexically inside it share THIS cell (not the interpreter's
@@ -2048,9 +2107,9 @@ pub const Interpreter = struct {
20482107
/// name)` and reflection see them (the test262 async harness relies on this).
20492108
pub fn globalDefine(self: *Interpreter, name: []const u8, v: Value) EvalError!void {
20502109
const vs = self.env.varScope(); // `var`/function declarations hoist here
2051-
vs.lockBindings(); // `vs` may be the shared global scope; a peer's put can rehash
2110+
const locked = vs.lockBindingsForRead();
20522111
const existed_local = vs.vars.contains(name);
2053-
vs.unlockBindings();
2112+
vs.unlockBindingsForRead(locked);
20542113
try vs.put(name, v);
20552114
if (vs.parent == null) {
20562115
if (self.global_object) |g| {
@@ -2469,11 +2528,11 @@ pub const Interpreter = struct {
24692528
while (env) |e| {
24702529
// Read `vars` under the binding lock: a peer thread's put/getOrPut on
24712530
// this scope can rehash it concurrently (no-GIL Environment race,
2472-
// Linux tsan-threadfuzz: globalDefine put vs this contains). Flag-
2473-
// gated, so the default engine stays lock-free.
2474-
e.lockBindings();
2531+
// Linux tsan-threadfuzz: globalDefine put vs this contains). Only a
2532+
// proven-private activation may elide this read lock.
2533+
const locked = e.lockBindingsForRead();
24752534
const has = e.vars.contains(name);
2476-
e.unlockBindings();
2535+
e.unlockBindingsForRead(locked);
24772536
if (has)
24782537
return if (e.parent == null and objectHasOwn(g, name)) g else null;
24792538
env = e.parent;
@@ -2533,7 +2592,7 @@ pub const Interpreter = struct {
25332592
// false→true transitions.
25342593
var env: ?*Environment = self.env;
25352594
while (env) |e| {
2536-
e.lockBindings();
2595+
const locked = e.lockBindingsForRead();
25372596
// Skip the `aliases` hash (which would still hash `name`) when the map
25382597
// is empty — the common case for ordinary scopes (aliases exist only for
25392598
// `arguments`/`with`/eval-mapped bindings). Halves the per-identifier
@@ -2552,7 +2611,7 @@ pub const Interpreter = struct {
25522611
// are read under the SAME `binding_lock` as `vars` (no-GIL race).
25532612
const global_shadowable = found_var != null and e.parent == null and
25542613
!e.consts.contains(name) and !e.lexicals.contains(name);
2555-
e.unlockBindings();
2614+
e.unlockBindingsForRead(locked);
25562615
if (alias) |a| return a.env.get(a.name);
25572616
if (found_var) |v| {
25582617
if (global_shadowable) {
@@ -2600,9 +2659,9 @@ pub const Interpreter = struct {
26002659
// Existence check under the binding lock (same no-GIL race class as
26012660
// lookupIdent); the `getPtr` result is not escaped, only its
26022661
// presence, so nothing dangles after unlock.
2603-
e.lockBindings();
2662+
const locked = e.lockBindingsForRead();
26042663
const owns = e.vars.getPtr(name) != null or e.aliases.get(name) != null;
2605-
e.unlockBindings();
2664+
e.unlockBindingsForRead(locked);
26062665
if (owns) return e;
26072666
env = e.parent;
26082667
}
@@ -2612,9 +2671,9 @@ pub const Interpreter = struct {
26122671
fn catchParamBindingEnvOf(start: *Environment, name: []const u8) ?*Environment {
26132672
var env: ?*Environment = start;
26142673
while (env) |e| {
2615-
e.lockBindings();
2674+
const locked = e.lockBindingsForRead();
26162675
const owns = e.vars.contains(name) or e.aliases.get(name) != null;
2617-
e.unlockBindings();
2676+
e.unlockBindingsForRead(locked);
26182677
if (owns) return if (e.is_catch_param) e else null;
26192678
env = e.parent;
26202679
}
@@ -2624,9 +2683,9 @@ pub const Interpreter = struct {
26242683
pub fn assignWithObject(self: *Interpreter, name: []const u8) EvalError!?*value.Object {
26252684
var env: ?*Environment = self.env;
26262685
while (env) |e| {
2627-
e.lockBindings();
2686+
const locked = e.lockBindingsForRead();
26282687
const owned = e.aliases.get(name) != null or e.vars.get(name) != null;
2629-
e.unlockBindings();
2688+
e.unlockBindingsForRead(locked);
26302689
if (owned) return null; // a real binding owns it
26312690
if (e.with_object) |wo| {
26322691
if (try self.withHasBinding(wo, name)) return wo;
@@ -2780,6 +2839,7 @@ pub const Interpreter = struct {
27802839
.parent = parent,
27812840
.gc_managed = gc_mod.allocationsAreManaged(),
27822841
.fn_scope = fn_scope,
2842+
.private_activation = if (parent) |p| p.private_activation and !p.captured else false,
27832843
};
27842844
}
27852845

@@ -3324,11 +3384,11 @@ pub const Interpreter = struct {
33243384
if (bindingEnvOf(self.env, name)) |e| {
33253385
// Read the 4 binding tables under one lock hold (no-GIL
33263386
// race class); isTdz on the copied value needs no lock.
3327-
e.lockBindings();
3387+
const locked = e.lockBindingsForRead();
33283388
const eligible = e.parent != null and !e.consts.contains(name) and
33293389
!e.fn_names.contains(name) and e.aliases.get(name) == null;
33303390
const cur = e.vars.get(name) orelse Value.undef();
3331-
e.unlockBindings();
3391+
e.unlockBindingsForRead(locked);
33323392
if (eligible and !self.isTdz(cur)) {
33333393
const v = try self.eval(a.value);
33343394
if (!a.target_parenthesized) try self.maybeNameAnon(v, a.value, name);
@@ -3454,11 +3514,11 @@ pub const Interpreter = struct {
34543514
const rhs = try self.eval(oa.value);
34553515
const new = try self.applyBinary(oa.op, old, rhs);
34563516
if (benv) |e| {
3457-
e.lockBindings();
3517+
const locked = e.lockBindingsForRead();
34583518
const eligible = e.parent != null and !e.consts.contains(name) and
34593519
!e.fn_names.contains(name) and e.aliases.get(name) == null;
34603520
const cur = e.vars.get(name) orelse Value.undef();
3461-
e.unlockBindings();
3521+
e.unlockBindingsForRead(locked);
34623522
if (eligible and !self.isTdz(cur)) {
34633523
try e.assign(name, new); // write to the captured binding
34643524
break :blk new;
@@ -6881,6 +6941,10 @@ pub const Interpreter = struct {
68816941

68826942
const call_env = try gc_mod.allocEnv(self.arena);
68836943
self.initEnvironment(call_env, func.closure, true);
6944+
// A fresh call Environment is reachable only by this activation and the
6945+
// concurrent marker. `makeFunction`/`markCaptured` revokes the proof
6946+
// before any closure carrying it can escape.
6947+
call_env.private_activation = true;
68846948

68856949
const saved_env = self.env;
68866950
const saved_signal = self.signal;

0 commit comments

Comments
 (0)