summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-05-13 10:38:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-05-13 10:38:59 -0300
commit58bf77bc7f37d697c5bfc33be169c6b841dd8a1d (patch)
tree67ed9dd2a677408b50c49855ff95e84bc64b61e8
parent8da6fe62d81b74086e1d3bd59b9c4e3b1067714b (diff)
downloadlua-58bf77bc7f37d697c5bfc33be169c6b841dd8a1d.tar.gz
lua-58bf77bc7f37d697c5bfc33be169c6b841dd8a1d.tar.bz2
lua-58bf77bc7f37d697c5bfc33be169c6b841dd8a1d.zip
no more extra space when growing hash
-rw-r--r--ltable.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/ltable.c b/ltable.c
index 64a72f53..546e3527 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.105 2002/04/23 15:04:39 roberto Exp roberto $ 2** $Id: ltable.c,v 1.106 2002/05/08 17:34:00 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -244,12 +244,13 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
244 nold = t->node; /* save old hash ... */ 244 nold = t->node; /* save old hash ... */
245 else { /* old hash is `dummynode' */ 245 else { /* old hash is `dummynode' */
246 lua_assert(t->node == G(L)->dummynode); 246 lua_assert(t->node == G(L)->dummynode);
247 temp[0] = t->node[0]; /* copy it to `temp' (in case of errors) */ 247 temp[0] = t->node[0]; /* copy it to `temp' */
248 nold = temp; 248 nold = temp;
249 setnilvalue(key(G(L)->dummynode)); /* restate invariant */ 249 setnilvalue(key(G(L)->dummynode)); /* restate invariant */
250 setnilvalue(val(G(L)->dummynode)); 250 setnilvalue(val(G(L)->dummynode));
251 lua_assert(G(L)->dummynode->next == NULL);
251 } 252 }
252 if (nasize > oldasize) /* should grow array part? */ 253 if (nasize > oldasize) /* array part must grow? */
253 setarrayvector(L, t, nasize); 254 setarrayvector(L, t, nasize);
254 /* create new hash part with appropriate size */ 255 /* create new hash part with appropriate size */
255 setnodevector(L, t, nhsize); 256 setnodevector(L, t, nhsize);
@@ -261,12 +262,11 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
261 if (ttype(&t->array[i]) != LUA_TNIL) 262 if (ttype(&t->array[i]) != LUA_TNIL)
262 luaH_setnum(L, t, i+1, &t->array[i]); 263 luaH_setnum(L, t, i+1, &t->array[i]);
263 } 264 }
264 /* shink array */ 265 /* shrink array */
265 luaM_reallocvector(L, t->array, oldasize, nasize, TObject); 266 luaM_reallocvector(L, t->array, oldasize, nasize, TObject);
266 } 267 }
267 /* re-insert elements in hash part */ 268 /* re-insert elements in hash part */
268 i = twoto(oldhsize); 269 for (i = twoto(oldhsize) - 1; i >= 0; i--) {
269 while (i--) {
270 Node *old = nold+i; 270 Node *old = nold+i;
271 if (ttype(val(old)) != LUA_TNIL) 271 if (ttype(val(old)) != LUA_TNIL)
272 luaH_set(L, t, key(old), val(old)); 272 luaH_set(L, t, key(old), val(old));
@@ -279,7 +279,6 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) {
279static void rehash (lua_State *L, Table *t) { 279static void rehash (lua_State *L, Table *t) {
280 int nasize, nhsize; 280 int nasize, nhsize;
281 numuse(t, &nasize, &nhsize); /* compute new sizes for array and hash parts */ 281 numuse(t, &nasize, &nhsize); /* compute new sizes for array and hash parts */
282 nhsize += nhsize/4; /* allow some extra for growing nhsize */
283 resize(L, t, nasize, luaO_log2(nhsize)+1); 282 resize(L, t, nasize, luaO_log2(nhsize)+1);
284} 283}
285 284