Reusing instances of objects and creating new ones with each update

What are the differences and traps between reusing instances of objects and creating new ones every time I replace buffers?

Background:

This is for my game engine.

I write TripleBuffer, where I have three versions of each object: the old version, the current version and the future version. Changes to these objects will be made by reading the state from the current version and making changes to the future version. After making changes to all objects (respectively, if applicable), the buffers will be replaced: future objects become current objects, current objects become old objects and old objects?

  • either discarded, and new objects will be allocated by iterating over the current objects
  • or become future future objects, and their values ​​will be updated (respectively redefined) by iterating over the current current objects.

Explanations:

  • reuse: overwriting the values ​​of the old instance with the new values, effectively changing the state of the instance
  • "new ones / cloning": create a new instance using the data from the old instance and make changes to it

Use Case:

Let's say that about 1000 objects are exchanged at 30 Hz, which means that they need to be recreated 30 times per second, either by cloning the current ones or reusing the old obsolete old ones (redefining their entire state).

They can vary in complexity from 5 properties to hundreds of properties and will always have a depth of at least 2 levels.

(depth not less than 2 levels = buffered objects themselves will contain only a map of other unique objects that make them up)

Recreation as well as reuse will require iterating over current objects and their components (in short: objects that, in turn, create them).

Further considerations:

Other parts of the engine will trigger events and other magic that will work using snapshots at a point in time. Therefore, a decision to recreate or reuse will result in either:

  • recreating means that I can safely pass references to an object since the object will become immutable the moment it becomes the current object.
  • Reuse means that I will have to create copies of the current object or any part of it that I need, because I cannot guarantee that the event (or other) will be processed before the object is reused.
+5
source share
2 answers

If you have no reason to do so, discard the old objects and create new ones.

This will reduce your chances of various errors. For example, if there is a link to an old object somewhere else, reuse can have bad side effects. Dropping and creating new objects is more conceptual and likely to make the code easier to read, debug, and maintain.

In general, the garbage collector is also smart enough to discard and recreate an object not so expensive.

What would I do in this situation, write the clearest, most direct code, which means creating a new object whenever I need it. Then I would check it to see if this is a performance bottleneck, and only if so, would I look at the optimization. In other words, I will try to avoid premature optimization .

+7
source

Graphics uses a buffer so that you don't draw a pixel pixel as it updates. Instead, you draw into the buffer, and then you can immediately change the overall image.

I assume there is a similar reason why you are using a buffer. You do not want to change the "current" objects with recently updated ones when working with them, because this will violate the "image" of the current objects.

Reuse Benefits:

  • You do not throw away so much memory, but consume less garbage
  • You only need to update the modified objects.

Recreation Benefits

  • Each object must be created.
  • Simpler and cleaner

Your final decision will be based on performance and clarity. How expensive is it to recreate all your objects? Is it cheaper to compare each of the objects and build only those that you need?

Even if it’s cheaper to compare objects or track changes, it may be worth the cost of creating new ones just for the sake of simplicity, which is exactly what @DaphnaShezaf answers about.

+1
source

All Articles