HackerPapers

arXiv:1509.05053cs.DS

Array Layouts for Comparison-Based Searching

Paul-Virak Khuong, Pat Morin

Abstract

We attempt to determine the best order and search algorithm to store $n$ comparable data items in an array, $A$, of length $n$ so that we can, for any query value, $x$, quickly find the smallest value in $A$ that is greater than or equal to $x$. In particular, we consider the important case where there are many such queries to the same array, $A$, which resides entirely in RAM. In addition to the obvious sorted order/binary search combination we consider the Eytzinger (BFS) layout normally used for heaps, an implicit B-tree layout that generalizes the Eytzinger layout, and the van Emde Boas layout commonly used in the cache-oblivious algorithms literature. After extensive testing and tuning on a wide variety of modern hardware, we arrive at the conclusion that, for small values of $n$, sorted order, combined with a good implementation of binary search is best. For larger values of $n$, we arrive at the surprising conclusion that the Eytzinger layout is usually the fastest. The latter conclusion is unexpected and goes counter to earlier experimental work by Brodal, Fagerberg, and Jacob (SODA~2003), who concluded that both the B-tree and van Emde Boas layouts were faster than the Eytzinger layout for large values of $n$. Our fastest C++ implementations, when compiled, use conditional moves to avoid branch mispredictions and prefetching to reduce cache latency.

arXiv comments
46 pages; 24 figures; updated after reviewing by ACM JEA
Journal reference
DOI
Subjects
cs.DSData Structures and Algorithms

On Hacker News

Linked by 9 identifiable commenters in 10 comments across 9 threads, from Oct 2015 to Jul 2026. Found in 8 HN submissions from Oct 2015 to Mar 2026. Open submission discussions.

Comments linking this paper, per year.

Discussed in comments9 threads

    • That one’s cache-oblivious so nominally the right answer if you don’t know how your cache works or have to deal with several of them at once, but the constants are bad enough to make it impractical[1,2]. Personally I’ve been on the lookout for a good vEB implementation for a…

      mananaysiempreView on HNParent context

  1. You can beat the binary search 1 comment
    • auto length = last - first; while (length > 0) { auto half = length / 2; if (comp(first[half], value)) { // length - half equals half + length % 2 first += length - half; } length = half; } return first; can be restructured with a conditional `first += half` and instead `length…

      pkhuongView on HNParent context

    • Very interesting post. The best results I knew of for static ordered search [1] were from the 2015 Khuong-Morin paper [2], which is not cited in the article but the author cites it in the previous post, and in their benchmarks Eytzinger+prefeching performed best. The improvement…

      otView on HNParent context

    • The more recent results (by the same author) is this paper: https://arxiv.org/pdf/1509.05053.pdf The conclusion is still that CMOV is best, but once the data stops fitting in the cache there's alternatives worth considering.

      pubbyView on HNParent context

    • > That is because you no longer need to compute a mid-point at every iteration, those are now pre-computed in the ordered array. This is called the Eytzinger layout [1]. [1] https://arxiv.org/abs/1509.05053

      lifthrasiirView on HNParent context

  2. Lomuto's Comeback 1 comment
    • Reminds me of this paper on binary search, which also shows a speed up using branch-free comparisons. "Array Layouts for Comparison-Based Searching" https://arxiv.org/pdf/1509.05053.pdf

      ArcuruView on HNParent context

    • Have a look at https://arxiv.org/abs/1509.05053 from pkhuong. They discuss this issue and other variants that paper. So e.g. branchless binary search, the branchy binary search + speculative execution as a form of prefetching, branchless binary search with explicit prefetching.

      jsnellView on HNParent context

    • I try to refrain from purely negative commentary on articles, but yuck! How can one hope to say anything useful about "memory performance" without once using the terms "latency" or "bandwidth"? Memory performance is getting higher in the same way that processor performance is…

      nkurzView on HNParent context

  3. Assist threads 1 comment
    • Just came across a good paper that discusses these issues: http://arxiv.org/pdf/1509.05053v1 The approach it from a different angle, but come to similar conclusions: Our conclusion is contrary to our expectations and to previous findings and goes against conventional wisdom…

      nkurzView on HNParent context

Submitted to Hacker News

Connected through shared HN threads, not inferred similarity or a recommendation.