too much optimization to "break" keys in tables; keep them as TObjects...

This commit is contained in:
Roberto Ierusalimschy 2001-06-26 10:20:45 -03:00
parent 9559c111a3
commit 37f3a1c045
8 changed files with 64 additions and 78 deletions

10
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 1.145 2001/06/15 19:16:41 roberto Exp roberto $ ** $Id: lapi.c,v 1.146 2001/06/15 20:36:57 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -672,7 +672,7 @@ LUA_API int lua_next (lua_State *L, int index) {
api_check(L, ttype(t) == LUA_TTABLE); api_check(L, ttype(t) == LUA_TTABLE);
n = luaH_next(L, hvalue(t), luaA_index(L, -1)); n = luaH_next(L, hvalue(t), luaA_index(L, -1));
if (n) { if (n) {
setkey2obj(L->top-1, n); setobj(L->top-1, key(n));
setobj(L->top, val(n)); setobj(L->top, val(n));
api_incr_top(L); api_incr_top(L);
more = 1; more = 1;
@ -701,10 +701,10 @@ LUA_API int lua_getn (lua_State *L, int index) {
int i = hvalue(t)->size; int i = hvalue(t)->size;
Node *nd = hvalue(t)->node; Node *nd = hvalue(t)->node;
while (i--) { while (i--) {
if (ttype_key(nd) == LUA_TNUMBER && if (ttype(key(nd)) == LUA_TNUMBER &&
ttype(val(nd)) != LUA_TNIL && ttype(val(nd)) != LUA_TNIL &&
nvalue_key(nd) > max) nvalue(key(nd)) > max)
max = nvalue_key(nd); max = nvalue(key(nd));
nd++; nd++;
} }
n = (int)max; n = (int)max;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 1.82 2001/06/11 14:56:42 roberto Exp roberto $ ** $Id: ldebug.c,v 1.83 2001/06/15 20:36:57 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -225,8 +225,8 @@ static const l_char *travglobals (lua_State *L, const TObject *o) {
int i; int i;
for (i=0; i<g->size; i++) { for (i=0; i<g->size; i++) {
if (luaO_equalObj(o, val(node(g, i))) && if (luaO_equalObj(o, val(node(g, i))) &&
ttype_key(node(g, i)) == LUA_TSTRING) ttype(key(node(g, i))) == LUA_TSTRING)
return getstr(tsvalue_key(node(g, i))); return getstr(tsvalue(key(node(g, i))));
} }
return NULL; return NULL;
} }

32
lgc.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 1.106 2001/06/15 20:36:57 roberto Exp roberto $ ** $Id: lgc.c,v 1.107 2001/06/21 16:41:34 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -128,8 +128,8 @@ static void traverseclosure (GCState *st, Closure *f) {
static void removekey (Node *n) { static void removekey (Node *n) {
lua_assert(ttype(val(n)) == LUA_TNIL); lua_assert(ttype(val(n)) == LUA_TNIL);
if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER) if (ttype(key(n)) != LUA_TNIL && ttype(key(n)) != LUA_TNUMBER)
ttype_key(n) = LUA_TNONE; /* dead key; remove it */ setttype(key(n), LUA_TNONE); /* dead key; remove it */
} }
@ -143,14 +143,11 @@ static void traversetable (GCState *st, Hash *h) {
if (ttype(val(n)) == LUA_TNIL) if (ttype(val(n)) == LUA_TNIL)
removekey(n); removekey(n);
else { else {
lua_assert(ttype_key(n) != LUA_TNIL); lua_assert(ttype(key(n)) != LUA_TNIL);
if (ttype_key(n) != LUA_TNUMBER && !(mode & LUA_WEAK_KEY)) { if (ttype(key(n)) != LUA_TNUMBER && !(mode & LUA_WEAK_KEY))
TObject k; markobject(st, key(n));
setkey2obj(&k, n);
markobject(st, &k);
}
if (!(mode & LUA_WEAK_VALUE)) if (!(mode & LUA_WEAK_VALUE))
markobject(st, &n->val); markobject(st, val(n));
} }
} }
} }
@ -181,16 +178,16 @@ static void markall (lua_State *L) {
} }
static int hasmark (int tt, Value *v) { static int hasmark (const TObject *o) {
switch (tt) { switch (ttype(o)) {
case LUA_TSTRING: case LUA_TSTRING:
return v->ts->tsv.marked; return tsvalue(o)->tsv.marked;
case LUA_TUSERDATA: case LUA_TUSERDATA:
return ismarkedudata(v->u); return ismarkedudata(uvalue(o));
case LUA_TTABLE: case LUA_TTABLE:
return ismarked(v->h); return ismarked(hvalue(o));
case LUA_TFUNCTION: case LUA_TFUNCTION:
return ismarked(v->cl); return ismarked(clvalue(o));
default: /* number, nil */ default: /* number, nil */
return 1; return 1;
} }
@ -202,8 +199,7 @@ static void cleardeadnodes (Hash *h) {
for (i=0; i<h->size; i++) { for (i=0; i<h->size; i++) {
Node *n = node(h, i); Node *n = node(h, i);
if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */ if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */
if (!hasmark(ttype(val(n)), &(val(n)->value)) || if (!hasmark(val(n)) || !hasmark(key(n))) {
!hasmark(ttype_key(n), &n->key_value)) {
setnilvalue(val(n)); /* remove value */ setnilvalue(val(n)); /* remove value */
removekey(n); removekey(n);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 1.105 2001/06/07 15:01:21 roberto Exp roberto $ ** $Id: lobject.h,v 1.106 2001/06/15 20:36:57 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -73,6 +73,9 @@ typedef struct lua_TObject {
{ TObject *o1=(obj1); const TObject *o2=(obj2); \ { TObject *o1=(obj1); const TObject *o2=(obj2); \
o1->tt=o2->tt; o1->value = o2->value; } o1->tt=o2->tt; o1->value = o2->value; }
#define setttype(obj, tt) (ttype(obj) = (tt))
typedef TObject *StkId; /* index to stack elements */ typedef TObject *StkId; /* index to stack elements */
@ -167,8 +170,7 @@ typedef struct Closure {
typedef struct Node { typedef struct Node {
struct Node *next; /* for chaining */ struct Node *next; /* for chaining */
int key_tt; /* (break object to save padding space) */ TObject key;
Value key_value;
TObject val; TObject val;
} Node; } Node;

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 1.80 2001/06/06 18:00:19 roberto Exp roberto $ ** $Id: ltable.c,v 1.81 2001/06/15 20:36:57 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -32,9 +32,9 @@
#define TagDefault LUA_TTABLE #define TagDefault LUA_TTABLE
#define hashnum(t,n) (&t->node[lmod((lu_hash)(ls_hash)(n), t->size)]) #define hashnum(t,n) (node(t, lmod((lu_hash)(ls_hash)(n), t->size)))
#define hashstr(t,str) (&t->node[lmod((str)->tsv.hash, t->size)]) #define hashstr(t,str) (node(t, lmod((str)->tsv.hash, t->size)))
#define hashpointer(t,p) (&t->node[lmod(IntPoint(p), t->size)]) #define hashpointer(t,p) (node(t, lmod(IntPoint(p), t->size)))
/* /*
@ -42,13 +42,13 @@
** of its hash value) ** of its hash value)
*/ */
Node *luaH_mainposition (const Hash *t, const Node *n) { Node *luaH_mainposition (const Hash *t, const Node *n) {
switch (ttype_key(n)) { switch (ttype(key(n))) {
case LUA_TNUMBER: case LUA_TNUMBER:
return hashnum(t, nvalue_key(n)); return hashnum(t, nvalue(key(n)));
case LUA_TSTRING: case LUA_TSTRING:
return hashstr(t, tsvalue_key(n)); return hashstr(t, tsvalue(key(n)));
default: /* all other types are hashed as (void *) */ default: /* all other types are hashed as (void *) */
return hashpointer(t, tsvalue_key(n)); return hashpointer(t, tsvalue(key(n)));
} }
} }
@ -62,7 +62,7 @@ Node *luaH_next (lua_State *L, Hash *t, const TObject *key) {
if (v == &luaO_nilobject) if (v == &luaO_nilobject)
luaD_error(L, l_s("invalid key for `next'")); luaD_error(L, l_s("invalid key for `next'"));
i = (int)(((const l_char *)v - i = (int)(((const l_char *)v -
(const l_char *)(&t->node[0].val)) / sizeof(Node)) + 1; (const l_char *)(val(node(t, 0)))) / sizeof(Node)) + 1;
} }
for (; i<t->size; i++) { for (; i<t->size; i++) {
Node *n = node(t, i); Node *n = node(t, i);
@ -103,11 +103,11 @@ static void setnodevector (lua_State *L, Hash *t, int size) {
t->node = luaM_newvector(L, size, Node); t->node = luaM_newvector(L, size, Node);
for (i=0; i<size; i++) { for (i=0; i<size; i++) {
t->node[i].next = NULL; t->node[i].next = NULL;
t->node[i].key_tt = LUA_TNIL; setnilvalue(key(node(t, i)));
setnilvalue(&t->node[i].val); setnilvalue(val(node(t, i)));
} }
t->size = size; t->size = size;
t->firstfree = &t->node[size-1]; /* first free position to be used */ t->firstfree = node(t, size-1); /* first free position to be used */
} }
@ -161,12 +161,9 @@ static void rehash (lua_State *L, Hash *t) {
setnodevector(L, t, oldsize); /* just rehash; keep the same size */ setnodevector(L, t, oldsize); /* just rehash; keep the same size */
for (i=0; i<oldsize; i++) { for (i=0; i<oldsize; i++) {
Node *old = nold+i; Node *old = nold+i;
if (ttype(&old->val) != LUA_TNIL) { if (ttype(val(old)) != LUA_TNIL) {
TObject o; TObject *v = luaH_set(L, t, key(old));
TObject *v; setobj(v, val(old));
setkey2obj(&o, old);
v = luaH_set(L, t, &o);
setobj(v, &old->val);
} }
} }
luaM_freearray(L, nold, oldsize, Node); /* free old array */ luaM_freearray(L, nold, oldsize, Node); /* free old array */
@ -181,7 +178,7 @@ static void rehash (lua_State *L, Hash *t) {
** position), new key goes to an empty position. ** position), new key goes to an empty position.
*/ */
static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) { static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
if (ttype(&mp->val) != LUA_TNIL) { /* main position is not free? */ if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */
Node *othern = luaH_mainposition(t, mp); /* `mp' of colliding node */ Node *othern = luaH_mainposition(t, mp); /* `mp' of colliding node */
Node *n = t->firstfree; /* get a free place */ Node *n = t->firstfree; /* get a free place */
if (othern != mp) { /* is colliding node out of its main position? */ if (othern != mp) { /* is colliding node out of its main position? */
@ -190,7 +187,7 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
othern->next = n; /* redo the chain with `n' in place of `mp' */ othern->next = n; /* redo the chain with `n' in place of `mp' */
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
mp->next = NULL; /* now `mp' is free */ mp->next = NULL; /* now `mp' is free */
setnilvalue(&mp->val); setnilvalue(val(mp));
} }
else { /* colliding node is in its own main position */ else { /* colliding node is in its own main position */
/* new node will go into free position */ /* new node will go into free position */
@ -199,11 +196,11 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) {
mp = n; mp = n;
} }
} }
setobj2key(mp, key); setobj(key(mp), key);
lua_assert(ttype(&mp->val) == LUA_TNIL); lua_assert(ttype(val(mp)) == LUA_TNIL);
for (;;) { /* correct `firstfree' */ for (;;) { /* correct `firstfree' */
if (ttype_key(t->firstfree) == LUA_TNIL) if (ttype(key(t->firstfree)) == LUA_TNIL)
return &mp->val; /* OK; table still has a free place */ return val(mp); /* OK; table still has a free place */
else if (t->firstfree == t->node) break; /* cannot decrement from here */ else if (t->firstfree == t->node) break; /* cannot decrement from here */
else (t->firstfree)--; else (t->firstfree)--;
} }
@ -220,8 +217,8 @@ TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) {
Node *mp = hashnum(t, key); Node *mp = hashnum(t, key);
Node *n = mp; Node *n = mp;
do { /* check whether `key' is somewhere in the chain */ do { /* check whether `key' is somewhere in the chain */
if (ttype_key(n) == LUA_TNUMBER && nvalue_key(n) == key) if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == key)
return &n->val; /* that's all */ return val(n); /* that's all */
else n = n->next; else n = n->next;
} while (n); } while (n);
if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */ if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
@ -239,8 +236,8 @@ TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
Node *mp = hashstr(t, key); Node *mp = hashstr(t, key);
Node *n = mp; Node *n = mp;
do { /* check whether `key' is somewhere in the chain */ do { /* check whether `key' is somewhere in the chain */
if (ttype_key(n) == LUA_TSTRING && tsvalue_key(n) == key) if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key)
return &n->val; /* that's all */ return val(n); /* that's all */
else n = n->next; else n = n->next;
} while (n); } while (n);
if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */ if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */
@ -258,8 +255,8 @@ static TObject *luaH_setany (lua_State *L, Hash *t, const TObject *key) {
Node *n = mp; Node *n = mp;
do { /* check whether `key' is somewhere in the chain */ do { /* check whether `key' is somewhere in the chain */
/* compare as `tsvalue', but may be other pointers (it is the same) */ /* compare as `tsvalue', but may be other pointers (it is the same) */
if (ttype_key(n) == ttype(key) && tsvalue_key(n) == tsvalue(key)) if (ttype(key(n)) == ttype(key) && tsvalue(key(n)) == tsvalue(key))
return &n->val; /* that's all */ return val(n); /* that's all */
else n = n->next; else n = n->next;
} while (n); } while (n);
if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */ if (L == NULL) return (TObject *)&luaO_nilobject; /* get option */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.h,v 1.31 2001/01/29 17:17:26 roberto Exp roberto $ ** $Id: ltable.h,v 1.32 2001/02/02 16:32:00 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -11,15 +11,9 @@
#define node(_t,_i) (&(_t)->node[_i]) #define node(_t,_i) (&(_t)->node[_i])
#define key(_n) (&(_n)->key)
#define val(_n) (&(_n)->val) #define val(_n) (&(_n)->val)
#define ttype_key(_n) ((_n)->key_tt)
#define nvalue_key(_n) ((_n)->key_value.n)
#define tsvalue_key(_n) ((_n)->key_value.ts)
#define setkey2obj(_o,_k) \
((_o)->tt = ttype_key(_k), (_o)->value = (_k)->key_value)
#define setobj2key(_k,_o) \
(ttype_key(_k) = (_o)->tt, (_k)->key_value = (_o)->value)
#define luaH_get(_t,_k) luaH_set(NULL,_t,_k) #define luaH_get(_t,_k) luaH_set(NULL,_t,_k)
#define luaH_getnum(_t,_k) luaH_setnum(NULL,_t,_k) #define luaH_getnum(_t,_k) luaH_setnum(NULL,_t,_k)

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 1.82 2001/06/06 18:00:19 roberto Exp roberto $ ** $Id: ltests.c,v 1.83 2001/06/15 20:36:57 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -298,7 +298,7 @@ static int hash_query (lua_State *L) {
TObject *o = luaA_index(L, 1); TObject *o = luaA_index(L, 1);
luaL_checktype(L, 2, LUA_TTABLE); luaL_checktype(L, 2, LUA_TTABLE);
t = hvalue(luaA_index(L, 2)); t = hvalue(luaA_index(L, 2));
setobj2key(&n, o); setobj(key(&n), o);
lua_pushnumber(L, luaH_mainposition(t, &n) - t->node); lua_pushnumber(L, luaH_mainposition(t, &n) - t->node);
} }
return 1; return 1;
@ -317,12 +317,9 @@ static int table_query (lua_State *L) {
} }
else if (i < t->size) { else if (i < t->size) {
if (ttype(val(node(t, i))) != LUA_TNIL || if (ttype(val(node(t, i))) != LUA_TNIL ||
ttype_key(node(t, i)) == LUA_TNIL || ttype(key(node(t, i))) == LUA_TNIL ||
ttype_key(node(t, i)) == LUA_TNUMBER || ttype(key(node(t, i))) == LUA_TNUMBER) {
tsvalue_key(node(t, i)) != NULL) { luaA_pushobject(L, key(node(t, i)));
TObject o;
setkey2obj(&o, node(t, i));
luaA_pushobject(L, &o);
} }
else else
lua_pushstring(L, "<undef>"); lua_pushstring(L, "<undef>");

6
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.186 2001/06/15 20:36:57 roberto Exp roberto $ ** $Id: lvm.c,v 1.187 2001/06/20 17:22:46 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -41,7 +41,7 @@ static void luaV_checkGC (lua_State *L, StkId top) {
const TObject *luaV_tonumber (const TObject *obj, TObject *n) { const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
if (ttype(obj) == LUA_TNUMBER) return obj; if (ttype(obj) == LUA_TNUMBER) return obj;
if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &nvalue(n))) { if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &nvalue(n))) {
ttype(n) = LUA_TNUMBER; setttype(n, LUA_TNUMBER);
return n; return n;
} }
else else
@ -617,7 +617,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
if (n != -1) { /* repeat loop? */ if (n != -1) { /* repeat loop? */
Node *node = node(t, n); Node *node = node(t, n);
setnvalue(ra+1, n); /* index */ setnvalue(ra+1, n); /* index */
setkey2obj(ra+2, node); setobj(ra+2, key(node));
setobj(ra+3, val(node)); setobj(ra+3, val(node));
dojump(pc, i); /* repeat loop */ dojump(pc, i); /* repeat loop */
} }