Flags versus virtual Functions

Let me first start off with saying even though the back-end for the blog system was already implemented a long time ago, I didn't use it but from now on I'll try to add a new blogpost every time I found something worth talking about.

When I was busy working on my 2D engine, I was toying around with a different implementation of the ECS when suddenly I realized that a component exists of a lot of virtual functions that can have an empty body. However I didn't know if the compiler would do some black magic and filter this out so I went ahead and tested it. As you could have guessed this does have an impact.

After that conclusion I started a quick search on the world wide web, and found some discussions, but no distinct better way. After that I started toying with possible solutions and found that when u store a byte and set every bit on it to correlate if a virtual function is implemented or not, u can use a simple and operation to call the implemented function or skip it all together.

After some tests I found that in 80 to 90 percent of the cases that's the case and since that's code in the hot path of the program u may consider using an extra byte of memory per component instead of losing some performance.

What's next? Since I use 1 byte already and the most common alignment method by the compiler is 4 bytes I'll probably make a 4 byte variable where I use 1 byte for flags and the other 3 bytes for custom smart pointers that point to a object pool of some sorts. But this idea is still very rough and I will probably start developing this early August since now most of my time goes into my holiday job and programming some other project. I'll also probably post the tests and a sample program in the next blogpost.

UPDATE:Here is the example code of Virtual Functions vs Flags, this was tested on 3 different PC's with varying results although all positive. If you have different results, I would like to hear them.

Till next time.