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

Commit 59b34db

Browse files
committed
perf(debugger): cache absent statement locations (#541)
Cache immutable negative source-location lookups alongside positive entries and replace the direct-mapped table with a fixed 16-set, four-way cache. The cache remains allocation-free and bounded at 64 entries, preserves exact positive locations, and deterministically evicts adversarial collisions. Keep debugger counters inside the already opt-in runtime attribution profiler rather than every Context. This restores the ordinary bounded-heap footprint exposed by the heap_limit_bytes recovery gate. At 1/2/4/8 lanes, base invocation registry acquisitions fall from 55,110/110,179/220,317/440,593 to 156/250/438/814 while checksums and object/shape counts remain exact. The 8-lane contended control falls from 440,593 to 779 acquisitions. Validation: 1,601 passed, 1 skipped, 0 failed, 0 leaked in test-parallel; focused positive, negative, and collision cache tests; bounded-heap recovery regression; dynamic-script publication normally and under TSan; opt-in attribution; 33/33 conformance smoke. Test262 flips not measured because the change preserves JavaScript evaluation semantics and only caches immutable debugger metadata.
1 parent 76591ca commit 59b34db

2 files changed

Lines changed: 129 additions & 20 deletions

File tree

‎src/context.zig‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ pub const RuntimeAttributionProfiler = struct {
504504
};
505505

506506
inner: std.mem.Allocator,
507+
debug_registry: interp.DebugRegistryStats = .{},
507508
backing_allocations: std.atomic.Value(u64) = .init(0),
508509
backing_allocation_bytes: std.atomic.Value(u64) = .init(0),
509510
backing_growths: std.atomic.Value(u64) = .init(0),
@@ -3190,9 +3191,6 @@ pub const Context = struct {
31903191
/// disabled and do not add atomics to scored execution paths.
31913192
profile_execution_tiers: bool = false,
31923193
execution_tier_inventory: interp.ExecutionTierInventory = .{},
3193-
/// Realm-owned statement-location attribution. The interpreter receives
3194-
/// this sink only for explicit profiling contexts.
3195-
debug_registry_stats: interp.DebugRegistryStats = .{},
31963194
debug_exception_hook: ?interp.DebugExceptionHook = null,
31973195
debug_statement_locations: std.AutoHashMapUnmanaged(*const ast.Node, interp.DebugStatementLocation) = .empty,
31983196
/// Guards the script-id/list and statement-location registry only in a
@@ -4750,7 +4748,7 @@ pub const Context = struct {
47504748
.profile_statement_hook = self.profile_statement_hook,
47514749
.bytecode_admission_inventory = &self.bytecode_admission_inventory,
47524750
.execution_tier_inventory = if (self.profile_execution_tiers) &self.execution_tier_inventory else null,
4753-
.debug_registry_stats = if (self.profile_execution_tiers) &self.debug_registry_stats else null,
4751+
.debug_registry_stats = if (self.runtime_attribution_profiler) |profile| &profile.debug_registry else null,
47544752
.bytecode_execution_mode = self.bytecode_execution_mode,
47554753
.debug_statement_locations = &self.debug_statement_locations,
47564754
.debug_registry_lock = if (self.parallel_js) &self.debug_registry_lock else null,
@@ -4836,7 +4834,7 @@ pub const Context = struct {
48364834
}
48374835

48384836
pub fn lockDebugRegistry(self: *Context) void {
4839-
if (self.parallel_js) self.debug_registry_lock.lockProfiled(if (self.profile_execution_tiers) &self.debug_registry_stats else null);
4837+
if (self.parallel_js) self.debug_registry_lock.lockProfiled(if (self.runtime_attribution_profiler) |profile| &profile.debug_registry else null);
48404838
}
48414839

48424840
pub fn unlockDebugRegistry(self: *Context) void {
@@ -4921,7 +4919,10 @@ pub const Context = struct {
49214919
.unknown_shape_invalidation_events = code.unknown_shape_invalidation_events,
49224920
.shape_fallback_events = code.shape_fallback_events,
49234921
},
4924-
.debug_registry = self.debug_registry_stats.snapshot(),
4922+
.debug_registry = if (self.runtime_attribution_profiler) |profile|
4923+
profile.debug_registry.snapshot()
4924+
else
4925+
.{},
49254926
.heap = self.runtimeHeapAccounting(),
49264927
.runtime = if (self.runtime_attribution_profiler) |profile|
49274928
profile.snapshot()

‎src/interpreter.zig‎

Lines changed: 122 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,25 @@ pub const DebugScriptRegistration = struct {
395395
start_line: usize,
396396
};
397397

398-
const debug_location_cache_size = 64;
398+
const debug_location_cache_set_count = 16;
399+
const debug_location_cache_way_count = 4;
400+
const debug_location_cache_size = debug_location_cache_set_count * debug_location_cache_way_count;
399401

400402
const DebugLocationCacheEntry = struct {
401403
node: ?*const Node = null,
402-
location: DebugStatementLocation = undefined,
404+
location: ?DebugStatementLocation = null,
403405
};
404406

407+
fn debugLocationCacheSetIndex(node: *const Node) usize {
408+
// Arena allocation gives adjacent nodes regular low address bits. Fold
409+
// higher bits before selecting a power-of-two set so one source construct
410+
// cannot evict every other hot statement merely because of its stride.
411+
var key = @intFromPtr(node) / @alignOf(Node);
412+
key ^= key >> 7;
413+
key ^= key >> 13;
414+
return key & (debug_location_cache_set_count - 1);
415+
}
416+
405417
pub const DebugRegistrySnapshot = struct {
406418
location_cache_hits: u64 = 0,
407419
location_cache_misses: u64 = 0,
@@ -1609,10 +1621,14 @@ pub const Interpreter = struct {
16091621
/// profiling is explicitly enabled.
16101622
debug_registry_stats: ?*DebugRegistryStats = null,
16111623
/// Registered locations never change for an AST node. Copy them into this
1612-
/// interpreter-local direct-mapped cache after one synchronized registry
1613-
/// lookup so hot loops in a shared realm do not contend on the registry at
1614-
/// every ordinary statement boundary.
1615-
debug_location_cache: [debug_location_cache_size]DebugLocationCacheEntry = @splat(.{}),
1624+
/// interpreter-local four-way cache after one synchronized registry lookup
1625+
/// so hot loops in a shared realm do not contend on the registry at every
1626+
/// ordinary statement boundary. Both positive and negative lookups are
1627+
/// immutable once an AST is published, so non-statement nodes are cached as
1628+
/// well. Total capacity remains fixed at 64 entries;
1629+
/// adversarial source can cause bounded misses but cannot grow storage.
1630+
debug_location_cache: [debug_location_cache_set_count][debug_location_cache_way_count]DebugLocationCacheEntry = @splat(@splat(.{})),
1631+
debug_location_cache_next: [debug_location_cache_set_count]u8 = @splat(0),
16161632
debug_location_cache_hits: usize = 0,
16171633
debug_location_cache_misses: usize = 0,
16181634
debug_script_ctx: ?*anyopaque = null,
@@ -4194,22 +4210,28 @@ pub const Interpreter = struct {
41944210
}
41954211

41964212
pub fn debugStatementLocation(self: *Interpreter, node: *const Node) ?DebugStatementLocation {
4197-
const cache_index = (@intFromPtr(node) / @alignOf(Node)) % debug_location_cache_size;
4198-
const cached = &self.debug_location_cache[cache_index];
4199-
if (cached.node == node) {
4200-
if (builtin.is_test) self.debug_location_cache_hits += 1;
4201-
if (self.debug_registry_stats) |stats| _ = stats.location_cache_hits.fetchAdd(1, .monotonic);
4202-
return cached.location;
4213+
const set_index = debugLocationCacheSetIndex(node);
4214+
const set = &self.debug_location_cache[set_index];
4215+
var first_empty: ?usize = null;
4216+
for (set, 0..) |*cached, way| {
4217+
if (cached.node == node) {
4218+
if (builtin.is_test) self.debug_location_cache_hits += 1;
4219+
if (self.debug_registry_stats) |stats| _ = stats.location_cache_hits.fetchAdd(1, .monotonic);
4220+
return cached.location;
4221+
}
4222+
if (cached.node == null and first_empty == null) first_empty = way;
42034223
}
42044224

42054225
const locations = self.debug_statement_locations orelse return null;
42064226
if (self.debug_registry_stats) |stats| _ = stats.location_cache_misses.fetchAdd(1, .monotonic);
42074227
const location = blk: {
42084228
self.lockDebugRegistry();
42094229
defer self.unlockDebugRegistry();
4210-
break :blk locations.get(node) orelse return null;
4230+
break :blk locations.get(node);
42114231
};
4212-
cached.* = .{ .node = node, .location = location };
4232+
const replacement = first_empty orelse self.debug_location_cache_next[set_index];
4233+
set[replacement] = .{ .node = node, .location = location };
4234+
self.debug_location_cache_next[set_index] = @intCast((replacement + 1) % debug_location_cache_way_count);
42134235
if (builtin.is_test) self.debug_location_cache_misses += 1;
42144236
return location;
42154237
}
@@ -51478,3 +51500,89 @@ test "interpreter caches immutable debug locations after one registry lookup" {
5147851500
try std.testing.expectEqual(@as(u64, 0), stats.lock_contentions);
5147951501
try std.testing.expectEqual(@as(u64, 0), stats.lock_spins);
5148051502
}
51503+
51504+
test "interpreter caches immutable absence of a debug location" {
51505+
var locations: std.AutoHashMapUnmanaged(*const Node, DebugStatementLocation) = .empty;
51506+
defer locations.deinit(std.testing.allocator);
51507+
var node: Node = undefined;
51508+
var registry_lock = DebugRegistryLock{};
51509+
var registry_stats = DebugRegistryStats{};
51510+
var machine = Interpreter{
51511+
.arena = std.testing.allocator,
51512+
.env = undefined,
51513+
.root_shape = undefined,
51514+
.debug_statement_locations = &locations,
51515+
.debug_registry_lock = &registry_lock,
51516+
.debug_registry_stats = &registry_stats,
51517+
};
51518+
try std.testing.expect(machine.debugStatementLocation(&node) == null);
51519+
try std.testing.expect(machine.debugStatementLocation(&node) == null);
51520+
51521+
const stats = registry_stats.snapshot();
51522+
try std.testing.expectEqual(@as(u64, 1), stats.location_cache_misses);
51523+
try std.testing.expectEqual(@as(u64, 1), stats.location_cache_hits);
51524+
try std.testing.expectEqual(@as(u64, 1), stats.lock_acquires);
51525+
}
51526+
51527+
test "interpreter debug location cache retains four colliding statements with bounded eviction" {
51528+
var locations: std.AutoHashMapUnmanaged(*const Node, DebugStatementLocation) = .empty;
51529+
defer locations.deinit(std.testing.allocator);
51530+
var nodes: [debug_location_cache_size + debug_location_cache_set_count]Node = undefined;
51531+
var selected: [debug_location_cache_way_count + 1]*Node = undefined;
51532+
var selected_len: usize = 0;
51533+
var selected_set: ?usize = null;
51534+
for (&nodes) |*node| {
51535+
const set_index = debugLocationCacheSetIndex(node);
51536+
if (selected_set == null) selected_set = set_index;
51537+
if (set_index != selected_set.?) continue;
51538+
selected[selected_len] = node;
51539+
selected_len += 1;
51540+
if (selected_len == selected.len) break;
51541+
}
51542+
if (selected_len != selected.len) {
51543+
// Select the most populated set rather than relying on the first node's
51544+
// distribution. Pigeonhole guarantees five peers across 80 nodes.
51545+
var counts: [debug_location_cache_set_count]usize = @splat(0);
51546+
for (&nodes) |*node| counts[debugLocationCacheSetIndex(node)] += 1;
51547+
var fullest_set: usize = 0;
51548+
for (counts[1..], 1..) |count, set_index|
51549+
if (count > counts[fullest_set]) {
51550+
fullest_set = set_index;
51551+
};
51552+
selected_set = fullest_set;
51553+
selected_len = 0;
51554+
for (&nodes) |*node| {
51555+
if (debugLocationCacheSetIndex(node) != selected_set.?) continue;
51556+
selected[selected_len] = node;
51557+
selected_len += 1;
51558+
if (selected_len == selected.len) break;
51559+
}
51560+
}
51561+
try std.testing.expectEqual(selected.len, selected_len);
51562+
for (selected, 0..) |node, index| try locations.put(std.testing.allocator, node, .{
51563+
.script_id = @intCast(index + 1),
51564+
.location = .{ .byte_offset = index, .line = index + 1, .column = 1 },
51565+
});
51566+
51567+
var registry_lock = DebugRegistryLock{};
51568+
var registry_stats = DebugRegistryStats{};
51569+
var machine = Interpreter{
51570+
.arena = std.testing.allocator,
51571+
.env = undefined,
51572+
.root_shape = undefined,
51573+
.debug_statement_locations = &locations,
51574+
.debug_registry_lock = &registry_lock,
51575+
.debug_registry_stats = &registry_stats,
51576+
};
51577+
for (selected[0..debug_location_cache_way_count], 0..) |node, index|
51578+
try std.testing.expectEqual(@as(u64, @intCast(index + 1)), machine.debugStatementLocation(node).?.script_id);
51579+
for (selected[0..debug_location_cache_way_count], 0..) |node, index|
51580+
try std.testing.expectEqual(@as(u64, @intCast(index + 1)), machine.debugStatementLocation(node).?.script_id);
51581+
try std.testing.expectEqual(@as(u64, debug_location_cache_way_count + 1), machine.debugStatementLocation(selected[debug_location_cache_way_count]).?.script_id);
51582+
try std.testing.expectEqual(@as(u64, 1), machine.debugStatementLocation(selected[0]).?.script_id);
51583+
51584+
const stats = registry_stats.snapshot();
51585+
try std.testing.expectEqual(@as(u64, 4), stats.location_cache_hits);
51586+
try std.testing.expectEqual(@as(u64, 6), stats.location_cache_misses);
51587+
try std.testing.expectEqual(@as(u64, 6), stats.lock_acquires);
51588+
}

0 commit comments

Comments
 (0)