diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-08-11 13:38:34 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-08-11 13:38:34 -0300 |
commit | 8e226e6a09390a80fde98f192833fc85a5537376 (patch) | |
tree | feb8913c42586aa7444923d6e9da77097bf7ecd4 | |
parent | 1d420c2c11a408d6374b074005b33417877caabc (diff) | |
download | lua-8e226e6a09390a80fde98f192833fc85a5537376.tar.gz lua-8e226e6a09390a80fde98f192833fc85a5537376.tar.bz2 lua-8e226e6a09390a80fde98f192833fc85a5537376.zip |
small bug: nuse may change when table is rehashed;
3/2 is a good fraction for hash limit (instead of 0.7, using floats)
-rw-r--r-- | ltable.c | 21 |
1 files changed, 10 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.13 1998/07/12 16:15:19 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.14 1998/08/10 21:36:32 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 | */ |
@@ -20,8 +20,6 @@ | |||
20 | #define nodevector(t) ((t)->node) | 20 | #define nodevector(t) ((t)->node) |
21 | 21 | ||
22 | 22 | ||
23 | #define REHASH_LIMIT 0.70 /* avoid more than this % full */ | ||
24 | |||
25 | #define TagDefault LUA_T_ARRAY; | 23 | #define TagDefault LUA_T_ARRAY; |
26 | 24 | ||
27 | 25 | ||
@@ -107,10 +105,9 @@ void luaH_free (Hash *frees) | |||
107 | } | 105 | } |
108 | 106 | ||
109 | 107 | ||
110 | Hash *luaH_new (int nhash) | 108 | Hash *luaH_new (int nhash) { |
111 | { | ||
112 | Hash *t = luaM_new(Hash); | 109 | Hash *t = luaM_new(Hash); |
113 | nhash = luaO_redimension((int)((float)nhash/REHASH_LIMIT)); | 110 | nhash = luaO_redimension(nhash*3/2); |
114 | nodevector(t) = hashnodecreate(nhash); | 111 | nodevector(t) = hashnodecreate(nhash); |
115 | nhash(t) = nhash; | 112 | nhash(t) = nhash; |
116 | nuse(t) = 0; | 113 | nuse(t) = 0; |
@@ -133,18 +130,20 @@ static int newsize (Hash *t) { | |||
133 | return luaO_redimension((realuse+1)*2); /* +1 is the new element */ | 130 | return luaO_redimension((realuse+1)*2); /* +1 is the new element */ |
134 | } | 131 | } |
135 | 132 | ||
136 | static void rehash (Hash *t) | 133 | static void rehash (Hash *t) { |
137 | { | ||
138 | int nold = nhash(t); | 134 | int nold = nhash(t); |
139 | Node *vold = nodevector(t); | 135 | Node *vold = nodevector(t); |
140 | int nnew = newsize(t); | 136 | int nnew = newsize(t); |
141 | int i; | 137 | int i; |
142 | nodevector(t) = hashnodecreate(nnew); | 138 | nodevector(t) = hashnodecreate(nnew); |
143 | nhash(t) = nnew; | 139 | nhash(t) = nnew; |
140 | nuse(t) = 0; | ||
144 | for (i=0; i<nold; i++) { | 141 | for (i=0; i<nold; i++) { |
145 | Node *n = vold+i; | 142 | Node *n = vold+i; |
146 | if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) | 143 | if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) { |
147 | *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */ | 144 | *node(t, present(t, ref(n))) = *n; /* copy old node to luaM_new hash */ |
145 | nuse(t)++; | ||
146 | } | ||
148 | } | 147 | } |
149 | L->nblocks += gcsize(nnew)-gcsize(nold); | 148 | L->nblocks += gcsize(nnew)-gcsize(nold); |
150 | luaM_free(vold); | 149 | luaM_free(vold); |
@@ -170,11 +169,11 @@ TObject *luaH_set (Hash *t, TObject *ref) | |||
170 | { | 169 | { |
171 | Node *n = node(t, present(t, ref)); | 170 | Node *n = node(t, present(t, ref)); |
172 | if (ttype(ref(n)) == LUA_T_NIL) { | 171 | if (ttype(ref(n)) == LUA_T_NIL) { |
173 | nuse(t)++; | 172 | if ((long)nuse(t)*3L > (long)nhash(t)*2L) { |
174 | if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) { | ||
175 | rehash(t); | 173 | rehash(t); |
176 | n = node(t, present(t, ref)); | 174 | n = node(t, present(t, ref)); |
177 | } | 175 | } |
176 | nuse(t)++; | ||
178 | *ref(n) = *ref; | 177 | *ref(n) = *ref; |
179 | ttype(val(n)) = LUA_T_NIL; | 178 | ttype(val(n)) = LUA_T_NIL; |
180 | } | 179 | } |