gc: Speed up new_object by adding new blocks to the front of the list

This way we don't have to traverse the whole list to the very end every
time we want a new object.
This commit is contained in:
Laria 2023-03-21 20:54:09 +01:00
parent 906ae3eac4
commit 8233e94ab3

View file

@ -124,17 +124,17 @@ new_block(struct gc *gc)
static struct gc_object *
new_object_inner(struct gc *gc)
{
struct gc_block **cur = &gc->block;
struct gc_block *cur = gc->block;
while (*cur != NULL) {
struct gc_block *block = *cur;
while (cur != NULL) {
struct gc_block *block = cur;
for (size_t i = 0; i < GC_OBJECTS_PER_BLOCK; i++) {
if (block->objects[i].status == GC_STATUS_FREE) {
return &block->objects[i];
}
}
cur = &block->next;
cur = block->next;
}
struct gc_block *nb = new_block(gc);
@ -142,7 +142,8 @@ new_object_inner(struct gc *gc)
return NULL;
}
*cur = nb;
nb->next = gc->block;
gc->block = nb;
return &nb->objects[0];
}