diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-25 13:55:09 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-25 13:55:09 -0300 |
| commit | 534c3a64d3b47585b415f229aa03af35f9a4796e (patch) | |
| tree | bda9de835dbeff8b73ed5f0977e81dc9573ad046 | |
| parent | b9c98cd4d97774d703a48fefd56c66f552e0cd83 (diff) | |
| download | lua-534c3a64d3b47585b415f229aa03af35f9a4796e.tar.gz lua-534c3a64d3b47585b415f229aa03af35f9a4796e.tar.bz2 lua-534c3a64d3b47585b415f229aa03af35f9a4796e.zip | |
small optimizations for table access
| -rw-r--r-- | lbuiltin.c | 24 | ||||
| -rw-r--r-- | lobject.c | 5 | ||||
| -rw-r--r-- | lobject.h | 5 | ||||
| -rw-r--r-- | ltable.c | 48 | ||||
| -rw-r--r-- | ltable.h | 6 |
5 files changed, 57 insertions, 31 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.105 2000/04/14 17:44:20 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.106 2000/04/17 19:23:12 roberto Exp roberto $ |
| 3 | ** Built-in functions | 3 | ** Built-in functions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -321,7 +321,7 @@ void luaB_call (lua_State *L) { | |||
| 321 | /* push arg[1...n] */ | 321 | /* push arg[1...n] */ |
| 322 | luaD_checkstack(L, narg); | 322 | luaD_checkstack(L, narg); |
| 323 | for (i=0; i<narg; i++) | 323 | for (i=0; i<narg; i++) |
| 324 | *(L->top++) = *luaH_getint(L, arg, i+1); | 324 | *(L->top++) = *luaH_getnum(arg, i+1); |
| 325 | status = lua_callfunction(L, f); | 325 | status = lua_callfunction(L, f); |
| 326 | if (err != LUA_NOOBJECT) { /* restore old error method */ | 326 | if (err != LUA_NOOBJECT) { /* restore old error method */ |
| 327 | lua_pushobject(L, err); | 327 | lua_pushobject(L, err); |
| @@ -434,7 +434,7 @@ void luaB_foreachi (lua_State *L) { | |||
| 434 | for (i=1; i<=n; i++) { | 434 | for (i=1; i<=n; i++) { |
| 435 | *(L->top++) = *f; | 435 | *(L->top++) = *f; |
| 436 | ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; | 436 | ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; |
| 437 | *(L->top++) = *luaH_getint(L, t, i); | 437 | *(L->top++) = *luaH_getnum(t, i); |
| 438 | luaD_call(L, L->top-3, 1); | 438 | luaD_call(L, L->top-3, 1); |
| 439 | if (ttype(L->top-1) != TAG_NIL) | 439 | if (ttype(L->top-1) != TAG_NIL) |
| 440 | return; | 440 | return; |
| @@ -513,7 +513,7 @@ void luaB_tremove (lua_State *L) { | |||
| 513 | int n = (int)getnarg(L, a); | 513 | int n = (int)getnarg(L, a); |
| 514 | int pos = luaL_opt_int(L, 2, n); | 514 | int pos = luaL_opt_int(L, 2, n); |
| 515 | if (n <= 0) return; /* table is "empty" */ | 515 | if (n <= 0) return; /* table is "empty" */ |
| 516 | luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */ | 516 | luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */ |
| 517 | for ( ;pos<n; pos++) | 517 | for ( ;pos<n; pos++) |
| 518 | luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */ | 518 | luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */ |
| 519 | luaV_setn(L, a, n-1); /* a.n = n-1 */ | 519 | luaV_setn(L, a, n-1); /* a.n = n-1 */ |
| @@ -529,7 +529,7 @@ void luaB_tremove (lua_State *L) { | |||
| 529 | 529 | ||
| 530 | static void swap (lua_State *L, Hash *a, int i, int j) { | 530 | static void swap (lua_State *L, Hash *a, int i, int j) { |
| 531 | TObject temp; | 531 | TObject temp; |
| 532 | temp = *luaH_getint(L, a, i); | 532 | temp = *luaH_getnum(a, i); |
| 533 | luaH_move(L, a, j, i); | 533 | luaH_move(L, a, j, i); |
| 534 | luaH_setint(L, a, j, &temp); | 534 | luaH_setint(L, a, j, &temp); |
| 535 | } | 535 | } |
| @@ -556,26 +556,26 @@ static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) { | |||
| 556 | while (l < u) { /* for tail recursion */ | 556 | while (l < u) { /* for tail recursion */ |
| 557 | int i, j; | 557 | int i, j; |
| 558 | /* sort elements a[l], a[(l+u)/2] and a[u] */ | 558 | /* sort elements a[l], a[(l+u)/2] and a[u] */ |
| 559 | if (sort_comp(L, f, luaH_getint(L, a, u), luaH_getint(L, a, l))) | 559 | if (sort_comp(L, f, luaH_getnum(a, u), luaH_getnum(a, l))) |
| 560 | swap(L, a, l, u); /* a[u]<a[l] */ | 560 | swap(L, a, l, u); /* a[u]<a[l] */ |
| 561 | if (u-l == 1) break; /* only 2 elements */ | 561 | if (u-l == 1) break; /* only 2 elements */ |
| 562 | i = (l+u)/2; | 562 | i = (l+u)/2; |
| 563 | *P = *luaH_getint(L, a, i); /* P = a[i] */ | 563 | *P = *luaH_getnum(a, i); /* P = a[i] */ |
| 564 | if (sort_comp(L, f, P, luaH_getint(L, a, l))) /* a[i]<a[l]? */ | 564 | if (sort_comp(L, f, P, luaH_getnum(a, l))) /* a[i]<a[l]? */ |
| 565 | swap(L, a, l, i); | 565 | swap(L, a, l, i); |
| 566 | else if (sort_comp(L, f, luaH_getint(L, a, u), P)) /* a[u]<a[i]? */ | 566 | else if (sort_comp(L, f, luaH_getnum(a, u), P)) /* a[u]<a[i]? */ |
| 567 | swap(L, a, i, u); | 567 | swap(L, a, i, u); |
| 568 | if (u-l == 2) break; /* only 3 elements */ | 568 | if (u-l == 2) break; /* only 3 elements */ |
| 569 | *P = *luaH_getint(L, a, i); /* save pivot on stack (GC) */ | 569 | *P = *luaH_getnum(a, i); /* save pivot on stack (GC) */ |
| 570 | swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */ | 570 | swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */ |
| 571 | /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */ | 571 | /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */ |
| 572 | i = l; j = u-1; | 572 | i = l; j = u-1; |
| 573 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ | 573 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ |
| 574 | /* repeat i++ until a[i] >= P */ | 574 | /* repeat i++ until a[i] >= P */ |
| 575 | while (sort_comp(L, f, luaH_getint(L, a, ++i), P)) | 575 | while (sort_comp(L, f, luaH_getnum(a, ++i), P)) |
| 576 | if (i>u) lua_error(L, "invalid order function for sorting"); | 576 | if (i>u) lua_error(L, "invalid order function for sorting"); |
| 577 | /* repeat j-- until a[j] <= P */ | 577 | /* repeat j-- until a[j] <= P */ |
| 578 | while (sort_comp(L, f, P, luaH_getint(L, a, --j))) | 578 | while (sort_comp(L, f, P, luaH_getnum(a, --j))) |
| 579 | if (j<l) lua_error(L, "invalid order function for sorting"); | 579 | if (j<l) lua_error(L, "invalid order function for sorting"); |
| 580 | if (j<i) break; | 580 | if (j<i) break; |
| 581 | swap(L, a, i, j); | 581 | swap(L, a, i, j); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 1.35 2000/03/29 20:19:20 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 1.36 2000/03/31 16:28:45 roberto Exp roberto $ |
| 3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -32,7 +32,8 @@ unsigned long luaO_power2 (unsigned long n) { | |||
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | int luaO_equalval (const TObject *t1, const TObject *t2) { | 35 | int luaO_equalObj (const TObject *t1, const TObject *t2) { |
| 36 | if (ttype(t1) != ttype(t2)) return 0; | ||
| 36 | switch (ttype(t1)) { | 37 | switch (ttype(t1)) { |
| 37 | case TAG_NUMBER: | 38 | case TAG_NUMBER: |
| 38 | return nvalue(t1) == nvalue(t2); | 39 | return nvalue(t1) == nvalue(t2); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 1.59 2000/03/31 16:28:45 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 1.60 2000/04/10 19:20:24 roberto Exp roberto $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -180,8 +180,7 @@ extern const TObject luaO_nilobject; | |||
| 180 | 180 | ||
| 181 | unsigned long luaO_power2 (unsigned long n); | 181 | unsigned long luaO_power2 (unsigned long n); |
| 182 | 182 | ||
| 183 | #define luaO_equalObj(t1,t2) (ttype(t1) == ttype(t2) && luaO_equalval(t1,t2)) | 183 | int luaO_equalObj (const TObject *t1, const TObject *t2); |
| 184 | int luaO_equalval (const TObject *t1, const TObject *t2); | ||
| 185 | int luaO_redimension (lua_State *L, int oldsize); | 184 | int luaO_redimension (lua_State *L, int oldsize); |
| 186 | int luaO_str2d (const char *s, Number *result); | 185 | int luaO_str2d (const char *s, Number *result); |
| 187 | 186 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.39 2000/03/31 16:28: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 | */ |
| @@ -60,11 +60,12 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) { | |||
| 60 | } | 60 | } |
| 61 | LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)), | 61 | LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)), |
| 62 | "a&(x-1) == a%x, for x power of 2"); | 62 | "a&(x-1) == a%x, for x power of 2"); |
| 63 | return &t->node[h&((unsigned int)t->size-1)]; | 63 | return &t->node[h&(t->size-1)]; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | 66 | ||
| 67 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { | 67 | static const TObject *luaH_getany (lua_State *L, const Hash *t, |
| 68 | const TObject *key) { | ||
| 68 | Node *n = luaH_mainposition(t, key); | 69 | Node *n = luaH_mainposition(t, key); |
| 69 | if (!n) | 70 | if (!n) |
| 70 | lua_error(L, "unexpected type to index table"); | 71 | lua_error(L, "unexpected type to index table"); |
| @@ -77,6 +78,39 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { | |||
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | 80 | ||
| 81 | /* specialized version for numbers */ | ||
| 82 | const TObject *luaH_getnum (const Hash *t, Number key) { | ||
| 83 | Node *n = &t->node[(unsigned long)(long)key&(t->size-1)]; | ||
| 84 | do { | ||
| 85 | if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key) | ||
| 86 | return &n->val; | ||
| 87 | n = n->next; | ||
| 88 | } while (n); | ||
| 89 | return &luaO_nilobject; /* key not found */ | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | /* specialized version for strings */ | ||
| 94 | static const TObject *luaH_getstr (const Hash *t, TString *key) { | ||
| 95 | Node *n = &t->node[key->hash&(t->size-1)]; | ||
| 96 | do { | ||
| 97 | if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key) | ||
| 98 | return &n->val; | ||
| 99 | n = n->next; | ||
| 100 | } while (n); | ||
| 101 | return &luaO_nilobject; /* key not found */ | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { | ||
| 106 | switch (ttype(key)) { | ||
| 107 | case TAG_NUMBER: return luaH_getnum(t, nvalue(key)); | ||
| 108 | case TAG_STRING: return luaH_getstr(t, tsvalue(key)); | ||
| 109 | default: return luaH_getany(L, t, key); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 80 | int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { | 114 | int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { |
| 81 | const TObject *v = luaH_get(L, t, key); | 115 | const TObject *v = luaH_get(L, t, key); |
| 82 | return (v == &luaO_nilobject) ? -1 : /* key not found */ | 116 | return (v == &luaO_nilobject) ? -1 : /* key not found */ |
| @@ -214,11 +248,3 @@ void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) { | |||
| 214 | luaH_set(L, t, &index, val); | 248 | luaH_set(L, t, &index, val); |
| 215 | } | 249 | } |
| 216 | 250 | ||
| 217 | |||
| 218 | const TObject *luaH_getint (lua_State *L, const Hash *t, int key) { | ||
| 219 | TObject index; | ||
| 220 | ttype(&index) = TAG_NUMBER; | ||
| 221 | nvalue(&index) = key; | ||
| 222 | return luaH_get(L, t, &index); | ||
| 223 | } | ||
| 224 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.h,v 1.17 1999/11/23 13:58:02 roberto Exp roberto $ | 2 | ** $Id: ltable.h,v 1.18 1999/12/07 12:05:34 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 | */ |
| @@ -14,7 +14,7 @@ | |||
| 14 | #define key(n) (&(n)->key) | 14 | #define key(n) (&(n)->key) |
| 15 | #define val(n) (&(n)->val) | 15 | #define val(n) (&(n)->val) |
| 16 | 16 | ||
| 17 | #define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getint(L, t, from))) | 17 | #define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getnum(t, from))) |
| 18 | 18 | ||
| 19 | Hash *luaH_new (lua_State *L, int nhash); | 19 | Hash *luaH_new (lua_State *L, int nhash); |
| 20 | void luaH_free (lua_State *L, Hash *t); | 20 | void luaH_free (lua_State *L, Hash *t); |
| @@ -22,7 +22,7 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); | |||
| 22 | void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); | 22 | void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); |
| 23 | int luaH_pos (lua_State *L, const Hash *t, const TObject *r); | 23 | int luaH_pos (lua_State *L, const Hash *t, const TObject *r); |
| 24 | void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); | 24 | void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); |
| 25 | const TObject *luaH_getint (lua_State *L, const Hash *t, int key); | 25 | const TObject *luaH_getnum (const Hash *t, Number key); |
| 26 | unsigned long luaH_hash (lua_State *L, const TObject *key); | 26 | unsigned long luaH_hash (lua_State *L, const TObject *key); |
| 27 | 27 | ||
| 28 | /* exported only for debugging */ | 28 | /* exported only for debugging */ |
