From 392aff70992260267590f431fb50c87b9d445ce2 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 2 Jan 2021 16:47:59 +0900 Subject: Use Jenkins One At A Time Hash for `mrb_str_hash()`. --- src/string.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/string.c b/src/string.c index 749d32698..946dc8be1 100644 --- a/src/string.c +++ b/src/string.c @@ -1752,13 +1752,17 @@ mrb_str_hash(mrb_state *mrb, mrb_value str) struct RString *s = mrb_str_ptr(str); mrb_int len = RSTR_LEN(s); char *p = RSTR_PTR(s); - uint32_t key = 0; + uint32_t hash = 0; - while (len--) { - key = (key << 6) + (key << 16) - key + *p; - p++; + for(int i = 0; i < len; ++i) { + hash += p[i]; + hash += (hash << 10); + hash ^= (hash >> 6); } - return key + (key>>5); + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + return hash; } /* 15.2.10.5.20 */ -- cgit v1.2.3