summaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-04-25 13:55:09 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-04-25 13:55:09 -0300
commit534c3a64d3b47585b415f229aa03af35f9a4796e (patch)
treebda9de835dbeff8b73ed5f0977e81dc9573ad046 /ltable.c
parentb9c98cd4d97774d703a48fefd56c66f552e0cd83 (diff)
downloadlua-github-534c3a64d3b47585b415f229aa03af35f9a4796e.tar.gz
small optimizations for table access
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c48
1 files changed, 37 insertions, 11 deletions
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);
-}
-