aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-03-12 11:29:34 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-03-12 11:29:34 -0300
commit014daf43cb3bf1ceb6af102c9294ec04abf9a6b6 (patch)
treecc6928f3ce544b6a93a103591b071cc28bd70af3
parent05b13651f96117674ba30dace03620658760acbd (diff)
downloadlua-014daf43cb3bf1ceb6af102c9294ec04abf9a6b6.tar.gz
lua-014daf43cb3bf1ceb6af102c9294ec04abf9a6b6.tar.bz2
lua-014daf43cb3bf1ceb6af102c9294ec04abf9a6b6.zip
Details
Comments and order of hashing macros in 'ltable.c'.
-rw-r--r--ltable.c61
1 files changed, 40 insertions, 21 deletions
diff --git a/ltable.c b/ltable.c
index b520cdf4..33c1ab30 100644
--- a/ltable.c
+++ b/ltable.c
@@ -68,20 +68,25 @@
68#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node) 68#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
69 69
70 70
71/*
72** When the original hash value is good, hashing by a power of 2
73** avoids the cost of '%'.
74*/
71#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) 75#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
72 76
73#define hashstr(t,str) hashpow2(t, (str)->hash)
74#define hashboolean(t,p) hashpow2(t, p)
75#define hashint(t,i) hashpow2(t, i)
76
77
78/* 77/*
79** for some types, it is better to avoid modulus by power of 2, as 78** for other types, it is better to avoid modulo by power of 2, as
80** they tend to have many 2 factors. 79** they can have many 2 factors.
81*/ 80*/
82#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) 81#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
83 82
84 83
84#define hashstr(t,str) hashpow2(t, (str)->hash)
85#define hashboolean(t,p) hashpow2(t, p)
86
87#define hashint(t,i) hashpow2(t, i)
88
89
85#define hashpointer(t,p) hashmod(t, point2uint(p)) 90#define hashpointer(t,p) hashmod(t, point2uint(p))
86 91
87 92
@@ -135,24 +140,38 @@ static int l_hashfloat (lua_Number n) {
135*/ 140*/
136static Node *mainposition (const Table *t, int ktt, const Value *kvl) { 141static Node *mainposition (const Table *t, int ktt, const Value *kvl) {
137 switch (withvariant(ktt)) { 142 switch (withvariant(ktt)) {
138 case LUA_VNUMINT: 143 case LUA_VNUMINT: {
139 return hashint(t, ivalueraw(*kvl)); 144 lua_Integer key = ivalueraw(*kvl);
140 case LUA_VNUMFLT: 145 return hashint(t, key);
141 return hashmod(t, l_hashfloat(fltvalueraw(*kvl))); 146 }
142 case LUA_VSHRSTR: 147 case LUA_VNUMFLT: {
143 return hashstr(t, tsvalueraw(*kvl)); 148 lua_Number n = fltvalueraw(*kvl);
144 case LUA_VLNGSTR: 149 return hashmod(t, l_hashfloat(n));
145 return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl))); 150 }
151 case LUA_VSHRSTR: {
152 TString *ts = tsvalueraw(*kvl);
153 return hashstr(t, ts);
154 }
155 case LUA_VLNGSTR: {
156 TString *ts = tsvalueraw(*kvl);
157 return hashpow2(t, luaS_hashlongstr(ts));
158 }
146 case LUA_VFALSE: 159 case LUA_VFALSE:
147 return hashboolean(t, 0); 160 return hashboolean(t, 0);
148 case LUA_VTRUE: 161 case LUA_VTRUE:
149 return hashboolean(t, 1); 162 return hashboolean(t, 1);
150 case LUA_VLIGHTUSERDATA: 163 case LUA_VLIGHTUSERDATA: {
151 return hashpointer(t, pvalueraw(*kvl)); 164 void *p = pvalueraw(*kvl);
152 case LUA_VLCF: 165 return hashpointer(t, p);
153 return hashpointer(t, fvalueraw(*kvl)); 166 }
154 default: 167 case LUA_VLCF: {
155 return hashpointer(t, gcvalueraw(*kvl)); 168 lua_CFunction f = fvalueraw(*kvl);
169 return hashpointer(t, f);
170 }
171 default: {
172 GCObject *o = gcvalueraw(*kvl);
173 return hashpointer(t, o);
174 }
156 } 175 }
157} 176}
158 177