Merge pull request #1486 from lightpanda-io/nikneym/hash-map-change
Some checks failed
e2e-test / zig build release (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / zig test using v8 in debug mode (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled

Change hash generation of global event handlers
This commit is contained in:
Halil Durak
2026-02-11 16:55:07 +03:00
committed by GitHub
2 changed files with 48 additions and 23 deletions

View File

@@ -1254,8 +1254,10 @@ pub fn setAttrListener(
});
}
const key = global_event_handlers.calculateKey(element.asEventTarget(), listener_type);
const gop = try self._element_attr_listeners.getOrPut(self.arena, key);
const gop = try self._element_attr_listeners.getOrPut(self.arena, .{
.target = element.asEventTarget(),
.handler = listener_type,
});
gop.value_ptr.* = listener_callback;
}
@@ -1265,8 +1267,10 @@ pub fn getAttrListener(
element: *Element,
listener_type: GlobalEventHandler,
) ?JS.Function.Global {
const key = global_event_handlers.calculateKey(element.asEventTarget(), listener_type);
return self._element_attr_listeners.get(key);
return self._element_attr_listeners.get(.{
.target = element.asEventTarget(),
.handler = listener_type,
});
}
pub fn registerPerformanceObserver(self: *Page, observer: *PerformanceObserver) !void {

View File

@@ -23,13 +23,47 @@ const js = @import("../js/js.zig");
const EventTarget = @import("EventTarget.zig");
/// Better to discriminate it since not directly a pointer int.
///
/// See `calculateKey` to obtain one.
const Key = u64;
const IS_DEBUG = @import("builtin").mode == .Debug;
/// Use `calculateKey` to create a key.
pub const Lookup = std.AutoHashMapUnmanaged(Key, js.Function.Global);
const Key = struct {
target: *EventTarget,
handler: Handler,
/// Fuses `target` pointer and `handler` enum; used at hashing.
/// NEVER use a fusion to retrieve a pointer back. Portability is not guaranteed.
/// See `Context.hash`.
fn fuse(self: *const Key) u64 {
// Check if we have 3 bits available from alignment of 8.
if (comptime IS_DEBUG) {
lp.assert(@alignOf(EventTarget) == 8, "Key.fuse: incorrect alignment", .{
.event_target_alignment = @alignOf(EventTarget),
});
}
const ptr = @intFromPtr(self.target) >> 3;
if (comptime IS_DEBUG) {
lp.assert(ptr < (1 << 57), "Key.fuse: pointer overflow", .{ .ptr = ptr });
}
return ptr | (@as(u64, @intFromEnum(self.handler)) << 57);
}
};
const Context = struct {
pub fn hash(_: @This(), key: Key) u64 {
return std.hash.int(key.fuse());
}
pub fn eql(_: @This(), a: Key, b: Key) bool {
return a.fuse() == b.fuse();
}
};
pub const Lookup = std.HashMapUnmanaged(
Key,
js.Function.Global,
Context,
std.hash_map.default_max_load_percentage,
);
/// Enum of known event listeners; increasing the size of it (u7)
/// can cause `Key` to behave incorrectly.
@@ -130,16 +164,3 @@ pub const Handler = enum(u7) {
onwaiting,
onwheel,
};
/// Calculates a lookup key to use with lookup for event target.
/// NEVER use generated key to retrieve a pointer back. Portability is not guaranteed.
pub fn calculateKey(event_target: *EventTarget, handler_type: Handler) Key {
// Check if we have 3 bits available from alignment of 8.
lp.assert(@alignOf(EventTarget) == 8, "calculateKey: incorrect alignment", .{
.event_target_alignment = @alignOf(EventTarget),
});
const ptr = @intFromPtr(event_target) >> 3;
lp.assert(ptr < (1 << 57), "calculateKey: pointer overflow", .{ .ptr = ptr });
return ptr | (@as(Key, @intFromEnum(handler_type)) << 57);
}