From 933bead92e48cdd8f2c9b5953854270e98d58c9a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 22 Jan 1999 16:47:23 -0200 Subject: small optimizations(?) --- ltable.c | 85 +++++++++++++++++++++++++++------------------------------------- 1 file changed, 36 insertions(+), 49 deletions(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index c875d189..205ab574 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.16 1998/12/30 13:14:46 roberto Exp $ +** $Id: ltable.c,v 1.17 1999/01/04 12:54:33 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -24,8 +24,7 @@ -static long int hashindex (TObject *ref) -{ +static long int hashindex (TObject *ref) { long int h; switch (ttype(ref)) { case LUA_T_NUMBER: @@ -54,57 +53,46 @@ static long int hashindex (TObject *ref) } -static int present (Hash *t, TObject *key) -{ +static Node *present (Hash *t, TObject *key) { int tsize = nhash(t); long int h = hashindex(key); int h1 = h%tsize; - TObject *rf = ref(node(t, h1)); - if (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)) { + Node *n = node(t, h1); + /* keep looking until an entry with "ref" equal to key or nil */ + if ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n)) + : ttype(ref(n)) != LUA_T_NIL)) { int h2 = h%(tsize-2) + 1; do { h1 += h2; if (h1 >= tsize) h1 -= tsize; - rf = ref(node(t, h1)); - } while (ttype(rf) != LUA_T_NIL && !luaO_equalObj(key, rf)); + n = node(t, h1); + } while ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n)) + : ttype(ref(n)) != LUA_T_NIL)); } - return h1; + return n; } -/* -** Alloc a vector node -*/ -static Node *hashnodecreate (int nhash) -{ - Node *v = luaM_newvector(nhash, Node); - int i; - for (i=0; ihead.next; L->nblocks -= gcsize(frees->nhash); - hashdelete(frees); + luaM_free(nodevector(frees)); + luaM_free(frees); frees = next; } } +static Node *hashnodecreate (int nhash) { + Node *v = luaM_newvector(nhash, Node); + int i; + for (i=0; i (long)nhash(t)*2L) { rehash(t); - n = node(t, present(t, ref)); + n = present(t, ref); } nuse(t)++; *ref(n) = *ref; @@ -192,18 +180,17 @@ static Node *hashnext (Hash *t, int i) { return NULL; n = node(t, i); } - return node(t, i); + return n; } Node *luaH_next (Hash *t, TObject *r) { if (ttype(r) == LUA_T_NIL) return hashnext(t, 0); else { - int i = present(t, r); - Node *n = node(t, i); + Node *n = present(t, r); luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL, 2, "key not found"); - return hashnext(t, i+1); + return hashnext(t, (n-(t->node))+1); } } -- cgit v1.2.3-55-g6feb