diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-09-27 15:54:45 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-09-27 15:54:45 -0300 |
commit | 66845f415feb5aa98be5190c7bf12931d3d4c388 (patch) | |
tree | 3fc878008fac14a3abc8774a4350053c40be52d9 /ltable.c | |
parent | b2820f39a23bad2bc568623dbb2d56cd5697efa4 (diff) | |
download | lua-66845f415feb5aa98be5190c7bf12931d3d4c388.tar.gz lua-66845f415feb5aa98be5190c7bf12931d3d4c388.tar.bz2 lua-66845f415feb5aa98be5190c7bf12931d3d4c388.zip |
small optimizations
Diffstat (limited to 'ltable.c')
-rw-r--r-- | ltable.c | 21 |
1 files changed, 8 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.4 2004/08/10 19:17:23 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.5 2004/08/31 17:57:33 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 | */ |
@@ -107,11 +107,10 @@ Node *luaH_mainposition (const Table *t, const TValue *key) { | |||
107 | ** returns the index for `key' if `key' is an appropriate key to live in | 107 | ** returns the index for `key' if `key' is an appropriate key to live in |
108 | ** the array part of the table, -1 otherwise. | 108 | ** the array part of the table, -1 otherwise. |
109 | */ | 109 | */ |
110 | static int arrayindex (const TValue *key, lua_Number lim) { | 110 | static int arrayindex (const TValue *key) { |
111 | if (ttisnumber(key)) { | 111 | if (ttisnumber(key)) { |
112 | lua_Number n = nvalue(key); | 112 | lua_Number n = nvalue(key); |
113 | int k; | 113 | int k; |
114 | if (n <= 0 || n > lim) return -1; /* out of range? */ | ||
115 | lua_number2int(k, n); | 114 | lua_number2int(k, n); |
116 | if (cast(lua_Number, k) == nvalue(key)) | 115 | if (cast(lua_Number, k) == nvalue(key)) |
117 | return k; | 116 | return k; |
@@ -128,10 +127,9 @@ static int arrayindex (const TValue *key, lua_Number lim) { | |||
128 | static int luaH_index (lua_State *L, Table *t, StkId key) { | 127 | static int luaH_index (lua_State *L, Table *t, StkId key) { |
129 | int i; | 128 | int i; |
130 | if (ttisnil(key)) return -1; /* first iteration */ | 129 | if (ttisnil(key)) return -1; /* first iteration */ |
131 | i = arrayindex(key, t->sizearray); | 130 | i = arrayindex(key); |
132 | if (0 <= i) { /* is `key' inside array part? */ | 131 | if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ |
133 | return i-1; /* yes; that's the index (corrected to C) */ | 132 | return i-1; /* yes; that's the index (corrected to C) */ |
134 | } | ||
135 | else { | 133 | else { |
136 | const TValue *v = luaH_get(t, key); | 134 | const TValue *v = luaH_get(t, key); |
137 | if (v == &luaO_nilobject) | 135 | if (v == &luaO_nilobject) |
@@ -195,7 +193,6 @@ static void numuse (const Table *t, int *narray, int *nhash) { | |||
195 | int nums[MAXBITS+1]; | 193 | int nums[MAXBITS+1]; |
196 | int i, lg; | 194 | int i, lg; |
197 | int totaluse = 0; | 195 | int totaluse = 0; |
198 | lua_Number sizelimit; /* an upper bound for the array size */ | ||
199 | /* count elements in array part */ | 196 | /* count elements in array part */ |
200 | for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */ | 197 | for (i=0, lg=0; lg<=MAXBITS; lg++) { /* for each slice [2^(lg-1) to 2^lg) */ |
201 | int ttlg = twoto(lg); /* 2^lg */ | 198 | int ttlg = twoto(lg); /* 2^lg */ |
@@ -215,14 +212,11 @@ static void numuse (const Table *t, int *narray, int *nhash) { | |||
215 | *narray = totaluse; /* all previous uses were in array part */ | 212 | *narray = totaluse; /* all previous uses were in array part */ |
216 | /* count elements in hash part */ | 213 | /* count elements in hash part */ |
217 | i = sizenode(t); | 214 | i = sizenode(t); |
218 | /* array part cannot be larger than twice the maximum number of elements */ | ||
219 | sizelimit = cast(lua_Number, totaluse + i) * 2; | ||
220 | if (sizelimit >= MAXASIZE) sizelimit = MAXASIZE; | ||
221 | while (i--) { | 215 | while (i--) { |
222 | Node *n = &t->node[i]; | 216 | Node *n = &t->node[i]; |
223 | if (!ttisnil(gval(n))) { | 217 | if (!ttisnil(gval(n))) { |
224 | int k = arrayindex(gkey(n), sizelimit); | 218 | int k = arrayindex(gkey(n)); |
225 | if (k >= 0) { /* is `key' an appropriate array index? */ | 219 | if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ |
226 | nums[luaO_log2(k-1)+1]++; /* count as such */ | 220 | nums[luaO_log2(k-1)+1]++; /* count as such */ |
227 | (*narray)++; | 221 | (*narray)++; |
228 | } | 222 | } |
@@ -397,7 +391,8 @@ static TValue *newkey (lua_State *L, Table *t, const TValue *key) { | |||
397 | ** search function for integers | 391 | ** search function for integers |
398 | */ | 392 | */ |
399 | const TValue *luaH_getnum (Table *t, int key) { | 393 | const TValue *luaH_getnum (Table *t, int key) { |
400 | if (1 <= key && key <= t->sizearray) | 394 | /* (1 <= key && key <= t->sizearray) */ |
395 | if ((unsigned int)(key-1) < (unsigned int)t->sizearray) | ||
401 | return &t->array[key-1]; | 396 | return &t->array[key-1]; |
402 | else { | 397 | else { |
403 | lua_Number nk = cast(lua_Number, key); | 398 | lua_Number nk = cast(lua_Number, key); |