What are the differences and pitfalls between reusing instances of objects vs creating new ones, every time I swap buffers?
Background:
This is for a game engine project of mine.
I am writing a TripleBuffer where I have three versions of every object: an old version, a current version, and a future version. Changes on these objects will be made by reading state from the current version of it and applying changes to the future version. After changes have been made to all objects (resp. where applicable), the buffers will be swapped: the future objects become the current objects, the current objects become the old objects, and the old objects?
Explanations:
Use Case:
Say some 1000 objects are swapped at 30Hz, meaning they need to be recreated 30 times a second by either cloning the now current ones or by reusing the now obsolete last old ones (overriding all their state).
They can range in complexity from some 5 properties up to hundreds of properties and will always have a depth of at least 2 levels.
(depth of at least 2 levels = the buffered objects will themselves only contain a map of other unique objects that make them up)
Recreating as well as reusing will both require to iterate over the current objects and their components (shorter than: the objects that in turn make them up).
Further considerations:
In other parts of the engine there will be event firing and other magic that will do work using in-time snapshots of the objects. Hence the decision of recreating or reusing will lead to either:
Unless you have a very good reason to do otherwise, discard the old objects and create new ones.
This will reduce your chances for various bugs. For example, if there's a reference to the old object somewhere else, re-using it may have bad side-effects. Discarding and creating new objects is cleaner conceptually and would probably make the code easier to read, debug and maintain.
In the general case, the garbage collector is also smart enough to make discarding and re-creating object not that expensive.
What I would do in this situation is write the clearest, most straight-forward code, which means creating a new object whenever I need it. Then I would test it to see if that's a performance bottleneck, and only if it is, I would look at optimization. In other words, I would try to avoid premature optimization.
A buffer is used in Graphics so that you are not "drawing" to the screen pixel by pixel as you attempt to update it. Instead, you draw to the buffer, and then you can swap the entire image all at once.
I assume there is a similar reason why you are using a buffer. You don't wish to modify "current" objects with newly updated ones as you work through them because it will disrupt the "image" of the current objects.
Advantages of reuse:
Advantages of re-creating
Your ultimate decision will be based off performance vs clarity. How expensive is it to recreate all of your objects? Is it cheaper to compare between each of the objects and only build the ones that are needed?
Even if it is cheaper to compare objects or keep track of which have changed, it might be worth the cost of creating new just for more simplicity, which is exactly what @DaphnaShezaf's answer is about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With