diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-01-16 20:02:46 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-01-16 20:02:46 -0200 |
| commit | 566310fa04621a6fb848efec5cd00b7c9c6575c8 (patch) | |
| tree | d36127e52d3d8788cc4041124ab5d3085a9af748 /ltable.c | |
| parent | 6272c843dee7544bf319afbac85e8064fa1f3a4b (diff) | |
| download | lua-566310fa04621a6fb848efec5cd00b7c9c6575c8.tar.gz lua-566310fa04621a6fb848efec5cd00b7c9c6575c8.tar.bz2 lua-566310fa04621a6fb848efec5cd00b7c9c6575c8.zip | |
small optimization
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 21 |
1 files changed, 13 insertions, 8 deletions
| @@ -138,20 +138,22 @@ int luaH_nexti (Table *t, int i, TObject *where) { | |||
| 138 | 138 | ||
| 139 | 139 | ||
| 140 | static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { | 140 | static void computesizes (int nums[], int ntotal, int *narray, int *nhash) { |
| 141 | int n = 0; /* optimal (log of) size for array part */ | 141 | int n = 0; /* (log of) optimal size for array part */ |
| 142 | int na = 0; /* number of elements to go to array part */ | 142 | int na = 0; /* number of elements to go to array part */ |
| 143 | int a = nums[0]; /* accumulator */ | 143 | int i=0; |
| 144 | int i; | 144 | int a = nums[0]; /* number of elements smaller than 2^i */ |
| 145 | for (i=1; i<=MAXBITS; i++) { | 145 | while (++i <= MAXBITS && *narray >= twoto(i-1)) { |
| 146 | if (nums[i] == 0) continue; /* ignore empty slices */ | 146 | if (nums[i] == 0) continue; |
| 147 | a += nums[i]; /* number of elements smaller than 2^i */ | 147 | a += nums[i]; |
| 148 | if (a >= (1<<(i-1))) { /* more than half elements in use? */ | 148 | if (a >= twoto(i-1)) { /* more than half elements in use? */ |
| 149 | n = i; | 149 | n = i; |
| 150 | na = a; | 150 | na = a; |
| 151 | } | 151 | } |
| 152 | } | 152 | } |
| 153 | lua_assert(na <= *narray && *narray <= ntotal); | ||
| 153 | *nhash = ntotal - na; | 154 | *nhash = ntotal - na; |
| 154 | *narray = (n == 0) ? 0 : (1<<n); | 155 | *narray = (n == 0) ? 0 : (1<<n); |
| 156 | lua_assert(na <= *narray && na >= *narray/2); | ||
| 155 | } | 157 | } |
| 156 | 158 | ||
| 157 | 159 | ||
| @@ -172,13 +174,16 @@ static void numuse (const Table *t, int *narray, int *nhash) { | |||
| 172 | totaluse++; | 174 | totaluse++; |
| 173 | } | 175 | } |
| 174 | } | 176 | } |
| 177 | *narray = totaluse; /* all previous uses were in array part */ | ||
| 175 | /* count elements in hash part */ | 178 | /* count elements in hash part */ |
| 176 | i = sizenode(t); | 179 | i = sizenode(t); |
| 177 | while (i--) { | 180 | while (i--) { |
| 178 | if (ttype(val(&t->node[i])) != LUA_TNIL) { | 181 | if (ttype(val(&t->node[i])) != LUA_TNIL) { |
| 179 | int k = arrayindex(key(&t->node[i])); | 182 | int k = arrayindex(key(&t->node[i])); |
| 180 | if (k >= 0) /* is `key' an appropriate array index? */ | 183 | if (k >= 0) { /* is `key' an appropriate array index? */ |
| 181 | nums[luaO_log2(k-1)+1]++; /* count as such */ | 184 | nums[luaO_log2(k-1)+1]++; /* count as such */ |
| 185 | (*narray)++; | ||
| 186 | } | ||
| 182 | totaluse++; | 187 | totaluse++; |
| 183 | } | 188 | } |
| 184 | } | 189 | } |
