On Sat Mar 16 09:44:22 2024 +0000, Jinoh Kang wrote:
This will scan the entire objects array once the last index has been occupied (object_count full). Instead, how about replacing `object_count` with `last_object_index`? This field doesn't actually count valid objects anyway, since some objects in the middle could be freed later. Instead, I would keep track of the *last* object index and start from there.
unsigned int index = session.last_object_index; for (;;) { index = (index + 1) % session.shared->object_capacity; if (!session.shared->objects[index].obj.id) { /* found a free index */ break; } /* check if we're back to the starting point */ if (index == session.last_object_index) { /* all slots exhausted; skip to first index to be allocated */ index = session.shared->object_capacity; break; } } while (index >= session.shared->object_capacity) { if (grow_session_mapping()) return -1; } session.last_object_index = index; SHARED_WRITE_BEGIN( &session.shared->objects[index], session_obj_t ) { assert(!shared->obj.id); shared->obj.id = ++session.last_object_id; } SHARED_WRITE_END;
Note that the following initialization is also required:
session.last_object_index = -1;
I've changed it to something similar, though mostly because it makes the code slightly simpler. I don't see what difference it makes as looking from a free index starting from the previous one doesn't seem obviously more efficient than from the beginning.