super-mario-bros icon indicating copy to clipboard operation
super-mario-bros copied to clipboard

Make the list (vector) of entities a contiguos array of objects

Open feresr opened this issue 4 years ago • 7 comments

World::entities is a vector of pointers, those pointers could be scattered in memory. Iterating over them might result in poor usage of CPU caches.

I tried changing std::vector<Entity*> entities; to std::vector<Entity> entities; but other problems arose. (Namely some systems might keep a pointer to a specific entity, the pointer becomes a dangling pointer when adding/removing items to the vector invalidates them)

I think my own inexperience with CPP is showing here, I'll revisit this when I feel more comfortable with it

feresr avatar May 31 '20 20:05 feresr

investigate: using std::list instead of std::vector might help here

Iterator Invalidation Deleting or Inserting an element in List does not invalidate any iterator because during insertion and deletion no element is moved from its position only a couple pointers are changed. Whereas, in vector insertion and deletion can invalidate the iterators.

feresr avatar Nov 09 '20 16:11 feresr

Try having a look at Entt! From their README:

EnTT is a header-only, tiny and easy to use library for game programming and much more written in modern C++, mainly known for its innovative entity-component-system (ECS) model. Among others, it's used in Minecraft by Mojang and the ArcGIS Runtime SDKs by Esri.

They might already have a solution for this.

danielfx90 avatar Nov 26 '20 15:11 danielfx90

I'm sure they do! 🙂 My goal with this small project was to learn CPP fundamentals so I try to keep the use of libraries to a minimum

feresr avatar Nov 26 '20 23:11 feresr

Oh I wasn't saying that you use the library here. I was saying that you could see how they solved this issue. Or even create an issue in their repository asking the same question

danielfx90 avatar Nov 27 '20 14:11 danielfx90

Sorry for the misunderstanding, yeah that's a good idea!

feresr avatar Nov 27 '20 18:11 feresr

I am also interested in potential solutions!

I found this article that tackles the problem. Not entirely convinced that the trade-offs are worth, but the solution is interesting: https://austinmorlan.com/posts/entity_component_system/

danielfx90 avatar Nov 28 '20 19:11 danielfx90