Skip to content

Change entity recycling to use pop #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions packages/classic/src/entity/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,23 @@ export const getGlobalSize = () => globalSize;

// removed eids should also be global to prevent memory leaks
const removed: number[] = [];
const removedOut: number[] = [];
const recycled: number[] = [];

const dequeuFromRemoved = () => {
if (removedOut.length === 0) {
while (removed.length > 0) {
removedOut.push(removed.pop()!);
}
}
if (removedOut.length === 0) {
throw new Error('Queue is empty');
}
return removedOut.pop()!;
};

const getRemovedLength = () => removed.length + removedOut.length;

const defaultRemovedReuseThreshold = 0.01;
let removedReuseThreshold = defaultRemovedReuseThreshold;

Expand All @@ -48,6 +63,7 @@ export const resetGlobals = () => {
globalEntityCursor = 0;
removedReuseThreshold = defaultRemovedReuseThreshold;
removed.length = 0;
removedOut.length = 0;
recycled.length = 0;
queries.length = 0;
worlds.length = 0;
Expand Down Expand Up @@ -104,13 +120,17 @@ export const Prefab = defineComponent();
* @returns {number} eid
*/
export const addEntity = (world: World): number => {
const eid: number = world[$manualEntityRecycling]
? removed.length
? removed.shift()!
: globalEntityCursor++
: removed.length > Math.round(globalSize * removedReuseThreshold)
? removed.shift()!
: globalEntityCursor++;
let eid: number;

if (
(world[$manualEntityRecycling] && getRemovedLength() > 0) ||
(!world[$manualEntityRecycling] &&
getRemovedLength() > Math.round(globalSize * removedReuseThreshold))
) {
eid = dequeuFromRemoved();
} else {
eid = globalEntityCursor++;
}

if (world[$entitySparseSet].dense.length >= world[$size]) {
throw new Error('bitECS - max entities reached');
Expand Down Expand Up @@ -177,9 +197,9 @@ export const removeEntity = (world: World, eid: number) => {

// Remove entity from all queries
// TODO: archetype graph
world[$queries].forEach((q) => {
queryRemoveEntity(world, q, eid);
});
for (const query of world[$queries]) {
queryRemoveEntity(world, query, eid);
}

// Free the entity
if (world[$manualEntityRecycling]) recycled.push(eid);
Expand Down
3 changes: 3 additions & 0 deletions packages/classic/test/integration/Entity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Entity Integration Tests', () => {
strictEqual(removed[1], 1);
strictEqual(removed[2], 2);
});

it('should recycle entity IDs after 1% have been removed by default', () => {
const world = createWorld();
const ents: number[] = [];
Expand Down Expand Up @@ -76,6 +77,7 @@ describe('Entity Integration Tests', () => {
eid = addEntity(world);
strictEqual(eid, 0);
});

it('should flush entity IDs', () => {
const world = createWorld();
enableManualEntityRecycling(world);
Expand Down Expand Up @@ -121,6 +123,7 @@ describe('Entity Integration Tests', () => {
eid = addEntity(world);
strictEqual(eid, 5);
});

it('should be able to configure % of removed entity IDs before recycle', () => {
const world = createWorld();

Expand Down