From 7cfc86335389f1dc2312f4e3200f3e8474d983e1 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 2 Aug 2025 23:23:26 +0900 Subject: [PATCH] khash: add hash calculation helper to eliminate duplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added kh_key_idx_##name() helper function to encapsulate the repeated pattern of calculating bucket index from key hash. Replaced 2 instances of manual hash calculation: - __hash_func(mrb,key) & khash_mask(h) → kh_key_idx_##name(mrb, key, h) This eliminates the duplicated hash-and-mask pattern and makes the code more readable by clearly expressing the intent (get bucket index for key). Co-authored-by: Claude --- include/mruby/khash.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/mruby/khash.h b/include/mruby/khash.h index 2ebf0bb50..086f26b7a 100644 --- a/include/mruby/khash.h +++ b/include/mruby/khash.h @@ -127,6 +127,9 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0}; uint8_t *flags = kh_flags_##name(h); \ flags[i/4] |= __m_del[i%4]; /* Set deleted bit */ \ } \ + static inline khint_t kh_key_idx_##name(mrb_state *mrb, khkey_t key, kh_##name##_t *h) { \ + return __hash_func(mrb, key) & khash_mask(h); \ + } \ /* Small table optimization functions */ \ static inline int kh_is_small_##name(const kh_##name##_t *h) { \ return h->n_buckets == 0; /* Small table marker */ \ @@ -216,7 +219,7 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0}; /* Cache calculated pointers for performance */ \ khkey_t *keys = kh_keys_##name(h); \ uint8_t *ed_flags = kh_flags_##name(h); \ - khint_t k = __hash_func(mrb,key) & khash_mask(h), step = 0; \ + khint_t k = kh_key_idx_##name(mrb, key, h), step = 0; \ (void)mrb; \ while (!__ac_isempty(ed_flags, k)) { \ if (!__ac_isdel(ed_flags, k)) { \ @@ -269,7 +272,7 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0}; /* Cache calculated pointers for performance */ \ khkey_t *keys = kh_keys_##name(h); \ uint8_t *ed_flags = kh_flags_##name(h); \ - k = __hash_func(mrb,key) & khash_mask(h); \ + k = kh_key_idx_##name(mrb, key, h); \ del_k = kh_end(h); \ while (!__ac_isempty(ed_flags, k)) { \ if (!__ac_isdel(ed_flags, k)) { \