Skip to content

Commit a5cc509

Browse files
author
Stephen Gutekanst
committed
object: fix dead object recycling
Signed-off-by: Stephen Gutekanst <[email protected]>
1 parent c99ae4a commit a5cc509

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/module.zig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ pub fn Objects(options: ObjectsOptions, comptime T: type) type {
166166
// all objects have been thrown on the floor. If they have, we find them and grow the
167167
// recycling bin to fit them.
168168
if (objs.internal.thrown_on_the_floor >= (data.len / 10)) {
169-
var iter = dead.iterator(.{});
170-
while (iter.next()) |index| try recycling_bin.append(allocator, @intCast(index));
169+
var iter = dead.iterator(.{ .kind = .set });
170+
while (iter.next()) |index| {
171+
// dead bitset contains data.capacity number of entries, we only care about ones that are in data.len range.
172+
if (index > data.len - 1) break;
173+
try recycling_bin.append(allocator, @intCast(index));
174+
}
171175
objs.internal.thrown_on_the_floor = 0;
172176
}
173177

@@ -176,6 +180,7 @@ pub fn Objects(options: ObjectsOptions, comptime T: type) type {
176180
dead.unset(index);
177181
const gen = generation.items[index] + 1;
178182
generation.items[index] = gen;
183+
data.set(index, value);
179184
return @bitCast(PackedID{
180185
.type_id = objs.internal.type_id,
181186
.generation = gen,
@@ -185,7 +190,7 @@ pub fn Objects(options: ObjectsOptions, comptime T: type) type {
185190

186191
// Ensure we have space for the new object
187192
try data.ensureUnusedCapacity(allocator, 1);
188-
try dead.resize(allocator, data.capacity, true);
193+
try dead.resize(allocator, data.capacity, false);
189194
try generation.ensureUnusedCapacity(allocator, 1);
190195

191196
// If we are tracking fields, we need to resize the bitset to hold another object's fields

0 commit comments

Comments
 (0)