Browse Source

small optimizations(?)

Roberto Ierusalimschy 26 years ago
parent
commit
933bead92e
1 changed files with 36 additions and 49 deletions
  1. 36 49
      ltable.c

+ 36 - 49
ltable.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ltable.c,v 1.16 1998/12/30 13:14:46 roberto Exp $
+** $Id: ltable.c,v 1.17 1999/01/04 12:54:33 roberto Exp $
 ** Lua tables (hash)
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -24,8 +24,7 @@
 
 
 
 
 
 
-static long int hashindex (TObject *ref)
-{
+static long int hashindex (TObject *ref) {
   long int h;
   long int h;
   switch (ttype(ref)) {
   switch (ttype(ref)) {
     case LUA_T_NUMBER:
     case LUA_T_NUMBER:
@@ -54,57 +53,46 @@ static long int hashindex (TObject *ref)
 }
 }
 
 
 
 
-static int present (Hash *t, TObject *key)
-{
+static Node *present (Hash *t, TObject *key) {
   int tsize = nhash(t);
   int tsize = nhash(t);
   long int h = hashindex(key);
   long int h = hashindex(key);
   int h1 = h%tsize;
   int h1 = h%tsize;
-  TObject *rf = ref(node(t, h1));
-  if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
+  Node *n = node(t, h1);
+  /* keep looking until an entry with "ref" equal to key or nil */
+  if ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n))
+                                   : ttype(ref(n)) != LUA_T_NIL)) {
     int h2 = h%(tsize-2) + 1;
     int h2 = h%(tsize-2) + 1;
     do {
     do {
       h1 += h2;
       h1 += h2;
       if (h1 >= tsize) h1 -= tsize;
       if (h1 >= tsize) h1 -= tsize;
-      rf = ref(node(t, h1));
-    } while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
+      n = node(t, h1);
+    } while ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n))
+                                          : ttype(ref(n)) != LUA_T_NIL));
   }
   }
-  return h1;
+  return n;
 }
 }
 
 
 
 
-/*
-** Alloc a vector node
-*/
-static Node *hashnodecreate (int nhash)
-{
-  Node *v = luaM_newvector(nhash, Node);
-  int i;
-  for (i=0; i<nhash; i++)
-    ttype(ref(&v[i])) = LUA_T_NIL;
-  return v;
-}
-
-/*
-** Delete a hash
-*/
-static void hashdelete (Hash *t)
-{
-  luaM_free(nodevector(t));
-  luaM_free(t);
-}
-
-
-void luaH_free (Hash *frees)
-{
+void luaH_free (Hash *frees) {
   while (frees) {
   while (frees) {
     Hash *next = (Hash *)frees->head.next;
     Hash *next = (Hash *)frees->head.next;
     L->nblocks -= gcsize(frees->nhash);
     L->nblocks -= gcsize(frees->nhash);
-    hashdelete(frees);
+    luaM_free(nodevector(frees));
+    luaM_free(frees);
     frees = next;
     frees = next;
   }
   }
 }
 }
 
 
 
 
+static Node *hashnodecreate (int nhash) {
+  Node *v = luaM_newvector(nhash, Node);
+  int i;
+  for (i=0; i<nhash; i++)
+    ttype(ref(&v[i])) = LUA_T_NIL;
+  return v;
+}
+
+
 Hash *luaH_new (int nhash) {
 Hash *luaH_new (int nhash) {
   Hash *t = luaM_new(Hash);
   Hash *t = luaM_new(Hash);
   nhash = luaO_redimension(nhash*3/2);
   nhash = luaO_redimension(nhash*3/2);
@@ -130,6 +118,7 @@ static int newsize (Hash *t) {
   return luaO_redimension((realuse+1)*2);  /* +1 is the new element */
   return luaO_redimension((realuse+1)*2);  /* +1 is the new element */
 }
 }
 
 
+
 static void rehash (Hash *t) {
 static void rehash (Hash *t) {
   int nold = nhash(t);
   int nold = nhash(t);
   Node *vold = nodevector(t);
   Node *vold = nodevector(t);
@@ -141,7 +130,7 @@ static void rehash (Hash *t) {
   for (i=0; i<nold; i++) {
   for (i=0; i<nold; i++) {
     Node *n = vold+i;
     Node *n = vold+i;
     if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
     if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) {
-      *node(t, present(t, ref(n))) = *n;  /* copy old node to luaM_new hash */
+      *present(t, ref(n)) = *n;  /* copy old node to new hash */
       nuse(t)++;
       nuse(t)++;
     }
     }
   }
   }
@@ -149,29 +138,28 @@ static void rehash (Hash *t) {
   luaM_free(vold);
   luaM_free(vold);
 }
 }
 
 
+
 /*
 /*
 ** If the hash node is present, return its pointer, otherwise return
 ** If the hash node is present, return its pointer, otherwise return
 ** null.
 ** null.
 */
 */
-TObject *luaH_get (Hash *t, TObject *ref)
-{
- int h = present(t, ref);
- if (ttype(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
+TObject *luaH_get (Hash *t, TObject *ref) {
+ Node *n = present(t, ref);
+ if (ttype(ref(n)) != LUA_T_NIL) return val(n);
  else return &luaO_nilobject;
  else return &luaO_nilobject;
 }
 }
 
 
 
 
 /*
 /*
-** If the hash node is present, return its pointer, otherwise create a luaM_new
+** If the hash node is present, return its pointer, otherwise create a new
 ** node for the given reference and also return its pointer.
 ** node for the given reference and also return its pointer.
 */
 */
-TObject *luaH_set (Hash *t, TObject *ref)
-{
-  Node *n = node(t, present(t, ref));
+TObject *luaH_set (Hash *t, TObject *ref) {
+  Node *n = present(t, ref);
   if (ttype(ref(n)) == LUA_T_NIL) {
   if (ttype(ref(n)) == LUA_T_NIL) {
     if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
     if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
       rehash(t);
       rehash(t);
-      n = node(t, present(t, ref));
+      n = present(t, ref);
     }
     }
     nuse(t)++;
     nuse(t)++;
     *ref(n) = *ref;
     *ref(n) = *ref;
@@ -192,18 +180,17 @@ static Node *hashnext (Hash *t, int i) {
       return NULL;
       return NULL;
     n = node(t, i);
     n = node(t, i);
   }
   }
-  return node(t, i);
+  return n;
 }
 }
 
 
 Node *luaH_next (Hash *t, TObject *r) {
 Node *luaH_next (Hash *t, TObject *r) {
   if (ttype(r) == LUA_T_NIL)
   if (ttype(r) == LUA_T_NIL)
     return hashnext(t, 0);
     return hashnext(t, 0);
   else {
   else {
-    int i = present(t, r);
-    Node *n = node(t, i);
+    Node *n = present(t, r);
     luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
     luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL,
                    2, "key not found");
                    2, "key not found");
-    return hashnext(t, i+1);
+    return hashnext(t, (n-(t->node))+1);
   }
   }
 }
 }