munificent / game-programming-patterns

Source repo for the book
http://gameprogrammingpatterns.com/
Other
4.03k stars 495 forks source link

Why would we use a pre-allocated pool of list nodes for the observers? #367

Open JensB-tech opened 2 years ago

JensB-tech commented 2 years ago

He says earlier (on p.46) in the observer chapter that you would usually use a dynamically sized collection for the observer pointers instead of a "dumb array", so that you don't have to use a compile-time constant for the max amount of observers a subject can have watching it. But then later (on p.53), he says that in order to avoid dynamically allocating the list nodes you should use a pre-allocated pool of list nodes. This confuses me, aren't we back to the same problem we had earlier when we had a fixed array of observer pointers? With a pre-allocated pool, we have to decide on a maximum number of observers for not just one subject, but for the whole program. How is that sustainable?