Skip to content

Commit 9850915

Browse files
authored
optimize bin search (#807)
1 parent 6cbc6f9 commit 9850915

1 file changed

Lines changed: 79 additions & 13 deletions

File tree

‎include/roaring/array_util.h‎

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,95 @@ namespace internal {
3535
#endif
3636

3737
/*
38-
* Good old binary search.
38+
* Sorted-array search.
3939
* Assumes that array is sorted, has logarithmic complexity.
4040
* if the result is x, then:
4141
* if ( x>0 ) you have array[x] = ikey
4242
* if ( x<0 ) then inserting ikey at position -x-1 in array (insuring that
4343
* array[-x-1]=ikey) keys the array sorted.
44+
*
45+
* Adapted from array_container_contains: a SIMD-quad block-narrowing
46+
* search at gap=16 (Daniel Lemire,
47+
* https://lemire.me/blog/2026/04/27/you-can-beat-the-binary-search/)
48+
* followed by a scalar in-block scan that recovers the exact insertion
49+
* point required by the binarySearch contract.
4450
*/
4551
inline int32_t binarySearch(const uint16_t *array, int32_t lenarray,
4652
uint16_t ikey) {
47-
int32_t low = 0;
48-
int32_t high = lenarray - 1;
49-
while (low <= high) {
50-
int32_t middleIndex = (low + high) >> 1;
51-
uint16_t middleValue = array[middleIndex];
52-
if (middleValue < ikey) {
53-
low = middleIndex + 1;
54-
} else if (middleValue > ikey) {
55-
high = middleIndex - 1;
56-
} else {
57-
return middleIndex;
53+
const int32_t gap = 16;
54+
if (lenarray < gap) {
55+
for (int32_t j = 0; j < lenarray; j++) {
56+
if (array[j] >= ikey) {
57+
return (array[j] == ikey) ? j : -(j + 1);
58+
}
59+
}
60+
return -(lenarray + 1);
61+
}
62+
const int32_t num_blocks = lenarray / gap;
63+
int32_t base = 0;
64+
int32_t n = num_blocks;
65+
while (n > 3) {
66+
int32_t quarter = n >> 2;
67+
68+
int32_t k1 = array[(base + quarter + 1) * gap - 1];
69+
int32_t k2 = array[(base + 2 * quarter + 1) * gap - 1];
70+
int32_t k3 = array[(base + 3 * quarter + 1) * gap - 1];
71+
72+
int32_t c1 = (k1 < ikey);
73+
int32_t c2 = (k2 < ikey);
74+
int32_t c3 = (k3 < ikey);
75+
76+
base += (c1 + c2 + c3) * quarter;
77+
n -= 3 * quarter;
78+
}
79+
while (n > 1) {
80+
int32_t half = n >> 1;
81+
base = (array[(base + half + 1) * gap - 1] < ikey) ? base + half : base;
82+
n -= half;
83+
}
84+
int32_t lo = (array[(base + 1) * gap - 1] < ikey) ? base + 1 : base;
85+
86+
if (lo < num_blocks) {
87+
const int32_t start = lo * gap;
88+
#if defined(CROARING_IS_X64)
89+
// SSE2: subs_epu16 yields zero where lane >= ikey. movemask of an
90+
// epi16 compare gives 2 bits per lane; ctz>>1 = lane index. Scan
91+
// the first 8 lanes first and exit early when they contain the
92+
// answer; otherwise the block-narrowing invariant guarantees the
93+
// second-half mask is non-zero.
94+
__m128i needle = _mm_set1_epi16((short)ikey);
95+
__m128i zero = _mm_setzero_si128();
96+
__m128i v0 = _mm_loadu_si128((const __m128i *)(array + start));
97+
__m128i ge0 = _mm_cmpeq_epi16(_mm_subs_epu16(needle, v0), zero);
98+
unsigned m0 = (unsigned)_mm_movemask_epi8(ge0);
99+
if (m0 != 0) {
100+
int32_t j = start + (int32_t)(roaring_trailing_zeroes(m0) >> 1);
101+
return (array[j] == ikey) ? j : -(j + 1);
102+
}
103+
__m128i v1 = _mm_loadu_si128((const __m128i *)(array + start + 8));
104+
__m128i ge1 = _mm_cmpeq_epi16(_mm_subs_epu16(needle, v1), zero);
105+
unsigned m1 = (unsigned)_mm_movemask_epi8(ge1);
106+
int32_t j = start + 8 + (int32_t)(roaring_trailing_zeroes(m1) >> 1);
107+
return (array[j] == ikey) ? j : -(j + 1);
108+
#else
109+
const int32_t end = start + gap;
110+
for (int32_t j = start; j < end; j++) {
111+
if (array[j] >= ikey) {
112+
return (array[j] == ikey) ? j : -(j + 1);
113+
}
114+
}
115+
// Unreachable: the narrowing guarantees the last element of the
116+
// selected block is >= ikey.
117+
return -(end + 1);
118+
#endif
119+
}
120+
121+
for (int32_t j = num_blocks * gap; j < lenarray; j++) {
122+
if (array[j] >= ikey) {
123+
return (array[j] == ikey) ? j : -(j + 1);
58124
}
59125
}
60-
return -(low + 1);
126+
return -(lenarray + 1);
61127
}
62128

63129
/**

0 commit comments

Comments
 (0)