@@ -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
400402const 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+
405417pub 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 = ®istry_lock,
51516+ .debug_registry_stats = ®istry_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 = ®istry_lock,
51575+ .debug_registry_stats = ®istry_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