瀏覽代碼

small optimization for table size in machines with double allignment

Roberto Ierusalimschy 21 年之前
父節點
當前提交
bd38017ddf
共有 7 個文件被更改,包括 47 次插入35 次删除
  1. 2 2
      lgc.c
  2. 2 2
      lobject.c
  3. 13 6
      lobject.h
  4. 2 2
      lstate.c
  5. 20 18
      ltable.c
  6. 4 1
      ltable.h
  7. 4 4
      ltests.c

+ 2 - 2
lgc.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 2.11 2004/09/08 14:23:09 roberto Exp roberto $
+** $Id: lgc.c,v 2.12 2004/09/15 20:38:15 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -367,7 +367,7 @@ static void cleartable (GCObject *l) {
     while (i--) {
       Node *n = gnode(h, i);
       if (!ttisnil(gval(n)) &&  /* non-empty entry? */
-          (iscleared(gkey(n), 1) || iscleared(gval(n), 0)))
+          (iscleared(key2tval(n), 1) || iscleared(gval(n), 0)))
         removeentry(n);  /* remove entry from table */
     }
     l = h->gclist;

+ 2 - 2
lobject.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.c,v 2.3 2004/05/03 12:30:41 roberto Exp roberto $
+** $Id: lobject.c,v 2.4 2004/07/09 16:01:38 roberto Exp roberto $
 ** Some generic functions over Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -23,7 +23,7 @@
 
 
 
-const TValue luaO_nilobject = {LUA_TNIL, {NULL}};
+const TValue luaO_nilobject = {{NULL}, LUA_TNIL};
 
 
 /*

+ 13 - 6
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 2.4 2004/03/15 21:04:33 roberto Exp roberto $
+** $Id: lobject.h,v 2.5 2004/05/31 18:51:50 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -64,9 +64,11 @@ typedef union {
 /*
 ** Tagged Values
 */
+
+#define TValuefields	Value value; int tt
+
 typedef struct lua_TValue {
-  int tt;
-  Value value;
+  TValuefields;
 } TValue;
 
 
@@ -158,7 +160,7 @@ typedef struct lua_TValue {
 
 #define setobj(L,obj1,obj2) \
   { const TValue *o2=(obj2); TValue *o1=(obj1); \
-    o1->tt=o2->tt; o1->value = o2->value; \
+    o1->value = o2->value; o1->tt=o2->tt; \
     checkliveness(G(L),o1); }
 
 
@@ -308,10 +310,15 @@ typedef union Closure {
 ** Tables
 */
 
+typedef struct TKey {
+  TValuefields;
+  struct Node *next;  /* for chaining */
+} TKey;
+
+
 typedef struct Node {
-  TValue i_key;
   TValue i_val;
-  struct Node *next;  /* for chaining */
+  TKey i_key;
 } Node;
 
 

+ 2 - 2
lstate.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 2.13 2004/09/08 14:23:09 roberto Exp roberto $
+** $Id: lstate.c,v 2.14 2004/09/15 20:39:42 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -191,7 +191,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   g->tmudata = NULL;
   setnilvalue(gkey(g->dummynode));
   setnilvalue(gval(g->dummynode));
-  g->dummynode->next = NULL;
+  gnext(g->dummynode) = NULL;
   g->totalbytes = sizeof(LG);
   if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
     /* memory allocation error: free partial state */

+ 20 - 18
ltable.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.c,v 2.5 2004/08/31 17:57:33 roberto Exp roberto $
+** $Id: ltable.c,v 2.6 2004/09/27 18:54:45 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -152,7 +152,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
   }
   for (i -= t->sizearray; i < sizenode(t); i++) {  /* then hash part */
     if (!ttisnil(gval(gnode(t, i)))) {  /* a non-nil value? */
-      setobj2s(L, key, gkey(gnode(t, i)));
+      setobj2s(L, key, key2tval(gnode(t, i)));
       setobj2s(L, key+1, gval(gnode(t, i)));
       return 1;
     }
@@ -215,7 +215,7 @@ static void numuse (const Table *t, int *narray, int *nhash) {
   while (i--) {
     Node *n = &t->node[i];
     if (!ttisnil(gval(n))) {
-      int k = arrayindex(gkey(n));
+      int k = arrayindex(key2tval(n));
       if (0 < k && k <= MAXASIZE) {  /* is `key' an appropriate array index? */
         nums[luaO_log2(k-1)+1]++;  /* count as such */
         (*narray)++;
@@ -245,12 +245,12 @@ static void setnodevector (lua_State *L, Table *t, int lsize) {
     t->node = G(L)->dummynode;  /* use common `dummynode' */
     lua_assert(ttisnil(gkey(t->node)));  /* assert invariants: */
     lua_assert(ttisnil(gval(t->node)));
-    lua_assert(t->node->next == NULL);  /* (`dummynode' must be empty) */
+    lua_assert(gnext(t->node) == NULL);  /* (`dummynode' must be empty) */
   }
   else {
     t->node = luaM_newvector(L, size, Node);
     for (i=0; i<size; i++) {
-      t->node[i].next = NULL;
+      gnext(&t->node[i]) = NULL;
       setnilvalue(gkey(gnode(t, i)));
       setnilvalue(gval(gnode(t, i)));
     }
@@ -274,7 +274,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
     nold = temp;
     setnilvalue(gkey(G(L)->dummynode));  /* restate invariant */
     setnilvalue(gval(G(L)->dummynode));
-    lua_assert(G(L)->dummynode->next == NULL);
+    lua_assert(gnext(G(L)->dummynode) == NULL);
   }
   if (nasize > oldasize)  /* array part must grow? */
     setarrayvector(L, t, nasize);
@@ -295,7 +295,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) {
   for (i = twoto(oldhsize) - 1; i >= 0; i--) {
     Node *old = nold+i;
     if (!ttisnil(gval(old)))
-      setobjt2t(L, luaH_set(L, t, gkey(old)), gval(old));
+      setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));
   }
   if (oldhsize)
     luaM_freearray(L, nold, twoto(oldhsize), Node);  /* free old array */
@@ -351,24 +351,25 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) {
   TValue *val;
   Node *mp = luaH_mainposition(t, key);
   if (!ttisnil(gval(mp))) {  /* main position is not free? */
-    Node *othern = luaH_mainposition(t, gkey(mp));  /* `mp' of colliding node */
+    /* `mp' of colliding node */
+    Node *othern = luaH_mainposition(t, key2tval(mp));
     Node *n = t->firstfree;  /* get a free place */
     if (othern != mp) {  /* is colliding node out of its main position? */
       /* yes; move colliding node into free position */
-      while (othern->next != mp) othern = othern->next;  /* find previous */
-      othern->next = n;  /* redo the chain with `n' in place of `mp' */
+      while (gnext(othern) != mp) othern = gnext(othern);  /* find previous */
+      gnext(othern) = n;  /* redo the chain with `n' in place of `mp' */
       *n = *mp;  /* copy colliding node into free pos. (mp->next also goes) */
-      mp->next = NULL;  /* now `mp' is free */
+      gnext(mp) = NULL;  /* now `mp' is free */
       setnilvalue(gval(mp));
     }
     else {  /* colliding node is in its own main position */
       /* new node will go into free position */
-      n->next = mp->next;  /* chain new position */
-      mp->next = n;
+      gnext(n) = gnext(mp);  /* chain new position */
+      gnext(mp) = n;
       mp = n;
     }
   }
-  setobj2t(L, gkey(mp), key);
+  gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;
   luaC_barriert(L, t, key);
   lua_assert(ttisnil(gval(mp)));
   for (;;) {  /* correct `firstfree' */
@@ -400,7 +401,7 @@ const TValue *luaH_getnum (Table *t, int key) {
     do {  /* check whether `key' is somewhere in the chain */
       if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == nk)
         return gval(n);  /* that's it */
-      else n = n->next;
+      else n = gnext(n);
     } while (n);
     return &luaO_nilobject;
   }
@@ -415,7 +416,7 @@ const TValue *luaH_getstr (Table *t, TString *key) {
   do {  /* check whether `key' is somewhere in the chain */
     if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)
       return gval(n);  /* that's it */
-    else n = n->next;
+    else n = gnext(n);
   } while (n);
   return &luaO_nilobject;
 }
@@ -438,8 +439,9 @@ const TValue *luaH_get (Table *t, const TValue *key) {
     default: {
       Node *n = luaH_mainposition(t, key);
       do {  /* check whether `key' is somewhere in the chain */
-        if (luaO_rawequalObj(gkey(n), key)) return gval(n);  /* that's it */
-        else n = n->next;
+        if (luaO_rawequalObj(key2tval(n), key))
+          return gval(n);  /* that's it */
+        else n = gnext(n);
       } while (n);
       return &luaO_nilobject;
     }

+ 4 - 1
ltable.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.h,v 2.1 2003/12/10 12:13:36 roberto Exp roberto $
+** $Id: ltable.h,v 2.2 2004/03/26 14:02:41 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -13,6 +13,9 @@
 #define gnode(t,i)	(&(t)->node[i])
 #define gkey(n)		(&(n)->i_key)
 #define gval(n)		(&(n)->i_val)
+#define gnext(n)	((n)->i_key.next)
+
+#define key2tval(n)	(cast(const TValue *, gkey(n)))
 
 
 const TValue *luaH_getnum (Table *t, int key);

+ 4 - 4
ltests.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltests.c,v 2.12 2004/09/21 16:54:32 roberto Exp roberto $
+** $Id: ltests.c,v 2.13 2004/09/29 21:00:25 roberto Exp roberto $
 ** Internal Module for Debugging of the Lua Implementation
 ** See Copyright Notice in lua.h
 */
@@ -554,13 +554,13 @@ static int table_query (lua_State *L) {
     if (!ttisnil(gval(gnode(t, i))) ||
         ttisnil(gkey(gnode(t, i))) ||
         ttisnumber(gkey(gnode(t, i)))) {
-      luaA_pushobject(L, gkey(gnode(t, i)));
+      luaA_pushobject(L, key2tval(gnode(t, i)));
     }
     else
       lua_pushliteral(L, "<undef>");
     luaA_pushobject(L, gval(gnode(t, i)));
-    if (t->node[i].next)
-      lua_pushinteger(L, t->node[i].next - t->node);
+    if (gnext(&t->node[i]))
+      lua_pushinteger(L, gnext(&t->node[i]) - t->node);
     else
       lua_pushnil(L);
   }