Skip to content

Commit b5f1796

Browse files
committed
Optimize bucket scanning
1 parent 7737738 commit b5f1796

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

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

+2-14
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,11 @@ static DN_FORCEINLINE(dn_simdhash_suffixes)
8383
build_search_vector (uint8_t needle)
8484
{
8585
dn_simdhash_suffixes result;
86-
// this produces a splat and then .const, .and in wasm, and the other architectures are fine too
86+
// this produces a splat in wasm, and the other architectures are fine too
8787
dn_u8x16 needles = {
8888
needle, needle, needle, needle, needle, needle, needle, needle,
8989
needle, needle, needle, needle, needle, needle, needle, needle
9090
};
91-
/*
92-
// TODO: Evaluate whether the & mask is actually worth it. In the C# prototype, it wasn't.
93-
// Not doing it means there is a ~1% chance of a false positive in the data bytes near the
94-
// end of the bucket, but we check the index against the bucket count anyway, so...
95-
dn_u8x16 mask = {
96-
0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu,
97-
0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0x00u, 0x00u
98-
};
99-
result.vec = needles & mask;
100-
*/
10191
result.vec = needles;
10292
return result;
10393
}
@@ -169,9 +159,7 @@ build_search_vector (uint8_t needle)
169159
static DN_FORCEINLINE(uint32_t)
170160
find_first_matching_suffix (dn_simdhash_suffixes needle, dn_simdhash_suffixes haystack, uint32_t count)
171161
{
172-
// FIXME: Completely untested.
173-
__m128i match_vector = _mm_cmpeq_epi8(needle.m128, haystack.m128);
174-
return ctz(_mm_movemask_epi8(match_vector));
162+
return ctz(_mm_movemask_epi8(_mm_cmpeq_epi8(needle.m128, haystack.m128)));
175163
}
176164

177165
#else // unknown compiler and/or unknown non-simd arch

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,18 @@ DN_SIMDHASH_SCAN_BUCKET_INTERNAL (DN_SIMDHASH_T_PTR hash, bucket_t *bucket, DN_S
165165
#define BEGIN_SCAN_BUCKETS(initial_index, bucket_index, bucket_address) \
166166
{ \
167167
uint32_t bucket_index = initial_index; \
168-
bucket_t *bucket_address; \
169-
do { \
170-
bucket_address = address_of_bucket(buffers, bucket_index);
168+
bucket_t *bucket_address = address_of_bucket(buffers, bucket_index); \
169+
do {
171170

172171
#define END_SCAN_BUCKETS(initial_index, bucket_index, bucket_address) \
173172
bucket_index++; \
174173
/* Wrap around if we hit the last bucket. */ \
175-
if (bucket_index >= buffers.buckets_length) \
174+
if (bucket_index >= buffers.buckets_length) { \
176175
bucket_index = 0; \
176+
bucket_address = address_of_bucket(buffers, 0); \
177+
} else { \
178+
bucket_address++; \
179+
} \
177180
/* if bucket_index == initial_index, we reached our starting point */ \
178181
} while (bucket_index != initial_index); \
179182
}

0 commit comments

Comments
 (0)