From 534c3a64d3b47585b415f229aa03af35f9a4796e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 25 Apr 2000 13:55:09 -0300 Subject: small optimizations for table access --- lbuiltin.c | 24 ++++++++++++------------ lobject.c | 5 +++-- lobject.h | 5 ++--- ltable.c | 48 +++++++++++++++++++++++++++++++++++++----------- ltable.h | 6 +++--- 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/lbuiltin.c b/lbuiltin.c index 502c70c8..d57f9fa2 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.105 2000/04/14 17:44:20 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.106 2000/04/17 19:23:12 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -321,7 +321,7 @@ void luaB_call (lua_State *L) { /* push arg[1...n] */ luaD_checkstack(L, narg); for (i=0; itop++) = *luaH_getint(L, arg, i+1); + *(L->top++) = *luaH_getnum(arg, i+1); status = lua_callfunction(L, f); if (err != LUA_NOOBJECT) { /* restore old error method */ lua_pushobject(L, err); @@ -434,7 +434,7 @@ void luaB_foreachi (lua_State *L) { for (i=1; i<=n; i++) { *(L->top++) = *f; ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; - *(L->top++) = *luaH_getint(L, t, i); + *(L->top++) = *luaH_getnum(t, i); luaD_call(L, L->top-3, 1); if (ttype(L->top-1) != TAG_NIL) return; @@ -513,7 +513,7 @@ void luaB_tremove (lua_State *L) { int n = (int)getnarg(L, a); int pos = luaL_opt_int(L, 2, n); if (n <= 0) return; /* table is "empty" */ - luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */ + luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */ for ( ;pos= P */ - while (sort_comp(L, f, luaH_getint(L, a, ++i), P)) + while (sort_comp(L, f, luaH_getnum(a, ++i), P)) if (i>u) lua_error(L, "invalid order function for sorting"); /* repeat j-- until a[j] <= P */ - while (sort_comp(L, f, P, luaH_getint(L, a, --j))) + while (sort_comp(L, f, P, luaH_getnum(a, --j))) if (jsize == (h&((unsigned int)t->size-1)), "a&(x-1) == a%x, for x power of 2"); - return &t->node[h&((unsigned int)t->size-1)]; + return &t->node[h&(t->size-1)]; } -const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { +static const TObject *luaH_getany (lua_State *L, const Hash *t, + const TObject *key) { Node *n = luaH_mainposition(t, key); if (!n) 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) { } +/* specialized version for numbers */ +const TObject *luaH_getnum (const Hash *t, Number key) { + Node *n = &t->node[(unsigned long)(long)key&(t->size-1)]; + do { + if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key) + return &n->val; + n = n->next; + } while (n); + return &luaO_nilobject; /* key not found */ +} + + +/* specialized version for strings */ +static const TObject *luaH_getstr (const Hash *t, TString *key) { + Node *n = &t->node[key->hash&(t->size-1)]; + do { + if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key) + return &n->val; + n = n->next; + } while (n); + return &luaO_nilobject; /* key not found */ +} + + +const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { + switch (ttype(key)) { + case TAG_NUMBER: return luaH_getnum(t, nvalue(key)); + case TAG_STRING: return luaH_getstr(t, tsvalue(key)); + default: return luaH_getany(L, t, key); + } +} + + int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { const TObject *v = luaH_get(L, t, key); 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) { luaH_set(L, t, &index, val); } - -const TObject *luaH_getint (lua_State *L, const Hash *t, int key) { - TObject index; - ttype(&index) = TAG_NUMBER; - nvalue(&index) = key; - return luaH_get(L, t, &index); -} - diff --git a/ltable.h b/ltable.h index e678533e..9a5fe9a5 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 1.17 1999/11/23 13:58:02 roberto Exp roberto $ +** $Id: ltable.h,v 1.18 1999/12/07 12:05:34 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -14,7 +14,7 @@ #define key(n) (&(n)->key) #define val(n) (&(n)->val) -#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getint(L, t, from))) +#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getnum(t, from))) Hash *luaH_new (lua_State *L, int nhash); 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); void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); int luaH_pos (lua_State *L, const Hash *t, const TObject *r); void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); -const TObject *luaH_getint (lua_State *L, const Hash *t, int key); +const TObject *luaH_getnum (const Hash *t, Number key); unsigned long luaH_hash (lua_State *L, const TObject *key); /* exported only for debugging */ -- cgit v1.2.3-55-g6feb