Skip to content

Commit

Permalink
Bucket scan optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
kg committed Apr 10, 2024
1 parent b5f1796 commit c08f4fd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/native/containers/dn-simdhash-specialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ DN_SIMDHASH_SCAN_BUCKET_INTERNAL (DN_SIMDHASH_T_PTR hash, bucket_t *bucket, DN_S
#else
uint32_t index = find_first_matching_suffix(search_vector, bucket_suffixes, count);
#endif
DN_SIMDHASH_KEY_T *key = &bucket->keys[index];

for (; index < count; index++, key++) {
for (; index < count; index++) {
// FIXME: Could be profitable to manually hoist the data load outside of the loop,
// if not out of SCAN_BUCKET_INTERNAL entirely. Clang appears to do LICM on it.
if (DN_SIMDHASH_KEY_EQUALS(DN_SIMDHASH_GET_DATA(hash), needle, *key))
// It's better to index bucket->keys each iteration inside the loop than to precompute
// a pointer outside and bump the pointer, because in many cases the bucket will be
// empty, and in many other cases it will have one match. Putting the index inside the
// loop means that for empty/no-match buckets we don't do the index calculation at all.
if (DN_SIMDHASH_KEY_EQUALS(DN_SIMDHASH_GET_DATA(hash), needle, bucket->keys[index]))
return index;
}

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

#define END_SCAN_BUCKETS(initial_index, bucket_index, bucket_address) \
bucket_index++; \
bucket_address++; \
/* Wrap around if we hit the last bucket. */ \
if (bucket_index >= buffers.buckets_length) { \
bucket_index = 0; \
bucket_address = address_of_bucket(buffers, 0); \
} else { \
bucket_address++; \
} \
/* if bucket_index == initial_index, we reached our starting point */ \
} while (bucket_index != initial_index); \
Expand Down
2 changes: 1 addition & 1 deletion src/native/containers/dn-simdhash-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct {
float f;
} instance_data_t;

static inline uint8_t
static DN_FORCEINLINE(uint8_t)
key_comparer (instance_data_t data, size_t lhs, size_t rhs) {
return ((data.f == 4.20f) || (lhs == rhs));
}
Expand Down

0 comments on commit c08f4fd

Please sign in to comment.