Lightpanda: the headless browser designed for AI and automation
switch from LRU to Clock
Muki Kiboigob8e2197b22f6
diff --git a/src/network/LruCache.zig b/src/network/ClockCache.zigsimilarity index 53%rename from src/network/LruCache.zigrename to src/network/ClockCache.zigindex e5e6dc65a..86a0abf7a 100644--- a/src/network/LruCache.zig+++ b/src/network/ClockCache.zig@@ -19,21 +19,19 @@const std = @import("std");const Allocator = std.mem.Allocator;-pub fn LruCache(comptime K: type) type {+pub fn ClockCache(comptime K: type) type {return struct {const Self = @This();- const Map = if (K == []const u8) std.StringHashMapUnmanaged(*Entry) else std.AutoArrayHashMapUnmanaged(K, *Entry);-- const Entry = struct {- key: K,- node: std.DoublyLinkedList.Node = .{},- };+ const Map = if (K == []const u8)+ std.array_hash_map.String(bool)+ else+ std.array_hash_map.Auto(K, bool);allocator: Allocator,capacity: usize,- list: std.DoublyLinkedList = .{},map: Map = .empty,+ hand: usize = 0,pub fn init(allocator: Allocator, capacity: usize) Self {std.debug.assert(capacity > 0);@@ -41,52 +39,53 @@ pub fn LruCache(comptime K: type) type {}pub fn deinit(self: *Self) void {- var it = self.map.valueIterator();- while (it.next()) |node| self.allocator.destroy(node.*);self.map.deinit(self.allocator);}pub fn insert(self: *Self, key: K) !?K {- const entry = try self.allocator.create(Entry);- entry.* = .{ .key = key };- try self.map.put(self.allocator, key, entry);- self.list.append(&entry.node);+ try self.map.put(self.allocator, key, true);if (self.map.count() <= self.capacity) return null;- return self.evictOldest();+ return self.evictOne();}pub fn touch(self: *Self, key: K) void {- const entry = self.map.get(key).?;- self.list.remove(&entry.node);- self.list.append(&entry.node);+ if (self.map.getPtr(key)) |referenced| referenced.* = true;}- fn evictOldest(self: *Self) ?K {- const node = self.list.popFirst() orelse return null;- const entry: *Entry = @fieldParentPtr("node", node);- const key = entry.key;- _ = self.map.remove(key);- self.allocator.destroy(entry);- return key;+ fn evictOne(self: *Self) ?K {+ const referenced = self.map.values();+ while (true) {+ if (self.hand >= referenced.len) self.hand = 0;++ if (referenced[self.hand]) {+ referenced[self.hand] = false;+ self.hand += 1;+ continue;+ }++ const key = self.map.keys()[self.hand];+ self.map.swapRemoveAt(self.hand);+ return key;+ }}};}const testing = @import("../testing.zig");-test "LruCache: no eviction under capacity" {- var q = LruCache([]const u8).init(testing.allocator, 3);+test "ClockCache: no eviction under capacity" {+ var q = ClockCache([]const u8).init(testing.allocator, 3);defer q.deinit();try testing.expectEqual(null, try q.insert("a"));try testing.expectEqual(null, try q.insert("b"));try testing.expectEqual(null, try q.insert("c"));- try testing.expectEqual(3, q.list.len());+ try testing.expectEqual(3, q.map.count());}-test "LruCache: evicts oldest key once over capacity" {- var q = LruCache([]const u8).init(testing.allocator, 2);+test "ClockCache: evicts once over capacity" {+ var q = ClockCache([]const u8).init(testing.allocator, 2);defer q.deinit();try testing.expectEqual(null, try q.insert("a"));@@ -95,30 +94,26 @@ test "LruCache: evicts oldest key once over capacity" {const evicted = try q.insert("c");try testing.expect(evicted != null);try testing.expectString("a", evicted.?);- try testing.expectEqual(2, q.list.len());-- const evicted2 = try q.insert("d");- try testing.expect(evicted2 != null);- try testing.expectString("b", evicted2.?);- try testing.expectEqual(2, q.list.len());+ try testing.expectEqual(2, q.map.count());}-test "LruCache: touch properly prevents eviction on oldest" {- var q = LruCache([]const u8).init(testing.allocator, 2);+test "ClockCache: touch protects a key from eviction" {+ var q = ClockCache([]const u8).init(testing.allocator, 2);defer q.deinit();try testing.expectEqual(null, try q.insert("a"));try testing.expectEqual(null, try q.insert("b"));- q.touch("a");- const evicted = try q.insert("c");- try testing.expect(evicted != null);- try testing.expectString("b", evicted.?);- try testing.expectEqual(2, q.list.len());- q.touch("a");+ const evicted1 = try q.insert("c");+ try testing.expect(evicted1 != null);+ try testing.expectString("a", evicted1.?);+ try testing.expect(q.map.contains("b"));+ try testing.expect(q.map.contains("c"));+ q.touch("b");const evicted2 = try q.insert("d");try testing.expect(evicted2 != null);try testing.expectString("c", evicted2.?);- try testing.expectEqual(2, q.list.len());+ try testing.expect(q.map.contains("b"));+ try testing.expectEqual(2, q.map.count());}diff --git a/src/network/Robots.zig b/src/network/Robots.zigindex b1fe63b40..f92cc9aba 100644--- a/src/network/Robots.zig+++ b/src/network/Robots.zig@@ -19,7 +19,7 @@const std = @import("std");const lp = @import("lightpanda");-const LruCache = @import("LruCache.zig").LruCache;+const ClockCache = @import("ClockCache.zig").ClockCache;const log = lp.log;@@ -99,7 +99,7 @@ pub const RobotStore = struct {allocator: std.mem.Allocator,map: RobotsMap,- evictions: LruCache([]const u8),+ evictions: ClockCache([]const u8),mutex: std.Io.Mutex = .init,pub fn init(allocator: std.mem.Allocator, capacity: u32) RobotStore {