1

So I have a question about ECS and how to structure it in a way that's performant in C++. Say I want to have components that are just data, and systems that are just logic, and I want to execute all of the Update functions for all of the systems, passing them a list of component tuples to operate on.

Would having the systems submit their Update method delegates as function pointers to an update list be more performant than adding the systems themselves to that list and using polymorphism to call a virtual Update method? Or is it indifferent in terms of what option I go with?

I was also wondering if anything changes if say, this is implemented in C# for example with delegates/Actions.

4
  • Performance is in no way a concern of delegates. You should at least try something and see if it fits your needs. We can't give you general support for abstract ideas. Commented Jun 28, 2024 at 14:05
  • Apart from delegates and polymorphsim are completly different things. Commented Jun 28, 2024 at 14:09
  • 1
    you won't measure a difference in performance, you should avoid virtual dispatch for things that happen like a few hundred thousand times per frame (like collision testing, particle physics, decal drawing, etc...) , for running systems which you can only have tens or hundreds of , the performance difference is not measurable, so just go for whatever looks nicer, by not measureable i mean it won't pop up in whatever perf tool you use, it is almost negligible compared to other things. Commented Jun 28, 2024 at 14:55
  • @MakePeaceGreatAgain well yes, they're of course different things but they're two ways to solve the same problem in this case. Which is why I was asking. Commented Jun 28, 2024 at 17:30

1 Answer 1

4

The reason polymorphism would be slow (cliché) is that the v-table lookup requires reading a pointer in an unrelated (to your entity's data) memory location. The page fault this may cause could cause a context switch to the OS, if it needs to look up new pages.

That applies when you have very tightly optimized code and put lots of care into what data gets loaded when. For example, if you were doing high-frequency-trading or hard real-time robot control, that would apply.

In a game, where you'll have a bunch of different threads for different subsystem and a bunch of engine code doing various things, the overhead of a function pointer, polymorphism or even delegate will be pretty similar. They all incur extra indirection. The only difference is where the extra pointers will be stored, and whether those will be already loaded or not.

The only sane suggestion is: profile to confirm. But in all likelihood, whatever is simpler and cleaner will be the best approach.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.