From aef4fe06b35d7086885940b19a43c255a8d15b96 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Mon, 11 Aug 2025 11:07:22 +0200 Subject: [PATCH] lib/kru: deduplicate kru_limited_prefetch_hash() Move the function up unchanged and use it on two places which have the exact lines as the function. Also fix its description. --- lib/kru.inc.c | 65 ++++++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 45 deletions(-) diff --git a/lib/kru.inc.c b/lib/kru.inc.c index b556f1680..162b1ab6d 100644 --- a/lib/kru.inc.c +++ b/lib/kru.inc.c @@ -245,6 +245,24 @@ struct query_ctx { uint16_t *load; }; +/// Phase 1/3 of a query -- prefetch, ctx init. Based on a kru_hash_t. +static inline void kru_limited_prefetch_hash(struct kru *kru, uint32_t time_now, kru_hash_t hash, kru_price_t price, struct query_ctx *ctx) +{ + // Choose the cache-lines to operate on + const uint32_t loads_mask = (1 << kru->loads_bits) - 1; + // Fetch the two cache-lines in parallel before we really touch them. + for (int li = 0; li < TABLE_COUNT; ++li) { + struct load_cl * const l = &kru->load_cls[hash & loads_mask][li]; + __builtin_prefetch(l, 0); // hope for read-only access + hash >>= kru->loads_bits; + ctx->l[li] = l; + } + + ctx->time_now = time_now; + ctx->price = price; + ctx->id = hash; +} + /// Phase 1/3 of a query -- hash, prefetch, ctx init. Based on one 16-byte key. static inline void kru_limited_prefetch(struct kru *kru, uint32_t time_now, uint8_t key[static 16], kru_price_t price, struct query_ctx *ctx) { @@ -266,20 +284,7 @@ static inline void kru_limited_prefetch(struct kru *kru, uint32_t time_now, uint memcpy(&hash, &h, sizeof(hash)); } #endif - - // Choose the cache-lines to operate on - const uint32_t loads_mask = (1 << kru->loads_bits) - 1; - // Fetch the two cache-lines in parallel before we really touch them. - for (int li = 0; li < TABLE_COUNT; ++li) { - struct load_cl * const l = &kru->load_cls[hash & loads_mask][li]; - __builtin_prefetch(l, 0); // hope for read-only access - hash >>= kru->loads_bits; - ctx->l[li] = l; - } - - ctx->time_now = time_now; - ctx->price = price; - ctx->id = hash; + kru_limited_prefetch_hash(kru, time_now, hash, price, ctx); } /// Phase 1/3 of a query -- hash, prefetch, ctx init. Based on a bit prefix of one 16-byte key. @@ -342,19 +347,7 @@ static inline void kru_limited_prefetch_prefix(struct kru *kru, uint32_t time_no } #endif - // Choose the cache-lines to operate on - const uint32_t loads_mask = (1 << kru->loads_bits) - 1; - // Fetch the two cache-lines in parallel before we really touch them. - for (int li = 0; li < TABLE_COUNT; ++li) { - struct load_cl * const l = &kru->load_cls[hash & loads_mask][li]; - __builtin_prefetch(l, 0); // hope for read-only access - hash >>= kru->loads_bits; - ctx->l[li] = l; - } - - ctx->time_now = time_now; - ctx->price = price; - ctx->id = hash; + kru_limited_prefetch_hash(kru, time_now, hash, price, ctx); } static kru_hash_t kru_hash_bytes(struct kru *kru, uint8_t *key, size_t key_size) @@ -372,24 +365,6 @@ static kru_hash_t kru_hash_bytes(struct kru *kru, uint8_t *key, size_t key_size) return hash; } -/// Phase 1/3 of a query -- hash, prefetch, ctx init. Based on arbitrary-length byte-stream. -static inline void kru_limited_prefetch_hash(struct kru *kru, uint32_t time_now, kru_hash_t hash, kru_price_t price, struct query_ctx *ctx) -{ - // Choose the cache-lines to operate on - const uint32_t loads_mask = (1 << kru->loads_bits) - 1; - // Fetch the two cache-lines in parallel before we really touch them. - for (int li = 0; li < TABLE_COUNT; ++li) { - struct load_cl * const l = &kru->load_cls[hash & loads_mask][li]; - __builtin_prefetch(l, 0); // hope for read-only access - hash >>= kru->loads_bits; - ctx->l[li] = l; - } - - ctx->time_now = time_now; - ctx->price = price; - ctx->id = hash; -} - /// Phase 2/3 of a query -- returns answer with no state modification (except update_time). static inline bool kru_limited_fetch(struct kru *kru, struct query_ctx *ctx) { -- 2.47.2