entityx: Entity creation / destruction slows down over time?

I’ve found a very odd situation that I’m trying to troubleshoot right now using entityx.

I’m using OpenCV to detect and track objects from frame-to-frame in a video, and then using entityx to manage the physics of each tracked object. I’ve found that as my project runs, the framerate drops – it starts at 60 fps, then within 30 minutes it’s dropped down to 48 or so, and by the 90 minute mark it’s barely hitting 20 fps. After several rounds of surgically removing / adding code back into the project to identify the issue, I’ve found that the creation / destruction of objects completely slows down the software:

void setup(void* display) {
	Display* parentDisplay = static_cast<Display*>(display);
	mEntityHandle = parentDisplay->mEntities.create();
	mEntityHandle.assign<Id>(-1);
}

void update(void* display) {
	Display* parentDisplay = static_cast<Display*>(display);

	if (mEntityHandle.valid()) {
		auto id_component = mEntityHandle.component<Id>();
		id_component->mId = getLabel();
	}
}

void destroy(void* display) {
	Display* parentDisplay = static_cast<Display*>(display);
	entityx::ComponentHandle<sitara::Id> id;
	//entityx::ComponentHandle<sitara::ecs::Attractor> force;
	for (auto entity : parentDisplay->mEntities.entities_with_components(id)) {
		if (id->mId == getLabel()) {
			entity.destroy();
		}
	}
}

These three functions are simply hooks that are called by my object tracker, and input a void* display that represents the parent class of my entityx::EntityManager objects. But I’ve found that removing these few lines of code removes my slowdown problem completely, and re-adding them causes the issue I’m observing.

So my question is: what could possibly be causing this? While running in VS2019 I don’t see any memory leaks (memory usage looks pretty steady); it could be that there’s a very expensive memory alignment operations that occurs somewhere due to the constant destruction / creation of entities, but if so, I’m not sure how to compensate for it.

For further troubleshooting information, my Id Component is very simple:

	struct Id {
		Id(int id) {
			mId = id;
		}

		int mId;
	};

So I doubt that there’s any issues with this being a very expensive operation…

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

@alecthomas Wild! I have no idea what’s going on here, but it seems clear that it’s on my end… thanks for the troubleshooting help, and at least helping me narrow down where the issue might be.

FWIW I ran the example for an hour and didn’t see any slowdown at all, still a solid ~400fps 🤷‍♂️