aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-26 16:59:20 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-26 16:59:20 -0200
commitd015f1fc02e03864b0ed3ad668a6e0660417a718 (patch)
tree00b5962e40540bbb8a71bf8665fed256182aa6c7
parent790690a2236ab0ad0cce35551c17e62064b4c85b (diff)
downloadlua-d015f1fc02e03864b0ed3ad668a6e0660417a718.tar.gz
lua-d015f1fc02e03864b0ed3ad668a6e0660417a718.tar.bz2
lua-d015f1fc02e03864b0ed3ad668a6e0660417a718.zip
table sizes don't need to be primes; power of 2 gives the same performance.
-rw-r--r--lgc.c4
-rw-r--r--lobject.c25
-rw-r--r--lobject.h9
-rw-r--r--lstring.c21
-rw-r--r--lstring.h4
-rw-r--r--ltable.c14
6 files changed, 37 insertions, 40 deletions
diff --git a/lgc.c b/lgc.c
index fe55f3e7..c3259421 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.32 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lgc.c,v 1.33 1999/11/23 13:58:02 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -208,7 +208,7 @@ static void collectstring (lua_State *L, int limit) {
208 } 208 }
209 } 209 }
210 if ((tb->nuse+1)*6 < tb->size) 210 if ((tb->nuse+1)*6 < tb->size)
211 luaS_grow(L, tb); /* table is too big; `grow' it to a smaller size */ 211 luaS_resize(L, tb, tb->size/2); /* table is too big */
212 } 212 }
213} 213}
214 214
diff --git a/lobject.c b/lobject.c
index 1e9f99d3..e2845127 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.24 1999/10/04 17:51:04 roberto Exp roberto $ 2** $Id: lobject.c,v 1.25 1999/11/22 13:12:07 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -22,22 +22,13 @@ const char *const luaO_typenames[] = { /* ORDER LUA_T */
22const TObject luaO_nilobject = {LUA_T_NIL, {NULL}}; 22const TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
23 23
24 24
25 25/*
26/* hash dimensions values */ 26** returns smaller power of 2 bigger than `n' (minimum is MINPOWER2)
27static const long dimensions[] = 27*/
28 {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L, 28unsigned long luaO_power2 (unsigned long n) {
29 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L, 29 unsigned long p = MINPOWER2;
30 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT}; 30 while (p<=n) p<<=1;
31 31 return p;
32
33int luaO_redimension (lua_State *L, int oldsize) {
34 int i;
35 for (i=0; dimensions[i]<MAX_INT; i++) {
36 if (dimensions[i] > oldsize)
37 return dimensions[i];
38 }
39 lua_error(L, "tableEM");
40 return 0; /* to avoid warnings */
41} 32}
42 33
43 34
diff --git a/lobject.h b/lobject.h
index ea397620..d286095f 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.36 1999/11/10 15:39:35 roberto Exp roberto $ 2** $Id: lobject.h,v 1.37 1999/11/22 13:12:07 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -45,7 +45,8 @@ typedef unsigned char Byte; /* unsigned 8 bits */
45 45
46 46
47/* convertion of pointer to int (for hashing only) */ 47/* convertion of pointer to int (for hashing only) */
48#define IntPoint(L, p) ((unsigned int)(p)) 48/* (the shift removes bits that are usually 0 because of alignment) */
49#define IntPoint(L, p) (((unsigned int)(p)) >> 3)
49 50
50 51
51/* 52/*
@@ -194,9 +195,11 @@ typedef struct Hash {
194 195
195extern const char *const luaO_typenames[]; 196extern const char *const luaO_typenames[];
196 197
197
198#define luaO_typename(L, o) luaO_typenames[-ttype(o)] 198#define luaO_typename(L, o) luaO_typenames[-ttype(o)]
199 199
200#define MINPOWER2 4 /* minimum size for "growing" vectors */
201
202unsigned long luaO_power2 (unsigned long n);
200 203
201extern const TObject luaO_nilobject; 204extern const TObject luaO_nilobject;
202 205
diff --git a/lstring.c b/lstring.c
index dcfbe718..d8e83953 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.28 1999/11/22 13:12:07 roberto Exp roberto $ 2** $Id: lstring.c,v 1.29 1999/11/22 18:24:50 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -52,24 +52,25 @@ static unsigned long hash_s (const char *s, long l) {
52} 52}
53 53
54 54
55void luaS_grow (lua_State *L, stringtable *tb) { 55void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
56 int ns = luaO_redimension(L, tb->nuse); /* new size */ 56 TaggedString **newhash = luaM_newvector(L, newsize, TaggedString *);
57 TaggedString **newhash = luaM_newvector(L, ns, TaggedString *);
58 int i; 57 int i;
59 for (i=0; i<ns; i++) newhash[i] = NULL; 58 for (i=0; i<newsize; i++) newhash[i] = NULL;
60 /* rehash */ 59 /* rehash */
61 for (i=0; i<tb->size; i++) { 60 for (i=0; i<tb->size; i++) {
62 TaggedString *p = tb->hash[i]; 61 TaggedString *p = tb->hash[i];
63 while (p) { /* for each node in the list */ 62 while (p) { /* for each node in the list */
64 TaggedString *next = p->nexthash; /* save next */ 63 TaggedString *next = p->nexthash; /* save next */
65 int h = p->hash%ns; /* new position */ 64 int h = p->hash&(newsize-1); /* new position */
65 LUA_ASSERT(L, p->hash%newsize == (p->hash&(newsize-1)),
66 "a&(x-1) == a%x, for x power of 2");
66 p->nexthash = newhash[h]; /* chain it in new position */ 67 p->nexthash = newhash[h]; /* chain it in new position */
67 newhash[h] = p; 68 newhash[h] = p;
68 p = next; 69 p = next;
69 } 70 }
70 } 71 }
71 luaM_free(L, tb->hash); 72 luaM_free(L, tb->hash);
72 tb->size = ns; 73 tb->size = newsize;
73 tb->hash = newhash; 74 tb->hash = newhash;
74} 75}
75 76
@@ -113,14 +114,14 @@ static void newentry (lua_State *L, stringtable *tb, TaggedString *ts, int h) {
113 tb->hash[h] = ts; 114 tb->hash[h] = ts;
114 tb->nuse++; 115 tb->nuse++;
115 if (tb->nuse > tb->size) /* too crowded? */ 116 if (tb->nuse > tb->size) /* too crowded? */
116 luaS_grow(L, tb); 117 luaS_resize(L, tb, tb->size*2);
117} 118}
118 119
119 120
120TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) { 121TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) {
121 unsigned long h = hash_s(str, l); 122 unsigned long h = hash_s(str, l);
122 stringtable *tb = &L->string_root[h%NUM_HASHSTR]; 123 stringtable *tb = &L->string_root[h%NUM_HASHSTR];
123 int h1 = h%tb->size; 124 int h1 = h&(tb->size-1);
124 TaggedString *ts; 125 TaggedString *ts;
125 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) { 126 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
126 if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0)) 127 if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
@@ -136,7 +137,7 @@ TaggedString *luaS_newlstr (lua_State *L, const char *str, long l) {
136TaggedString *luaS_createudata (lua_State *L, void *udata, int tag) { 137TaggedString *luaS_createudata (lua_State *L, void *udata, int tag) {
137 unsigned long h = IntPoint(L, udata); 138 unsigned long h = IntPoint(L, udata);
138 stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR]; 139 stringtable *tb = &L->string_root[(h%NUM_HASHUDATA)+NUM_HASHSTR];
139 int h1 = h%tb->size; 140 int h1 = h&(tb->size-1);
140 TaggedString *ts; 141 TaggedString *ts;
141 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) { 142 for (ts = tb->hash[h1]; ts; ts = ts->nexthash) {
142 if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG)) 143 if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
diff --git a/lstring.h b/lstring.h
index be260b89..31feabdb 100644
--- a/lstring.h
+++ b/lstring.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.h,v 1.12 1999/11/04 17:22:26 roberto Exp roberto $ 2** $Id: lstring.h,v 1.13 1999/11/22 13:12:07 roberto Exp roberto $
3** String table (keep all strings handled by Lua) 3** String table (keep all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -26,7 +26,7 @@
26 26
27 27
28void luaS_init (lua_State *L); 28void luaS_init (lua_State *L);
29void luaS_grow (lua_State *L, stringtable *tb); 29void luaS_resize (lua_State *L, stringtable *tb, int newsize);
30TaggedString *luaS_createudata (lua_State *L, void *udata, int tag); 30TaggedString *luaS_createudata (lua_State *L, void *udata, int tag);
31void luaS_freeall (lua_State *L); 31void luaS_freeall (lua_State *L);
32void luaS_free (lua_State *L, TaggedString *ts); 32void luaS_free (lua_State *L, TaggedString *ts);
diff --git a/ltable.c b/ltable.c
index 8a207b80..ea3ab4a9 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.29 1999/11/10 15:39:35 roberto Exp roberto $ 2** $Id: ltable.c,v 1.30 1999/11/22 13:12:07 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -65,7 +65,9 @@ Node *luaH_mainposition (lua_State *L, const Hash *t, const TObject *key) {
65 lua_error(L, "unexpected type to index table"); 65 lua_error(L, "unexpected type to index table");
66 h = 0; /* to avoid warnings */ 66 h = 0; /* to avoid warnings */
67 } 67 }
68 return &t->node[h%(unsigned int)t->size]; 68 LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
69 "a&(x-1) == a%x, for x power of 2");
70 return &t->node[h&((unsigned int)t->size-1)];
69} 71}
70 72
71 73
@@ -109,7 +111,7 @@ static void setnodevector (lua_State *L, Hash *t, int size) {
109 111
110Hash *luaH_new (lua_State *L, int size) { 112Hash *luaH_new (lua_State *L, int size) {
111 Hash *t = luaM_new(L, Hash); 113 Hash *t = luaM_new(L, Hash);
112 setnodevector(L, t, luaO_redimension(L, size+1)); 114 setnodevector(L, t, luaO_power2(size));
113 t->htag = TagDefault; 115 t->htag = TagDefault;
114 t->next = L->roottable; 116 t->next = L->roottable;
115 L->roottable = t; 117 L->roottable = t;
@@ -125,7 +127,7 @@ void luaH_free (lua_State *L, Hash *t) {
125} 127}
126 128
127 129
128static int newsize (lua_State *L, const Hash *t) { 130static int newsize (const Hash *t) {
129 Node *v = t->node; 131 Node *v = t->node;
130 int size = t->size; 132 int size = t->size;
131 int realuse = 0; 133 int realuse = 0;
@@ -134,7 +136,7 @@ static int newsize (lua_State *L, const Hash *t) {
134 if (ttype(&v[i].val) != LUA_T_NIL) 136 if (ttype(&v[i].val) != LUA_T_NIL)
135 realuse++; 137 realuse++;
136 } 138 }
137 return luaO_redimension(L, realuse*2); 139 return luaO_power2(realuse+realuse/4+1);
138} 140}
139 141
140 142
@@ -148,7 +150,7 @@ static void rehash (lua_State *L, Hash *t) {
148 Node *nold = t->node; 150 Node *nold = t->node;
149 int i; 151 int i;
150 L->nblocks -= gcsize(L, oldsize); 152 L->nblocks -= gcsize(L, oldsize);
151 setnodevector(L, t, newsize(L, t)); /* create new array of nodes */ 153 setnodevector(L, t, newsize(t)); /* create new array of nodes */
152 /* first loop; set only elements that can go in their main positions */ 154 /* first loop; set only elements that can go in their main positions */
153 for (i=0; i<oldsize; i++) { 155 for (i=0; i<oldsize; i++) {
154 Node *old = nold+i; 156 Node *old = nold+i;