Skip to content

Commit

Permalink
Improving iterator using prefetch
Browse files Browse the repository at this point in the history
Signed-off-by: NadavGigi <[email protected]>
  • Loading branch information
NadavGigi committed Jan 7, 2025
1 parent e1db553 commit 4de5b1a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,32 @@ static inline incrementalFind *incrementalFindFromOpaque(hashtableIncrementalFin
return (incrementalFind *)(void *)state;
}

static void prefetchBucketEntries(bucket *b) {
if (b->chained) {
valkey_prefetch(bucketNext(b));
}
for (int pos = 0; pos < numBucketPositions(b); pos++) {
if (isPositionFilled(b, pos)) {
valkey_prefetch(b->entries[pos]);
}
}
}

static void prefetchNextFilledBucket(iter *iter, bucket *current_bucket) {
if (current_bucket->chained) {
prefetchBucketEntries(bucketNext(current_bucket));
} else {
size_t table_size = numBuckets(iter->hashtable->bucket_exp[iter->table]);
for (size_t bucket_index = (size_t)iter->index + 1; bucket_index < table_size; bucket_index++) {
bucket *b = &iter->hashtable->tables[iter->table][bucket_index];
if (b->presence) {
prefetchBucketEntries(b);
break;
}
}
}
}

/* --- API functions --- */

/* Allocates and initializes a new hashtable specified by the given type. */
Expand Down Expand Up @@ -1890,6 +1916,9 @@ int hashtableNext(hashtableIterator *iterator, void **elemptr) {
}
}
bucket *b = iter->bucket;
if (iter->pos_in_bucket == 0 && b->presence) {
prefetchNextFilledBucket(iter, b);
}
if (!isPositionFilled(b, iter->pos_in_bucket)) {
/* No entry here. */
continue;
Expand Down

0 comments on commit 4de5b1a

Please sign in to comment.