From 5378331f2df1ba5bc3a4dc38f7b3ae53c3f2cbef Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Aug 1998 18:36:32 -0300 Subject: hash tables may shrink if use rate is too small. --- lstring.c | 10 +++------- ltable.c | 10 +++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/lstring.c b/lstring.c index 11303877..80103a28 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 1.13 1998/06/19 16:14:09 roberto Exp roberto $ +** $Id: lstring.c,v 1.14 1998/07/27 17:06:17 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -45,8 +45,7 @@ static unsigned long hash_s (char *s, long l) return h; } -static int newsize (stringtable *tb) -{ +static int newsize (stringtable *tb) { int size = tb->size; int realuse = 0; int i; @@ -54,10 +53,7 @@ static int newsize (stringtable *tb) for (i=0; ihash[i] != NULL && tb->hash[i] != &EMPTY) realuse++; - if (2*(realuse+1) <= size) /* +1 is the new element */ - return size; /* don't need to grow, just rehash to clear EMPTYs */ - else - return luaO_redimension(size); + return luaO_redimension((realuse+1)*2); /* +1 is the new element */ } diff --git a/ltable.c b/ltable.c index 91556b78..ac0c6a94 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.12 1998/01/28 16:50:33 roberto Exp roberto $ +** $Id: ltable.c,v 1.13 1998/07/12 16:15:19 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -121,8 +121,7 @@ Hash *luaH_new (int nhash) } -static int newsize (Hash *t) -{ +static int newsize (Hash *t) { Node *v = t->node; int size = nhash(t); int realuse = 0; @@ -131,10 +130,7 @@ static int newsize (Hash *t) if (ttype(ref(v+i)) != LUA_T_NIL && ttype(val(v+i)) != LUA_T_NIL) realuse++; } - if (2*(realuse+1) <= size) /* +1 is the new element */ - return size; /* don't need to grow, just rehash */ - else - return luaO_redimension(size); + return luaO_redimension((realuse+1)*2); /* +1 is the new element */ } static void rehash (Hash *t) -- cgit v1.2.3-55-g6feb