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 --- ltable.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index 299821ca..cc3a64f1 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $ +** $Id: ltable.c,v 1.39 2000/03/31 16:28:45 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -60,11 +60,12 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) { } LUA_ASSERT(L, h%(unsigned int)t->size == (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); -} - -- cgit v1.2.3-55-g6feb