Skip to content

Commit c08f4fd

Browse files
committed
Bucket scan optimization
1 parent b5f1796 commit c08f4fd

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/native/containers/dn-simdhash-specialization.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,14 @@ DN_SIMDHASH_SCAN_BUCKET_INTERNAL (DN_SIMDHASH_T_PTR hash, bucket_t *bucket, DN_S
146146
#else
147147
uint32_t index = find_first_matching_suffix(search_vector, bucket_suffixes, count);
148148
#endif
149-
DN_SIMDHASH_KEY_T *key = &bucket->keys[index];
150-
151-
for (; index < count; index++, key++) {
149+
for (; index < count; index++) {
152150
// FIXME: Could be profitable to manually hoist the data load outside of the loop,
153151
// if not out of SCAN_BUCKET_INTERNAL entirely. Clang appears to do LICM on it.
154-
if (DN_SIMDHASH_KEY_EQUALS(DN_SIMDHASH_GET_DATA(hash), needle, *key))
152+
// It's better to index bucket->keys each iteration inside the loop than to precompute
153+
// a pointer outside and bump the pointer, because in many cases the bucket will be
154+
// empty, and in many other cases it will have one match. Putting the index inside the
155+
// loop means that for empty/no-match buckets we don't do the index calculation at all.
156+
if (DN_SIMDHASH_KEY_EQUALS(DN_SIMDHASH_GET_DATA(hash), needle, bucket->keys[index]))
155157
return index;
156158
}
157159

@@ -170,12 +172,11 @@ DN_SIMDHASH_SCAN_BUCKET_INTERNAL (DN_SIMDHASH_T_PTR hash, bucket_t *bucket, DN_S
170172

171173
#define END_SCAN_BUCKETS(initial_index, bucket_index, bucket_address) \
172174
bucket_index++; \
175+
bucket_address++; \
173176
/* Wrap around if we hit the last bucket. */ \
174177
if (bucket_index >= buffers.buckets_length) { \
175178
bucket_index = 0; \
176179
bucket_address = address_of_bucket(buffers, 0); \
177-
} else { \
178-
bucket_address++; \
179180
} \
180181
/* if bucket_index == initial_index, we reached our starting point */ \
181182
} while (bucket_index != initial_index); \

src/native/containers/dn-simdhash-test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef struct {
1717
float f;
1818
} instance_data_t;
1919

20-
static inline uint8_t
20+
static DN_FORCEINLINE(uint8_t)
2121
key_comparer (instance_data_t data, size_t lhs, size_t rhs) {
2222
return ((data.f == 4.20f) || (lhs == rhs));
2323
}

0 commit comments

Comments
 (0)