소스 검색

small bug: nuse may change when table is rehashed;
3/2 is a good fraction for hash limit (instead of 0.7, using floats)

Roberto Ierusalimschy 27 년 전
부모
커밋
8e226e6a09
1개의 변경된 파일10개의 추가작업 그리고 11개의 파일을 삭제
  1. 10 11
      ltable.c

+ 10 - 11
ltable.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.c,v 1.13 1998/07/12 16:15:19 roberto Exp roberto $
+** $Id: ltable.c,v 1.14 1998/08/10 21:36:32 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -20,8 +20,6 @@
 #define nodevector(t)	((t)->node)
 
 
-#define REHASH_LIMIT    0.70    /* avoid more than this % full */
-
 #define TagDefault LUA_T_ARRAY;
 
 
@@ -107,10 +105,9 @@ void luaH_free (Hash *frees)
 }
 
 
-Hash *luaH_new (int nhash)
-{
+Hash *luaH_new (int nhash) {
   Hash *t = luaM_new(Hash);
-  nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT));
+  nhash = luaO_redimension(nhash*3/2);
   nodevector(t) = hashnodecreate(nhash);
   nhash(t) = nhash;
   nuse(t) = 0;
@@ -133,18 +130,20 @@ static int newsize (Hash *t) {
   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);
   Node *vold = nodevector(t);
   int nnew = newsize(t);
   int i;
   nodevector(t) = hashnodecreate(nnew);
   nhash(t) = nnew;
+  nuse(t) = 0;
   for (i=0; i<nold; 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 */
+      nuse(t)++;
+    }
   }
   L->nblocks += gcsize(nnew)-gcsize(nold);
   luaM_free(vold);
@@ -170,11 +169,11 @@ TObject *luaH_set (Hash *t, TObject *ref)
 {
   Node *n = node(t, present(t, ref));
   if (ttype(ref(n)) == LUA_T_NIL) {
-    nuse(t)++;
-    if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
+    if ((long)nuse(t)*3L > (long)nhash(t)*2L) {
       rehash(t);
       n = node(t, present(t, ref));
     }
+    nuse(t)++;
     *ref(n) = *ref;
     ttype(val(n)) = LUA_T_NIL;
   }