std.sort.equalRange: improve performance by using center-left upperBound#21278
std.sort.equalRange: improve performance by using center-left upperBound#21278Olvilock wants to merge 0 commit intoziglang:masterfrom Olvilock:master
Conversation
|
Andrei's algorithm assumes that the result will most frequently be found in the lower indices. I watched the talk a long time ago, and, from what I remember, Andrei claimed that this is the most common case, but he never backed it up with evidence. In my project, I use binary search in two places and, in both cases, that assumption does not hold, and this change would therefore decrease performance. I think it would be better to leave binary search as is, and create a new function, called, for instance, |
|
@LucasSantos91 From how I understood the talk, the assumption for the portion optimizing general binary search is that the input value can lie outside of the range of contained elements (also referred to as an "open range search" iiuc, otherwise it would be referred to as a "closed range search"). Regardless, this PR only seems to port the Some sort of performance benchmarking code and results as motivation for whether to merge this would be nice. |
|
Sorry, my bad. I was thinking of binary search instead of equal range. |
|
I've written a little benchmark to assess which approach would be best in the case of
It seems that there's a bug somewhere in my version, and it is also slower than 21290, so this pull definitely needs holding off. Not closing yet because 21290 needs two comparisons to work during the common iterations, and therefore can be slower than lowerBound + adjusted upperBound for trivial types. |
|
It seems to me that under the current API, this implementation is bound to underperform in ReleaseSafe, because the |
Ports solution presented at CppCon by Andrei Alexandrescu.
The upper bound uses result of the lower bound, and also skews the first few choppings to the left, leading up to 1.5x speedup (as reported by Andrei)