From 73111cacfbd6e729b61e644d8e85cbd7513eb532 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sun, 3 Aug 2025 16:08:37 +0900 Subject: [PATCH] khash.h: add linear probing helper to eliminate duplication Added kh_next_probe_##name() helper function to encapsulate the repeated linear probing step calculation pattern. Replaced 2 instances of manual probing calculation: - k = (k+(++step)) & khash_mask(h) -> k = kh_next_probe_##name(k, &step, h) This eliminates the duplicated bit manipulation pattern and makes the probing logic more readable and less error-prone. 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 9a17789ee..f96838b89 100644 --- a/include/mruby/khash.h +++ b/include/mruby/khash.h @@ -133,6 +133,9 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0}; 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); \ } \ + static inline khint_t kh_next_probe_##name(khint_t k, khint_t *step, kh_##name##_t *h) { \ + return (k+(++(*step))) & 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 */ \ @@ -233,7 +236,7 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0}; if (!__ac_isdel(ed_flags, k)) { \ if (__hash_equal(mrb, keys[k], key)) return k; \ } \ - k = (k+(++step)) & khash_mask(h); \ + k = kh_next_probe_##name(k, &step, h); \ } \ return kh_end(h); \ } \ @@ -268,7 +271,7 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0}; else if (del_k == kh_end(h)) { \ del_k = k; \ } \ - k = (k+(++step)) & khash_mask(h); \ + k = kh_next_probe_##name(k, &step, h); \ } \ if (del_k != kh_end(h)) { \ /* put at del */ \