Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utilize @branchHint more #22242

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/std/hash/xxhash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ pub const XxHash3 = struct {
}

fn hash3(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
@branchHint(.cold);
@branchHint(.unlikely);
std.debug.assert(input.len > 0 and input.len < 4);

const flip: [2]u32 = @bitCast(secret[0..8].*);
Expand Down Expand Up @@ -625,7 +625,7 @@ pub const XxHash3 = struct {
}

fn hash16(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
@branchHint(.cold);
@branchHint(.unlikely);
std.debug.assert(input.len > 8 and input.len <= 16);

const flip: [4]u64 = @bitCast(secret[24..56].*);
Expand All @@ -641,7 +641,7 @@ pub const XxHash3 = struct {
}

fn hash128(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
@branchHint(.cold);
@branchHint(.unlikely);
std.debug.assert(input.len > 16 and input.len <= 128);

var acc = XxHash64.prime_1 *% @as(u64, input.len);
Expand All @@ -657,7 +657,7 @@ pub const XxHash3 = struct {
}

fn hash240(seed: u64, input: anytype, noalias secret: *const [192]u8) u64 {
@branchHint(.cold);
@branchHint(.unlikely);
std.debug.assert(input.len > 128 and input.len <= 240);

var acc = XxHash64.prime_1 *% @as(u64, input.len);
Expand All @@ -676,7 +676,7 @@ pub const XxHash3 = struct {
}

noinline fn hashLong(seed: u64, input: []const u8) u64 {
@branchHint(.cold);
@branchHint(.unlikely);
std.debug.assert(input.len >= 240);

const block_count = ((input.len - 1) / @sizeOf(Block)) * @sizeOf(Block);
Expand Down
12 changes: 4 additions & 8 deletions lib/std/hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1187,17 +1187,13 @@ pub fn HashMapUnmanaged(
}

/// Find the index containing the data for the given key.
/// Whether this function returns null is almost always
/// branched on after this function returns, and this function
/// returns null/not null from separate code paths. We
/// want the optimizer to remove that branch and instead directly
/// fuse the basic blocks after the branch to the basic blocks
/// from this function. To encourage that, this function is
/// marked as inline.
inline fn getIndex(self: Self, key: anytype, ctx: anytype) ?usize {
fn getIndex(self: Self, key: anytype, ctx: anytype) ?usize {
comptime verifyContext(@TypeOf(ctx), @TypeOf(key), K, Hash, false);

if (self.size == 0) {
// We use cold instead of unlikely to force a jump to this case,
// no matter the weight of the opposing side.
@branchHint(.cold);
return null;
}

Expand Down
Loading