aboutsummaryrefslogtreecommitdiff
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
parentb9c98cd4d97774d703a48fefd56c66f552e0cd83 (diff)
downloadlua-534c3a64d3b47585b415f229aa03af35f9a4796e.tar.gz
lua-534c3a64d3b47585b415f229aa03af35f9a4796e.tar.bz2
lua-534c3a64d3b47585b415f229aa03af35f9a4796e.zip
small optimizations for table access
-rw-r--r--lbuiltin.c24
-rw-r--r--lobject.c5
-rw-r--r--lobject.h5
-rw-r--r--ltable.c48
-rw-r--r--ltable.h6
5 files changed, 57 insertions, 31 deletions
diff --git a/lbuiltin.c b/lbuiltin.c
index 502c70c8..d57f9fa2 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.105 2000/04/14 17:44:20 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.106 2000/04/17 19:23:12 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -321,7 +321,7 @@ void luaB_call (lua_State *L) {
321 /* push arg[1...n] */ 321 /* push arg[1...n] */
322 luaD_checkstack(L, narg); 322 luaD_checkstack(L, narg);
323 for (i=0; i<narg; i++) 323 for (i=0; i<narg; i++)
324 *(L->top++) = *luaH_getint(L, arg, i+1); 324 *(L->top++) = *luaH_getnum(arg, i+1);
325 status = lua_callfunction(L, f); 325 status = lua_callfunction(L, f);
326 if (err != LUA_NOOBJECT) { /* restore old error method */ 326 if (err != LUA_NOOBJECT) { /* restore old error method */
327 lua_pushobject(L, err); 327 lua_pushobject(L, err);
@@ -434,7 +434,7 @@ void luaB_foreachi (lua_State *L) {
434 for (i=1; i<=n; i++) { 434 for (i=1; i<=n; i++) {
435 *(L->top++) = *f; 435 *(L->top++) = *f;
436 ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; 436 ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i;
437 *(L->top++) = *luaH_getint(L, t, i); 437 *(L->top++) = *luaH_getnum(t, i);
438 luaD_call(L, L->top-3, 1); 438 luaD_call(L, L->top-3, 1);
439 if (ttype(L->top-1) != TAG_NIL) 439 if (ttype(L->top-1) != TAG_NIL)
440 return; 440 return;
@@ -513,7 +513,7 @@ void luaB_tremove (lua_State *L) {
513 int n = (int)getnarg(L, a); 513 int n = (int)getnarg(L, a);
514 int pos = luaL_opt_int(L, 2, n); 514 int pos = luaL_opt_int(L, 2, n);
515 if (n <= 0) return; /* table is "empty" */ 515 if (n <= 0) return; /* table is "empty" */
516 luaA_pushobject(L, luaH_getint(L, a, pos)); /* result = a[pos] */ 516 luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */
517 for ( ;pos<n; pos++) 517 for ( ;pos<n; pos++)
518 luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */ 518 luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */
519 luaV_setn(L, a, n-1); /* a.n = n-1 */ 519 luaV_setn(L, a, n-1); /* a.n = n-1 */
@@ -529,7 +529,7 @@ void luaB_tremove (lua_State *L) {
529 529
530static void swap (lua_State *L, Hash *a, int i, int j) { 530static void swap (lua_State *L, Hash *a, int i, int j) {
531 TObject temp; 531 TObject temp;
532 temp = *luaH_getint(L, a, i); 532 temp = *luaH_getnum(a, i);
533 luaH_move(L, a, j, i); 533 luaH_move(L, a, j, i);
534 luaH_setint(L, a, j, &temp); 534 luaH_setint(L, a, j, &temp);
535} 535}
@@ -556,26 +556,26 @@ static void auxsort (lua_State *L, Hash *a, int l, int u, lua_Object f) {
556 while (l < u) { /* for tail recursion */ 556 while (l < u) { /* for tail recursion */
557 int i, j; 557 int i, j;
558 /* sort elements a[l], a[(l+u)/2] and a[u] */ 558 /* sort elements a[l], a[(l+u)/2] and a[u] */
559 if (sort_comp(L, f, luaH_getint(L, a, u), luaH_getint(L, a, l))) 559 if (sort_comp(L, f, luaH_getnum(a, u), luaH_getnum(a, l)))
560 swap(L, a, l, u); /* a[u]<a[l] */ 560 swap(L, a, l, u); /* a[u]<a[l] */
561 if (u-l == 1) break; /* only 2 elements */ 561 if (u-l == 1) break; /* only 2 elements */
562 i = (l+u)/2; 562 i = (l+u)/2;
563 *P = *luaH_getint(L, a, i); /* P = a[i] */ 563 *P = *luaH_getnum(a, i); /* P = a[i] */
564 if (sort_comp(L, f, P, luaH_getint(L, a, l))) /* a[i]<a[l]? */ 564 if (sort_comp(L, f, P, luaH_getnum(a, l))) /* a[i]<a[l]? */
565 swap(L, a, l, i); 565 swap(L, a, l, i);
566 else if (sort_comp(L, f, luaH_getint(L, a, u), P)) /* a[u]<a[i]? */ 566 else if (sort_comp(L, f, luaH_getnum(a, u), P)) /* a[u]<a[i]? */
567 swap(L, a, i, u); 567 swap(L, a, i, u);
568 if (u-l == 2) break; /* only 3 elements */ 568 if (u-l == 2) break; /* only 3 elements */
569 *P = *luaH_getint(L, a, i); /* save pivot on stack (GC) */ 569 *P = *luaH_getnum(a, i); /* save pivot on stack (GC) */
570 swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */ 570 swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */
571 /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */ 571 /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
572 i = l; j = u-1; 572 i = l; j = u-1;
573 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ 573 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
574 /* repeat i++ until a[i] >= P */ 574 /* repeat i++ until a[i] >= P */
575 while (sort_comp(L, f, luaH_getint(L, a, ++i), P)) 575 while (sort_comp(L, f, luaH_getnum(a, ++i), P))
576 if (i>u) lua_error(L, "invalid order function for sorting"); 576 if (i>u) lua_error(L, "invalid order function for sorting");
577 /* repeat j-- until a[j] <= P */ 577 /* repeat j-- until a[j] <= P */
578 while (sort_comp(L, f, P, luaH_getint(L, a, --j))) 578 while (sort_comp(L, f, P, luaH_getnum(a, --j)))
579 if (j<l) lua_error(L, "invalid order function for sorting"); 579 if (j<l) lua_error(L, "invalid order function for sorting");
580 if (j<i) break; 580 if (j<i) break;
581 swap(L, a, i, j); 581 swap(L, a, i, j);
diff --git a/lobject.c b/lobject.c
index 14cc2313..9d743fd4 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.35 2000/03/29 20:19:20 roberto Exp roberto $ 2** $Id: lobject.c,v 1.36 2000/03/31 16:28:45 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*/
@@ -32,7 +32,8 @@ unsigned long luaO_power2 (unsigned long n) {
32} 32}
33 33
34 34
35int luaO_equalval (const TObject *t1, const TObject *t2) { 35int luaO_equalObj (const TObject *t1, const TObject *t2) {
36 if (ttype(t1) != ttype(t2)) return 0;
36 switch (ttype(t1)) { 37 switch (ttype(t1)) {
37 case TAG_NUMBER: 38 case TAG_NUMBER:
38 return nvalue(t1) == nvalue(t2); 39 return nvalue(t1) == nvalue(t2);
diff --git a/lobject.h b/lobject.h
index 57ea2bd0..921a7a05 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.59 2000/03/31 16:28:45 roberto Exp roberto $ 2** $Id: lobject.h,v 1.60 2000/04/10 19:20:24 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*/
@@ -180,8 +180,7 @@ extern const TObject luaO_nilobject;
180 180
181unsigned long luaO_power2 (unsigned long n); 181unsigned long luaO_power2 (unsigned long n);
182 182
183#define luaO_equalObj(t1,t2) (ttype(t1) == ttype(t2) && luaO_equalval(t1,t2)) 183int luaO_equalObj (const TObject *t1, const TObject *t2);
184int luaO_equalval (const TObject *t1, const TObject *t2);
185int luaO_redimension (lua_State *L, int oldsize); 184int luaO_redimension (lua_State *L, int oldsize);
186int luaO_str2d (const char *s, Number *result); 185int luaO_str2d (const char *s, Number *result);
187 186
diff --git a/ltable.c b/ltable.c
index 299821ca..cc3a64f1 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.38 2000/03/29 20:19:20 roberto Exp roberto $ 2** $Id: ltable.c,v 1.39 2000/03/31 16:28:45 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*/
@@ -60,11 +60,12 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) {
60 } 60 }
61 LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)), 61 LUA_ASSERT(L, h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
62 "a&(x-1) == a%x, for x power of 2"); 62 "a&(x-1) == a%x, for x power of 2");
63 return &t->node[h&((unsigned int)t->size-1)]; 63 return &t->node[h&(t->size-1)];
64} 64}
65 65
66 66
67const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { 67static const TObject *luaH_getany (lua_State *L, const Hash *t,
68 const TObject *key) {
68 Node *n = luaH_mainposition(t, key); 69 Node *n = luaH_mainposition(t, key);
69 if (!n) 70 if (!n)
70 lua_error(L, "unexpected type to index table"); 71 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) {
77} 78}
78 79
79 80
81/* specialized version for numbers */
82const TObject *luaH_getnum (const Hash *t, Number key) {
83 Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
84 do {
85 if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key)
86 return &n->val;
87 n = n->next;
88 } while (n);
89 return &luaO_nilobject; /* key not found */
90}
91
92
93/* specialized version for strings */
94static const TObject *luaH_getstr (const Hash *t, TString *key) {
95 Node *n = &t->node[key->hash&(t->size-1)];
96 do {
97 if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key)
98 return &n->val;
99 n = n->next;
100 } while (n);
101 return &luaO_nilobject; /* key not found */
102}
103
104
105const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
106 switch (ttype(key)) {
107 case TAG_NUMBER: return luaH_getnum(t, nvalue(key));
108 case TAG_STRING: return luaH_getstr(t, tsvalue(key));
109 default: return luaH_getany(L, t, key);
110 }
111}
112
113
80int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { 114int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
81 const TObject *v = luaH_get(L, t, key); 115 const TObject *v = luaH_get(L, t, key);
82 return (v == &luaO_nilobject) ? -1 : /* key not found */ 116 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) {
214 luaH_set(L, t, &index, val); 248 luaH_set(L, t, &index, val);
215} 249}
216 250
217
218const TObject *luaH_getint (lua_State *L, const Hash *t, int key) {
219 TObject index;
220 ttype(&index) = TAG_NUMBER;
221 nvalue(&index) = key;
222 return luaH_get(L, t, &index);
223}
224
diff --git a/ltable.h b/ltable.h
index e678533e..9a5fe9a5 100644
--- a/ltable.h
+++ b/ltable.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.h,v 1.17 1999/11/23 13:58:02 roberto Exp roberto $ 2** $Id: ltable.h,v 1.18 1999/12/07 12:05:34 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*/
@@ -14,7 +14,7 @@
14#define key(n) (&(n)->key) 14#define key(n) (&(n)->key)
15#define val(n) (&(n)->val) 15#define val(n) (&(n)->val)
16 16
17#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getint(L, t, from))) 17#define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getnum(t, from)))
18 18
19Hash *luaH_new (lua_State *L, int nhash); 19Hash *luaH_new (lua_State *L, int nhash);
20void luaH_free (lua_State *L, Hash *t); 20void luaH_free (lua_State *L, Hash *t);
@@ -22,7 +22,7 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key);
22void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); 22void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val);
23int luaH_pos (lua_State *L, const Hash *t, const TObject *r); 23int luaH_pos (lua_State *L, const Hash *t, const TObject *r);
24void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); 24void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val);
25const TObject *luaH_getint (lua_State *L, const Hash *t, int key); 25const TObject *luaH_getnum (const Hash *t, Number key);
26unsigned long luaH_hash (lua_State *L, const TObject *key); 26unsigned long luaH_hash (lua_State *L, const TObject *key);
27 27
28/* exported only for debugging */ 28/* exported only for debugging */