aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-03-29 15:47:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-03-29 15:47:18 -0300
commit7fbe2158089898f09d741e991737f282e514ffaa (patch)
tree798f3843c162f260beea1ca2c334dd76a34708eb
parentbf10593a3a912cd3cac69569c7474e687c0d0cd8 (diff)
downloadlua-7fbe2158089898f09d741e991737f282e514ffaa.tar.gz
lua-7fbe2158089898f09d741e991737f282e514ffaa.tar.bz2
lua-7fbe2158089898f09d741e991737f282e514ffaa.zip
New hash function for integer keys
When integer keys do not form a sequence, it is better to use all their bits to compute their hashes. (The previous implementation was quite bad for integer keys with common lower bits, and disastrous for integer keys changing only in their upper 32 bits.)
-rw-r--r--ltable.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/ltable.c b/ltable.c
index 33c1ab30..af878368 100644
--- a/ltable.c
+++ b/ltable.c
@@ -84,8 +84,6 @@
84#define hashstr(t,str) hashpow2(t, (str)->hash) 84#define hashstr(t,str) hashpow2(t, (str)->hash)
85#define hashboolean(t,p) hashpow2(t, p) 85#define hashboolean(t,p) hashpow2(t, p)
86 86
87#define hashint(t,i) hashpow2(t, i)
88
89 87
90#define hashpointer(t,p) hashmod(t, point2uint(p)) 88#define hashpointer(t,p) hashmod(t, point2uint(p))
91 89
@@ -101,6 +99,20 @@ static const Node dummynode_ = {
101static const TValue absentkey = {ABSTKEYCONSTANT}; 99static const TValue absentkey = {ABSTKEYCONSTANT};
102 100
103 101
102/*
103** Hash for integers. To allow a good hash, use the remainder operator
104** ('%'). If integer fits as a non-negative int, compute an int
105** remainder, which is faster. Otherwise, use an unsigned-integer
106** remainder, which uses all bits and ensures a non-negative result.
107*/
108static Node *hashint (const Table *t, lua_Integer i) {
109 lua_Unsigned ui = l_castS2U(i);
110 if (ui <= (unsigned int)INT_MAX)
111 return hashmod(t, cast_int(ui));
112 else
113 return hashmod(t, ui);
114}
115
104 116
105/* 117/*
106** Hash for floating-point numbers. 118** Hash for floating-point numbers.