Live data from Hacker News

You can beat the binary search

lemire.me

141–150 of 175 posts

Re: You can beat the binary search

#141
post #53
post #44

Earlier quoted context omitted.

This is a drop-in improvement for essentially any binary search over 16-bit integer members.

With "practically benefit" I meant a speedup that is noticable. Is there any software that is significantly bottlenecked by the speed of sorted search?

I think it's possible to come up with a situation where you want to do a sorted search per every pixel in the screen, for every frame.

Re: You can beat the binary search

#142
post #127

Earlier quoted context omitted.

Binary searching a sorted array is isomorphic to a sorted binary tree with implicit child pointers. It seems to me like there should be a sort order that stores the items as a fully-dense left-shifted binary tree from top-to-bottom (e.g. like the implicit heap in an in-place heap sort, but a binary search tree instead of a hea). Is there a name for this? Does it show any performance wins in practice?

There's Eytzinger order: https://algorithmica.org/en/eytzinger

Thanks for that name; that's the exact layout I was considering.

See also https://arxiv.org/abs/1509.05053

Re: You can beat the binary search

#143

Earlier quoted context omitted.

> I really can't stand the c-style naming conventions. Honestly I don't see much difference between upb_MiniTable_FindFieldByNumber and upb::MiniTable::FindFieldByNumber

Those are fairly indistinguishable. It's when they start removing letters from words to save... debug symbol bytes or something? That's when c-style naming annoys me.

In their defence "hi" sounds very much like "high" in my mind's ear and "lo" like "low" :)

Re: You can beat the binary search

#144
I did little experiments with search in small arrays (16-32 items) and binary search is one of the worst methods because it requires lot of branches. The fastest method for small arrays was linear branchless search (you walk over all elements without breaking out of the loop. For example, if you want to know whether the array contains a number, you logically OR the checks for all items). I didn't use SIMD though, but the branches are very expensive for small arrays and simply checking all elements without branching is faster.

Re: You can beat the binary search

#145
post #141
post #53

Earlier quoted context omitted.

With "practically benefit" I meant a speedup that is noticable. Is there any software that is significantly bottlenecked by the speed of sorted search?

I think it's possible to come up with a situation where you want to do a sorted search per every pixel in the screen, for every frame.

That sounds promising. I think ray tracing checks for ray intersection over an unsorted polygon soup. Sorted data seems hard to come by.

Re: You can beat the binary search

#146
post #58

Will I get a job if i say i can beat binary search?

I dunno but I once didn't get a job because I argued with the interviewer about my (Perl) implementation of binary search[0] - he said it was buggy, I proved it wasn't, he insisted it was, I proved it wasn't some more, I was correct, he was miffed. No job for me.

[0] A nonsense thing to ask people to implement in an interview

Re: You can beat the binary search

#147

Earlier quoted context omitted.

> I really can't stand the c-style naming conventions. Honestly I don't see much difference between upb_MiniTable_FindFieldByNumber and upb::MiniTable::FindFieldByNumber

Those are fairly indistinguishable. It's when they start removing letters from words to save... debug symbol bytes or something? That's when c-style naming annoys me.

This is also my pet peeve with a lot of code as well as commands like

    npm -g i package-name 
Like why would you teach people to do this? I understand people needed to save precious bytes in the sixties so we have cat and ls but saving 192 bytes or whatever with shorter variable names is not a worthwhile tradeoff anymore.

Re: You can beat the binary search

#148
post #71

Earlier quoted context omitted.

It's not about doing more or less work; it's about doing the work faster . For instance, it's relatively common to discover that some recomputation can be faster than caching or lookup tables. Similarly, fetching more from memory also can be faster if it means you make less roundtrips.

Well that's where I thought this link was going to go before it went down the simd path... We have a way to beat binary search, it is called b-trees, it has the same basic insight that you can easily take 64 elements from your data set evenly spaced, compare against all of those rapidly, and instead of bifurcating your search space once, you do the same as six times, but because you store the 64 elements in an array…

A B-tree is not a search algorithm though, it is a data structure. While it would nice to be able to somehow instantly materialize a B-tree from a linear array, CPUs aren't quite there yet. It would also be nice not to have to deal with linear arrays where B-trees would be better fit in the first place, but we are not quite there yet either.

Re: You can beat the binary search

#149

I did little experiments with search in small arrays (16-32 items) and binary search is one of the worst methods because it requires lot of branches. The fastest method for small arrays was linear branchless search (you walk over all elements without breaking out of the loop. For example, if you want to know whether the array contains a number, you logically OR the checks for all items). I didn't use SIMD though, but…

I wonder if this is faster because it makes the prefetcher happy.

Re: You can beat the binary search

#150
post #42
post #3

If you are storing 16-bit integers, wouldn't an 8kB bitmap be even faster?

The library the author is talking about selects between bitmap and array dynamically depending on density. https://roaringbitmap.org/

That explains the maximum size of 4096 elements (exactly where a bitmap would be smaller).
Post reply on HN