authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-16 07:27:20-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-12-16 07:27:20-05:00
logd12c0bf90911339c8db0741b257eac251b827b2c
treea35c9b98cc1e601c09e96983548e2a62c8ad0625
parent3a0a9aa9b866ad8483d298c6b891f9b321303bb9
parent0fe17ea12a5bc389d8cb0c87027a112a552e9cbc
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #22242 from Rexicon226/moar-branch-hint

utilize `@branchHint` more

2 files changed, 9 insertions(+), 13 deletions(-)

lib/std/hash/xxhash.zig+5-5
...@@ -593,7 +593,7 @@ pub const XxHash3 = struct {...@@ -593,7 +593,7 @@ pub const XxHash3 = struct {
593 }593 }
594594
595 fn hash3(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {595 fn hash3(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
596 @branchHint(.cold);596 @branchHint(.unlikely);
597 std.debug.assert(input.len > 0 and input.len < 4);597 std.debug.assert(input.len > 0 and input.len < 4);
598598
599 const flip: [2]u32 = @bitCast(secret[0..8].*);599 const flip: [2]u32 = @bitCast(secret[0..8].*);
...@@ -625,7 +625,7 @@ pub const XxHash3 = struct {...@@ -625,7 +625,7 @@ pub const XxHash3 = struct {
625 }625 }
626626
627 fn hash16(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {627 fn hash16(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
628 @branchHint(.cold);628 @branchHint(.unlikely);
629 std.debug.assert(input.len > 8 and input.len <= 16);629 std.debug.assert(input.len > 8 and input.len <= 16);
630630
631 const flip: [4]u64 = @bitCast(secret[24..56].*);631 const flip: [4]u64 = @bitCast(secret[24..56].*);
...@@ -641,7 +641,7 @@ pub const XxHash3 = struct {...@@ -641,7 +641,7 @@ pub const XxHash3 = struct {
641 }641 }
642642
643 fn hash128(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {643 fn hash128(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
644 @branchHint(.cold);644 @branchHint(.unlikely);
645 std.debug.assert(input.len > 16 and input.len <= 128);645 std.debug.assert(input.len > 16 and input.len <= 128);
646646
647 var acc = XxHash64.prime_1 *% @as(u64, input.len);647 var acc = XxHash64.prime_1 *% @as(u64, input.len);
...@@ -657,7 +657,7 @@ pub const XxHash3 = struct {...@@ -657,7 +657,7 @@ pub const XxHash3 = struct {
657 }657 }
658658
659 fn hash240(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {659 fn hash240(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
660 @branchHint(.cold);660 @branchHint(.unlikely);
661 std.debug.assert(input.len > 128 and input.len <= 240);661 std.debug.assert(input.len > 128 and input.len <= 240);
662662
663 var acc = XxHash64.prime_1 *% @as(u64, input.len);663 var acc = XxHash64.prime_1 *% @as(u64, input.len);
...@@ -676,7 +676,7 @@ pub const XxHash3 = struct {...@@ -676,7 +676,7 @@ pub const XxHash3 = struct {
676 }676 }
677677
678 noinline fn hashLong(seed: u64, input: []const u8) u64 {678 noinline fn hashLong(seed: u64, input: []const u8) u64 {
679 @branchHint(.cold);679 @branchHint(.unlikely);
680 std.debug.assert(input.len >= 240);680 std.debug.assert(input.len >= 240);
681681
682 const block_count = ((input.len - 1) / @sizeOf(Block)) * @sizeOf(Block);682 const block_count = ((input.len - 1) / @sizeOf(Block)) * @sizeOf(Block);
lib/std/hash_map.zig+4-8
...@@ -1187,17 +1187,13 @@ pub fn HashMapUnmanaged(...@@ -1187,17 +1187,13 @@ pub fn HashMapUnmanaged(
1187 }1187 }
11881188
1189 /// Find the index containing the data for the given key.1189 /// Find the index containing the data for the given key.
1190 /// Whether this function returns null is almost always1190 fn getIndex(self: Self, key: anytype, ctx: anytype) ?usize {
1191 /// branched on after this function returns, and this function
1192 /// returns null/not null from separate code paths. We
1193 /// want the optimizer to remove that branch and instead directly
1194 /// fuse the basic blocks after the branch to the basic blocks
1195 /// from this function. To encourage that, this function is
1196 /// marked as inline.
1197 inline fn getIndex(self: Self, key: anytype, ctx: anytype) ?usize {
1198 comptime verifyContext(@TypeOf(ctx), @TypeOf(key), K, Hash, false);1191 comptime verifyContext(@TypeOf(ctx), @TypeOf(key), K, Hash, false);
11991192
1200 if (self.size == 0) {1193 if (self.size == 0) {
1194 // We use cold instead of unlikely to force a jump to this case,
1195 // no matter the weight of the opposing side.
1196 @branchHint(.cold);
1201 return null;1197 return null;
1202 }1198 }
12031199