Browse Source

small optimizations (?)

Roberto Ierusalimschy 27 years ago
parent
commit
5981161360
2 changed files with 10 additions and 5 deletions
  1. 7 3
      lstring.c
  2. 3 2
      ltable.c

+ 7 - 3
lstring.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstring.c,v 1.8 1997/12/09 13:35:19 roberto Exp roberto $
+** $Id: lstring.c,v 1.9 1997/12/30 19:15:52 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -102,17 +102,21 @@ static TaggedString *insert (char *buff, int tag, stringtable *tb)
 {
 {
   TaggedString *ts;
   TaggedString *ts;
   unsigned long h = hash(buff, tag);
   unsigned long h = hash(buff, tag);
+  int size = tb->size;
   int i;
   int i;
   int j = -1;
   int j = -1;
-  if ((long)tb->nuse*3 >= (long)tb->size*2)
+  if ((long)tb->nuse*3 >= (long)size*2) {
     grow(tb);
     grow(tb);
-  for (i = h%tb->size; (ts = tb->hash[i]) != NULL; i = (i+1)%tb->size) {
+    size = tb->size;
+  }
+  for (i = h%size; (ts = tb->hash[i]) != NULL; ) {
     if (ts == &EMPTY)
     if (ts == &EMPTY)
       j = i;
       j = i;
     else if ((ts->constindex >= 0) ?  /* is a string? */
     else if ((ts->constindex >= 0) ?  /* is a string? */
               (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
               (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
               ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v))
               ((tag == ts->u.d.tag || tag == LUA_ANYTAG) && buff == ts->u.d.v))
       return ts;
       return ts;
+    if (++i == size) i=0;
   }
   }
   /* not found */
   /* not found */
   if (j != -1)  /* is there an EMPTY space? */
   if (j != -1)  /* is there an EMPTY space? */

+ 3 - 2
ltable.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ltable.c,v 1.9 1997/12/15 16:17:20 roberto Exp roberto $
+** $Id: ltable.c,v 1.10 1998/01/09 14:44:55 roberto Exp roberto $
 ** Lua tables (hash)
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -65,7 +65,8 @@ static int present (Hash *t, TObject *key)
   if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
   if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) {
     int h2 = h%(tsize-2) + 1;
     int h2 = h%(tsize-2) + 1;
     do {
     do {
-      h1 = (h1+h2)%tsize;
+      h1 += h2;
+      if (h1 >= tsize) h1 -= tsize;
       rf = ref(node(t, h1));
       rf = ref(node(t, h1));
     } while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
     } while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf));
   }
   }