diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-10-06 15:34:16 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-10-06 15:34:16 -0300 |
| commit | bd38017ddfeda738f3f1881043c0ec8bbea1adbc (patch) | |
| tree | 4fd935dbf16599661abc2d0c6be8ca8be083b678 /ltable.c | |
| parent | 652f885c30e36c3bdbecd71b70dfbf273abfe24e (diff) | |
| download | lua-bd38017ddfeda738f3f1881043c0ec8bbea1adbc.tar.gz lua-bd38017ddfeda738f3f1881043c0ec8bbea1adbc.tar.bz2 lua-bd38017ddfeda738f3f1881043c0ec8bbea1adbc.zip | |
small optimization for table size in machines with double allignment
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 38 |
1 files changed, 20 insertions, 18 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 2.5 2004/08/31 17:57:33 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.6 2004/09/27 18:54:45 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 | */ |
| @@ -152,7 +152,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { | |||
| 152 | } | 152 | } |
| 153 | for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ | 153 | for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ |
| 154 | if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ | 154 | if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ |
| 155 | setobj2s(L, key, gkey(gnode(t, i))); | 155 | setobj2s(L, key, key2tval(gnode(t, i))); |
| 156 | setobj2s(L, key+1, gval(gnode(t, i))); | 156 | setobj2s(L, key+1, gval(gnode(t, i))); |
| 157 | return 1; | 157 | return 1; |
| 158 | } | 158 | } |
| @@ -215,7 +215,7 @@ static void numuse (const Table *t, int *narray, int *nhash) { | |||
| 215 | while (i--) { | 215 | while (i--) { |
| 216 | Node *n = &t->node[i]; | 216 | Node *n = &t->node[i]; |
| 217 | if (!ttisnil(gval(n))) { | 217 | if (!ttisnil(gval(n))) { |
| 218 | int k = arrayindex(gkey(n)); | 218 | int k = arrayindex(key2tval(n)); |
| 219 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ | 219 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ |
| 220 | nums[luaO_log2(k-1)+1]++; /* count as such */ | 220 | nums[luaO_log2(k-1)+1]++; /* count as such */ |
| 221 | (*narray)++; | 221 | (*narray)++; |
| @@ -245,12 +245,12 @@ static void setnodevector (lua_State *L, Table *t, int lsize) { | |||
| 245 | t->node = G(L)->dummynode; /* use common `dummynode' */ | 245 | t->node = G(L)->dummynode; /* use common `dummynode' */ |
| 246 | lua_assert(ttisnil(gkey(t->node))); /* assert invariants: */ | 246 | lua_assert(ttisnil(gkey(t->node))); /* assert invariants: */ |
| 247 | lua_assert(ttisnil(gval(t->node))); | 247 | lua_assert(ttisnil(gval(t->node))); |
| 248 | lua_assert(t->node->next == NULL); /* (`dummynode' must be empty) */ | 248 | lua_assert(gnext(t->node) == NULL); /* (`dummynode' must be empty) */ |
| 249 | } | 249 | } |
| 250 | else { | 250 | else { |
| 251 | t->node = luaM_newvector(L, size, Node); | 251 | t->node = luaM_newvector(L, size, Node); |
| 252 | for (i=0; i<size; i++) { | 252 | for (i=0; i<size; i++) { |
| 253 | t->node[i].next = NULL; | 253 | gnext(&t->node[i]) = NULL; |
| 254 | setnilvalue(gkey(gnode(t, i))); | 254 | setnilvalue(gkey(gnode(t, i))); |
| 255 | setnilvalue(gval(gnode(t, i))); | 255 | setnilvalue(gval(gnode(t, i))); |
| 256 | } | 256 | } |
| @@ -274,7 +274,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
| 274 | nold = temp; | 274 | nold = temp; |
| 275 | setnilvalue(gkey(G(L)->dummynode)); /* restate invariant */ | 275 | setnilvalue(gkey(G(L)->dummynode)); /* restate invariant */ |
| 276 | setnilvalue(gval(G(L)->dummynode)); | 276 | setnilvalue(gval(G(L)->dummynode)); |
| 277 | lua_assert(G(L)->dummynode->next == NULL); | 277 | lua_assert(gnext(G(L)->dummynode) == NULL); |
| 278 | } | 278 | } |
| 279 | if (nasize > oldasize) /* array part must grow? */ | 279 | if (nasize > oldasize) /* array part must grow? */ |
| 280 | setarrayvector(L, t, nasize); | 280 | setarrayvector(L, t, nasize); |
| @@ -295,7 +295,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { | |||
| 295 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { | 295 | for (i = twoto(oldhsize) - 1; i >= 0; i--) { |
| 296 | Node *old = nold+i; | 296 | Node *old = nold+i; |
| 297 | if (!ttisnil(gval(old))) | 297 | if (!ttisnil(gval(old))) |
| 298 | setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old)); | 298 | setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); |
| 299 | } | 299 | } |
| 300 | if (oldhsize) | 300 | if (oldhsize) |
| 301 | luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ | 301 | luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ |
| @@ -351,24 +351,25 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | |||
| 351 | TValue *val; | 351 | TValue *val; |
| 352 | Node *mp = luaH_mainposition(t, key); | 352 | Node *mp = luaH_mainposition(t, key); |
| 353 | if (!ttisnil(gval(mp))) { /* main position is not free? */ | 353 | if (!ttisnil(gval(mp))) { /* main position is not free? */ |
| 354 | Node *othern = luaH_mainposition(t, gkey(mp)); /* `mp' of colliding node */ | 354 | /* `mp' of colliding node */ |
| 355 | Node *othern = luaH_mainposition(t, key2tval(mp)); | ||
| 355 | Node *n = t->firstfree; /* get a free place */ | 356 | Node *n = t->firstfree; /* get a free place */ |
| 356 | if (othern != mp) { /* is colliding node out of its main position? */ | 357 | if (othern != mp) { /* is colliding node out of its main position? */ |
| 357 | /* yes; move colliding node into free position */ | 358 | /* yes; move colliding node into free position */ |
| 358 | while (othern->next != mp) othern = othern->next; /* find previous */ | 359 | while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ |
| 359 | othern->next = n; /* redo the chain with `n' in place of `mp' */ | 360 | gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ |
| 360 | *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ | 361 | *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ |
| 361 | mp->next = NULL; /* now `mp' is free */ | 362 | gnext(mp) = NULL; /* now `mp' is free */ |
| 362 | setnilvalue(gval(mp)); | 363 | setnilvalue(gval(mp)); |
| 363 | } | 364 | } |
| 364 | else { /* colliding node is in its own main position */ | 365 | else { /* colliding node is in its own main position */ |
| 365 | /* new node will go into free position */ | 366 | /* new node will go into free position */ |
| 366 | n->next = mp->next; /* chain new position */ | 367 | gnext(n) = gnext(mp); /* chain new position */ |
| 367 | mp->next = n; | 368 | gnext(mp) = n; |
| 368 | mp = n; | 369 | mp = n; |
| 369 | } | 370 | } |
| 370 | } | 371 | } |
| 371 | setobj2t(L, gkey(mp), key); | 372 | gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; |
| 372 | luaC_barriert(L, t, key); | 373 | luaC_barriert(L, t, key); |
| 373 | lua_assert(ttisnil(gval(mp))); | 374 | lua_assert(ttisnil(gval(mp))); |
| 374 | for (;;) { /* correct `firstfree' */ | 375 | for (;;) { /* correct `firstfree' */ |
| @@ -400,7 +401,7 @@ const TValue *luaH_getnum (Table *t, int key) { | |||
| 400 | do { /* check whether `key' is somewhere in the chain */ | 401 | do { /* check whether `key' is somewhere in the chain */ |
| 401 | if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == nk) | 402 | if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == nk) |
| 402 | return gval(n); /* that's it */ | 403 | return gval(n); /* that's it */ |
| 403 | else n = n->next; | 404 | else n = gnext(n); |
| 404 | } while (n); | 405 | } while (n); |
| 405 | return &luaO_nilobject; | 406 | return &luaO_nilobject; |
| 406 | } | 407 | } |
| @@ -415,7 +416,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { | |||
| 415 | do { /* check whether `key' is somewhere in the chain */ | 416 | do { /* check whether `key' is somewhere in the chain */ |
| 416 | if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) | 417 | if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) |
| 417 | return gval(n); /* that's it */ | 418 | return gval(n); /* that's it */ |
| 418 | else n = n->next; | 419 | else n = gnext(n); |
| 419 | } while (n); | 420 | } while (n); |
| 420 | return &luaO_nilobject; | 421 | return &luaO_nilobject; |
| 421 | } | 422 | } |
| @@ -438,8 +439,9 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
| 438 | default: { | 439 | default: { |
| 439 | Node *n = luaH_mainposition(t, key); | 440 | Node *n = luaH_mainposition(t, key); |
| 440 | do { /* check whether `key' is somewhere in the chain */ | 441 | do { /* check whether `key' is somewhere in the chain */ |
| 441 | if (luaO_rawequalObj(gkey(n), key)) return gval(n); /* that's it */ | 442 | if (luaO_rawequalObj(key2tval(n), key)) |
| 442 | else n = n->next; | 443 | return gval(n); /* that's it */ |
| 444 | else n = gnext(n); | ||
| 443 | } while (n); | 445 | } while (n); |
| 444 | return &luaO_nilobject; | 446 | return &luaO_nilobject; |
| 445 | } | 447 | } |
