Live data from Hacker News

Fastest branchless binary search

mhdm.dev

81–90 of 155 posts

Re: Fastest branchless binary search

#81
If the compiler doesn't optimize the if(cmp(...)) first += length + rem; perhaps a sign operation could be used when cmp returns a number. Something along the lines of:

    first += (length + rem) * ((sign(cmp(...)) + 1) / 2)
Has anyone tried that? DDG isn't very helpful.

Re: Fastest branchless binary search

#83

How can you call it branchless if it has "while (length > 0) {"

That particular branch is well-predicted for long arrays. You probably only gonna have a single misprediction for the complete binary search function, the performance consequence of that branch is negligible.

People (and compilers, too) don’t write branchless code just for the sake of it, they do because it helps with performance.

Re: Fastest branchless binary search

#85
post #26
post #6

Earlier quoted context omitted.

> BUT ZIG.. There’s no binary search implementation in Zig that I could find, rather it calls to C++. Zig’s binary search is here, it’s an unoptimized textbook version: https://github.com/ziglang/zig/blob/b835fd90cef1447904d3b009... At TigerBeetle, we have our own branchless implementation here: https://github.com/tigerbeetle/tigerbeetle/blob/e996abcf7154...

Rust's binary search is here. Also a pretty textbook version of the function: https://github.com/rust-lang/rust/blob/4d7a80d48697171ed151c... How much faster is the branchless version in practice? I make heavy use of binary search in some code I maintain, and I wonder if it would make much of a difference to switch to a more efficient version of the function.

Rust's binary search used to be branchless but due to some issues down in LLVM land, the codegen doesn't issue a `cmov` where it should. https://github.com/rust-lang/rust/issues/53823

old branchless version: https://github.com/rust-lang/rust/pull/45333/files

Re: Fastest branchless binary search

#87

How can you call it branchless if it has "while (length > 0) {"

That particular branch is well-predicted for long arrays. You probably only gonna have a single misprediction for the complete binary search function, the performance consequence of that branch is negligible. People (and compilers, too) don’t write branchless code just for the sake of it, they do because it helps with performance.

sometimes branchless code also stops timing side channel attacks, but it's not enough by itself

Re: Fastest branchless binary search

#88
post #2

>gcc has __builtin_expect_with_probability(cond, 0, 0.5) but it does nothing (tested v10). ↩ I wonder what could possibly be the use of this builtin. Branch prediction varies enough between different processors that it seems unlikely that anything useful could be done with a fine-grained probability estimate.

At least when I last read it a couple years ago, Intel's optimization manual recommends certain code layouts. For example in the absence of better information a conditional jump backwards would be predicted as true (because that's what loops look like) while a jump forward is seen as less likely to happen (error conditions and else branches look like that). Not sure how consistent this is across architectures, and ho…

pretty common but not, as i thought, universal since stretch https://en.wikipedia.org/wiki/Branch_predictor#Static_branch...

but the only simpler approach is 'predict not taken' which in a sense is what a cortex-m0 does

Re: Fastest branchless binary search

#89
post #56
post #22

I wish all blog posts started the way his does: "You’re a busy person so I’ll first jump right to it. Here it is, the fastest general (and simple) binary search C++ implementation:"

Except I am too busy to read "You’re a busy person"

But not too busy to read the comments!
Post reply on HN