mercredi 28 juillet 2021

Big projects that require very good performance really don't use polymorphism?

For quite some time I have been interested in perfomance in C ++.

A lot of things keep coming up, whether in conferences or in books:

Do not use a virtual function, have the data in the cache, the branches etc.

There are many benchmarks with examples of video games to show the differences in performance. The thing is, the examples are always very simple.

How does that really work in code that is more than 20 lines? in AAA video games, finance etc.

If I have 100 types of objects that have different update, different behavior and other joys, it's easy to set up via polymorphism or function pointers.

Now, by following the advice given to make a powerful code, the above options are not possible. We will therefore prefer to have 100 arrays that we will update separately. We will have good access to the cache, the functions will probably be inline etc. in short, the performance will in principle be better.

So, I have 100 arrays and 100 differents functions that i will have to call at each frame. The tables dynamically change depending on what happens, new players appear, a monster dies etc. Some array will have 0 elements active, others 10... I would call functions that will have no work (array without active element) but I have no choice, I have to have a flag or look if elements are active or not in my array.

I end up with something like this:

obj1update ();
obje2update ();
....

obj1behavior ();
obj2behavior ();
....

obj1render ();
obj2render ();
.....

objectxy ();
....

Of course, there will undoubtedly be a function which manages all the update calls on the objects, one for the behavior etc but to simplify it gives as above.

To stay in the video game, if the x action of a player causes the arrival of y monsters, there are several types of monsters which have different functions. We will therefore put the monsters in our tables and this time the functions dealing with these monsters will have work.

We can use the ECS pattern or a derivative but they will potentially have very different behaviors (an ia that directs them or other), different components and therefore different functions will be needed to process them. They will be called hard in the code since we don't have polymorphism or function pointers and we will have to check at each frame if they have something to process or not.

Is it really done that way? suppose i have 500 types? 1000 ?

Aucun commentaire:

Enregistrer un commentaire