zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit e427ba9cd59dc876d27e4a71104b5156460ca618 (tree)
parent 854774d468b0a439def7eef73c544ac8d64f1b62
Author: Andrew Kelley <andrewrk@noreply.codeberg.org>
Date:   Thu, 27 Nov 2025 20:48:54 +0100

std.sort.partitionPoint: faster implementation (#30005)

Migrated from https://github.com/ziglang/zig/pull/21419

Co-authored-by: Jonathan Hallstrom <lmj.hallstrom@gmail.com>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30005

Diffstat:
Mlib/std/sort.zig | 23++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/lib/std/sort.zig b/lib/std/sort.zig @@ -678,18 +678,23 @@ pub fn partitionPoint( context: anytype, comptime predicate: fn (@TypeOf(context), T) bool, ) usize { - var low: usize = 0; - var high: usize = items.len; + var it: usize = 0; + var len: usize = items.len; - while (low < high) { - const mid = low + (high - low) / 2; - if (predicate(context, items[mid])) { - low = mid + 1; - } else { - high = mid; + while (len > 1) { + const half: usize = len / 2; + len -= half; + if (predicate(context, items[it + half - 1])) { + @branchHint(.unpredictable); + it += half; } } - return low; + + if (it < items.len) { + it += @intFromBool(predicate(context, items[it])); + } + + return it; } test partitionPoint {