>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.
Read https://en.algorithmica.org/hpc/pipelining/branching/ and the following chapter. TL;DR: you can generate different, and more efficient code if you know how much it is going to be mispredicted.
Fastest branchless binary search
61–70 of 155 posts
Re: Fastest branchless binary search
#62Every post like this makes me update my junior engineer watchlist. Then I have to go patiently explain why mucking about in the code to save one instruction because you read a blog post is a horrible idea. Easily half of all silly junior code is pointless optimization. Having said that, I did enjoy the post.
What kind of junior engineers are you hiring that prematurely rewrite a hot loop to use branchless assembly logic? The junior employees I have had to deal with tended to build towering abstractions that no one really needed, rather than optimising hot loops.
Re: Fastest branchless binary search
#63Earlier quoted context omitted.
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.
I don't know much about Rust data types or Rust in general but does it not have any integer overflow? I see (left+right)/2. Is it like python with unbounded precision?
There is no (left+right)/2 on that page.
let mid = left + size / 2;
It's only size that is being divided by 2, which is the size of the segment still under consideration (which is initially the whole array). left is the starting index of segment still under consideration (which is initially 0).Re: Fastest branchless binary search
#64Re: Fastest branchless binary search
#65Is that still lower_bound? Maybe I am misreading the code but it looks like this returns any match, not the earliest match (when there are dupes). It’s common to have multiple matches even in a unique list if the comparison function is say looking for a certain string prefix to do autocomplete, but we want the earliest in the list.
I guess it’s good to have the option of not caring if you want even more speed
The most common argument against optimization is "just use the existing code", but the "existing code" _always_ attempts to handle special edge cases that probably don't apply in your case. We programmers waste a lot of end-user time saving a bit of programmer time.
Re: Fastest branchless binary search
#66Earlier quoted context omitted.
I guess it’s good to have the option of not caring if you want even more speed
Or, less contentiously - if you know you don't care/don't need to disambiguate dups, look how much efficiency you're losing on a case that isn't important. The most common argument against optimization is "just use the existing code", but the "existing code" _always_ attempts to handle special edge cases that probably don't apply in your case. We programmers waste a lot of end-user time saving a bit of programmer tim…
Re: Fastest branchless binary search
#67I was hoping for a binary search without branches in it. Not really what we got.
Re: Fastest branchless binary search
#68lower_bound and upper_bound are typically implemented in terms of partition_point, which is much more general than this version of lower_bound taking an element.
Re: Fastest branchless binary search
#69Earlier quoted context omitted.
Or, less contentiously - if you know you don't care/don't need to disambiguate dups, look how much efficiency you're losing on a case that isn't important. The most common argument against optimization is "just use the existing code", but the "existing code" _always_ attempts to handle special edge cases that probably don't apply in your case. We programmers waste a lot of end-user time saving a bit of programmer tim…
On the other hand, assuming it's OK to ignore the special edge cases (or not even thinking of them) can come back to bite you (or your users) when eventually one of them does show up in a situation you didn't anticipate.
Re: Fastest branchless binary search
#70Earlier quoted context omitted.
Alright, and as a versioned link that'll be: https://github.com/rust-lang/rust/blob/7d8386f05cedf33c7475c... Thank you!
You can get versioned link directly into rustdoc: https://doc.rust-lang.org/1.71.1/src/core/slice/mod.rs.html#...