summaryrefslogtreecommitdiff
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-github-014daf43cb3bf1ceb6af102c9294ec04abf9a6b6.tar.gz
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 @@
#define MAXHSIZE luaM_limitN(1u << MAXHBITS, Node)
+/*
+** When the original hash value is good, hashing by a power of 2
+** avoids the cost of '%'.
+*/
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
-#define hashstr(t,str) hashpow2(t, (str)->hash)
-#define hashboolean(t,p) hashpow2(t, p)
-#define hashint(t,i) hashpow2(t, i)
-
-
/*
-** for some types, it is better to avoid modulus by power of 2, as
-** they tend to have many 2 factors.
+** for other types, it is better to avoid modulo by power of 2, as
+** they can have many 2 factors.
*/
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
+#define hashstr(t,str) hashpow2(t, (str)->hash)
+#define hashboolean(t,p) hashpow2(t, p)
+
+#define hashint(t,i) hashpow2(t, i)
+
+
#define hashpointer(t,p) hashmod(t, point2uint(p))
@@ -135,24 +140,38 @@ static int l_hashfloat (lua_Number n) {
*/
static Node *mainposition (const Table *t, int ktt, const Value *kvl) {
switch (withvariant(ktt)) {
- case LUA_VNUMINT:
- return hashint(t, ivalueraw(*kvl));
- case LUA_VNUMFLT:
- return hashmod(t, l_hashfloat(fltvalueraw(*kvl)));
- case LUA_VSHRSTR:
- return hashstr(t, tsvalueraw(*kvl));
- case LUA_VLNGSTR:
- return hashpow2(t, luaS_hashlongstr(tsvalueraw(*kvl)));
+ case LUA_VNUMINT: {
+ lua_Integer key = ivalueraw(*kvl);
+ return hashint(t, key);
+ }
+ case LUA_VNUMFLT: {
+ lua_Number n = fltvalueraw(*kvl);
+ return hashmod(t, l_hashfloat(n));
+ }
+ case LUA_VSHRSTR: {
+ TString *ts = tsvalueraw(*kvl);
+ return hashstr(t, ts);
+ }
+ case LUA_VLNGSTR: {
+ TString *ts = tsvalueraw(*kvl);
+ return hashpow2(t, luaS_hashlongstr(ts));
+ }
case LUA_VFALSE:
return hashboolean(t, 0);
case LUA_VTRUE:
return hashboolean(t, 1);
- case LUA_VLIGHTUSERDATA:
- return hashpointer(t, pvalueraw(*kvl));
- case LUA_VLCF:
- return hashpointer(t, fvalueraw(*kvl));
- default:
- return hashpointer(t, gcvalueraw(*kvl));
+ case LUA_VLIGHTUSERDATA: {
+ void *p = pvalueraw(*kvl);
+ return hashpointer(t, p);
+ }
+ case LUA_VLCF: {
+ lua_CFunction f = fvalueraw(*kvl);
+ return hashpointer(t, f);
+ }
+ default: {
+ GCObject *o = gcvalueraw(*kvl);
+ return hashpointer(t, o);
+ }
}
}