diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-15 16:17:33 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-15 16:17:33 -0300 |
commit | eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231 (patch) | |
tree | 281c96989a0f2bf9181551304ae8aa804935da04 /lgc.c | |
parent | ae19b2f51e21cef8ede07ce4e0b1546f2deefe91 (diff) | |
download | lua-eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231.tar.gz lua-eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231.tar.bz2 lua-eadf2aaaffa7a35e7f67b150ce0d57f2c17b9231.zip |
small optimizations
Diffstat (limited to 'lgc.c')
-rw-r--r-- | lgc.c | 114 |
1 files changed, 57 insertions, 57 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.c,v 1.103 2001/06/12 18:43:13 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 1.104 2001/06/13 18:51:20 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 | */ |
@@ -62,7 +62,7 @@ static void markclosure (GCState *st, Closure *cl) { | |||
62 | 62 | ||
63 | static void marktable (GCState *st, Hash *h) { | 63 | static void marktable (GCState *st, Hash *h) { |
64 | if (!ismarked(h)) { | 64 | if (!ismarked(h)) { |
65 | h->mark = st->tmark; /* chain it in list of marked */ | 65 | h->mark = st->tmark; /* chain it for later traversal */ |
66 | st->tmark = h; | 66 | st->tmark = h; |
67 | } | 67 | } |
68 | } | 68 | } |
@@ -127,8 +127,9 @@ static void traverseclosure (GCState *st, Closure *f) { | |||
127 | 127 | ||
128 | 128 | ||
129 | static void removekey (Node *n) { | 129 | static void removekey (Node *n) { |
130 | lua_assert(ttype(val(n)) == LUA_TNIL); | ||
130 | if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER) | 131 | if (ttype_key(n) != LUA_TNIL && ttype_key(n) != LUA_TNUMBER) |
131 | n->key_value.ts = NULL; /* dead key; remove it */ | 132 | ttype_key(n) = LUA_TNONE; /* dead key; remove it */ |
132 | } | 133 | } |
133 | 134 | ||
134 | 135 | ||
@@ -180,30 +181,29 @@ static void markall (lua_State *L) { | |||
180 | } | 181 | } |
181 | 182 | ||
182 | 183 | ||
183 | static int hasmark (const TObject *o) { | 184 | static int hasmark (int tt, Value *v) { |
184 | switch (ttype(o)) { | 185 | switch (tt) { |
185 | case LUA_TSTRING: | 186 | case LUA_TSTRING: |
186 | return tsvalue(o)->marked; | 187 | return v->ts->marked; |
187 | case LUA_TUSERDATA: | 188 | case LUA_TUSERDATA: |
188 | return ismarkedudata(uvalue(o)); | 189 | return ismarkedudata(v->u); |
189 | case LUA_TTABLE: | 190 | case LUA_TTABLE: |
190 | return ismarked(hvalue(o)); | 191 | return ismarked(v->h); |
191 | case LUA_TFUNCTION: | 192 | case LUA_TFUNCTION: |
192 | return ismarked(clvalue(o)); | 193 | return ismarked(v->cl); |
193 | default: /* number, nil */ | 194 | default: /* number, nil */ |
194 | return 1; | 195 | return 1; |
195 | } | 196 | } |
196 | } | 197 | } |
197 | 198 | ||
198 | 199 | ||
199 | static void invalidatetable (Hash *h) { | 200 | static void cleardeadnodes (Hash *h) { |
200 | int i; | 201 | int i; |
201 | for (i=0; i<h->size; i++) { | 202 | for (i=0; i<h->size; i++) { |
202 | Node *n = node(h, i); | 203 | Node *n = node(h, i); |
203 | TObject k; | ||
204 | if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */ | 204 | if (ttype(val(n)) == LUA_TNIL) continue; /* empty node */ |
205 | setkey2obj(&k, n); | 205 | if (!hasmark(ttype(val(n)), &(val(n)->value)) || |
206 | if (!hasmark(val(n)) || !hasmark(&k)) { | 206 | !hasmark(ttype_key(n), &n->key_value)) { |
207 | setnilvalue(val(n)); /* remove value */ | 207 | setnilvalue(val(n)); /* remove value */ |
208 | removekey(n); | 208 | removekey(n); |
209 | } | 209 | } |
@@ -211,27 +211,26 @@ static void invalidatetable (Hash *h) { | |||
211 | } | 211 | } |
212 | 212 | ||
213 | 213 | ||
214 | static void invalidatetables (global_State *G) { | 214 | static void cleartables (global_State *G) { |
215 | Hash *h; | 215 | Hash *h; |
216 | for (h = G->roottable; h; h = h->next) { | 216 | for (h = G->roottable; h; h = h->next) { |
217 | if (ismarked(h) && h->weakmode) | 217 | if (h->weakmode && ismarked(h)) |
218 | invalidatetable(h); | 218 | cleardeadnodes(h); |
219 | } | 219 | } |
220 | } | 220 | } |
221 | 221 | ||
222 | 222 | ||
223 | |||
224 | static void collectproto (lua_State *L) { | 223 | static void collectproto (lua_State *L) { |
225 | Proto **p = &G(L)->rootproto; | 224 | Proto **p = &G(L)->rootproto; |
226 | Proto *next; | 225 | Proto *curr; |
227 | while ((next = *p) != NULL) { | 226 | while ((curr = *p) != NULL) { |
228 | if (next->marked) { | 227 | if (curr->marked) { |
229 | next->marked = 0; | 228 | curr->marked = 0; |
230 | p = &next->next; | 229 | p = &curr->next; |
231 | } | 230 | } |
232 | else { | 231 | else { |
233 | *p = next->next; | 232 | *p = curr->next; |
234 | luaF_freeproto(L, next); | 233 | luaF_freeproto(L, curr); |
235 | } | 234 | } |
236 | } | 235 | } |
237 | } | 236 | } |
@@ -239,15 +238,15 @@ static void collectproto (lua_State *L) { | |||
239 | 238 | ||
240 | static void collectclosure (lua_State *L) { | 239 | static void collectclosure (lua_State *L) { |
241 | Closure **p = &G(L)->rootcl; | 240 | Closure **p = &G(L)->rootcl; |
242 | Closure *next; | 241 | Closure *curr; |
243 | while ((next = *p) != NULL) { | 242 | while ((curr = *p) != NULL) { |
244 | if (ismarked(next)) { | 243 | if (ismarked(curr)) { |
245 | next->mark = next; /* unmark */ | 244 | curr->mark = curr; /* unmark */ |
246 | p = &next->next; | 245 | p = &curr->next; |
247 | } | 246 | } |
248 | else { | 247 | else { |
249 | *p = next->next; | 248 | *p = curr->next; |
250 | luaF_freeclosure(L, next); | 249 | luaF_freeclosure(L, curr); |
251 | } | 250 | } |
252 | } | 251 | } |
253 | } | 252 | } |
@@ -255,15 +254,15 @@ static void collectclosure (lua_State *L) { | |||
255 | 254 | ||
256 | static void collecttable (lua_State *L) { | 255 | static void collecttable (lua_State *L) { |
257 | Hash **p = &G(L)->roottable; | 256 | Hash **p = &G(L)->roottable; |
258 | Hash *next; | 257 | Hash *curr; |
259 | while ((next = *p) != NULL) { | 258 | while ((curr = *p) != NULL) { |
260 | if (ismarked(next)) { | 259 | if (ismarked(curr)) { |
261 | next->mark = next; /* unmark */ | 260 | curr->mark = curr; /* unmark */ |
262 | p = &next->next; | 261 | p = &curr->next; |
263 | } | 262 | } |
264 | else { | 263 | else { |
265 | *p = next->next; | 264 | *p = curr->next; |
266 | luaH_free(L, next); | 265 | luaH_free(L, curr); |
267 | } | 266 | } |
268 | } | 267 | } |
269 | } | 268 | } |
@@ -271,17 +270,17 @@ static void collecttable (lua_State *L) { | |||
271 | 270 | ||
272 | void luaC_collectudata (lua_State *L) { | 271 | void luaC_collectudata (lua_State *L) { |
273 | Udata **p = &G(L)->rootudata; | 272 | Udata **p = &G(L)->rootudata; |
274 | Udata *next; | 273 | Udata *curr; |
275 | while ((next = *p) != NULL) { | 274 | while ((curr = *p) != NULL) { |
276 | if (ismarkedudata(next)) { | 275 | if (ismarkedudata(curr)) { |
277 | switchudatamark(next); /* unmark */ | 276 | switchudatamark(curr); /* unmark */ |
278 | p = &next->next; | 277 | p = &curr->next; |
279 | } | 278 | } |
280 | else { /* collect */ | 279 | else { /* collect */ |
281 | int tag = next->tag; | 280 | int tag = curr->tag; |
282 | *p = next->next; | 281 | *p = curr->next; |
283 | next->next = G(L)->TMtable[tag].collected; /* chain udata */ | 282 | curr->next = G(L)->TMtable[tag].collected; /* chain udata */ |
284 | G(L)->TMtable[tag].collected = next; | 283 | G(L)->TMtable[tag].collected = curr; |
285 | } | 284 | } |
286 | } | 285 | } |
287 | } | 286 | } |
@@ -291,17 +290,17 @@ static void collectstrings (lua_State *L, int all) { | |||
291 | int i; | 290 | int i; |
292 | for (i=0; i<G(L)->strt.size; i++) { /* for each list */ | 291 | for (i=0; i<G(L)->strt.size; i++) { /* for each list */ |
293 | TString **p = &G(L)->strt.hash[i]; | 292 | TString **p = &G(L)->strt.hash[i]; |
294 | TString *next; | 293 | TString *curr; |
295 | while ((next = *p) != NULL) { | 294 | while ((curr = *p) != NULL) { |
296 | if (next->marked && !all) { /* preserve? */ | 295 | if (curr->marked && !all) { /* preserve? */ |
297 | if (next->marked < FIXMARK) /* does not change FIXMARKs */ | 296 | if (curr->marked < FIXMARK) /* does not change FIXMARKs */ |
298 | next->marked = 0; | 297 | curr->marked = 0; |
299 | p = &next->nexthash; | 298 | p = &curr->nexthash; |
300 | } | 299 | } |
301 | else { /* collect */ | 300 | else { /* collect */ |
302 | *p = next->nexthash; | 301 | *p = curr->nexthash; |
303 | G(L)->strt.nuse--; | 302 | G(L)->strt.nuse--; |
304 | luaM_free(L, next, sizestring(next->len)); | 303 | luaM_free(L, curr, sizestring(curr->len)); |
305 | } | 304 | } |
306 | } | 305 | } |
307 | } | 306 | } |
@@ -365,11 +364,12 @@ void luaC_collect (lua_State *L, int all) { | |||
365 | 364 | ||
366 | void luaC_collectgarbage (lua_State *L) { | 365 | void luaC_collectgarbage (lua_State *L) { |
367 | markall(L); | 366 | markall(L); |
368 | invalidatetables(G(L)); | 367 | cleartables(G(L)); |
369 | luaC_collect(L, 0); | 368 | luaC_collect(L, 0); |
370 | checkMbuffer(L); | 369 | checkMbuffer(L); |
371 | G(L)->GCthreshold = 2*G(L)->nblocks; /* set new threshold */ | 370 | G(L)->GCthreshold = 2*G(L)->nblocks; /* temporary threshold (for TM) */ |
372 | luaC_callgcTMudata(L); | 371 | luaC_callgcTMudata(L); |
372 | G(L)->GCthreshold = 2*G(L)->nblocks; /* new threshold */ | ||
373 | callgcTM(L, &luaO_nilobject); | 373 | callgcTM(L, &luaO_nilobject); |
374 | } | 374 | } |
375 | 375 | ||