Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pooling of user objects #5

Closed
Genbox opened this issue Apr 7, 2017 · 1 comment
Closed

Pooling of user objects #5

Genbox opened this issue Apr 7, 2017 · 1 comment

Comments

@Genbox
Copy link
Owner

Genbox commented Apr 7, 2017

Object pooling is an optimization method for reducing the load on garbage collectors. Velcro already makes heavy use of pooling for internal objects such as contacts. This proposal extends this capability to user objects such as Body, Shape and Fixture to further reduce the load on the garbage collector.

The proposal is to add an interface to all 'poolable' objects that contain a Reset() method. Each class will be responsible for providing their own implementation of Reset to make sure they have cleaned their internal state completely. Each user object will be extended with a Destroy() method as well, that can be called by the user when the object is no longer needed.

The factory classes will then be made 'pool' aware and fetch one from the pool rather than create a new object.

Advantages:

  1. Reduced load on the garbage collector since we reuse objects
  2. Faster object creation as we reuse existing objects

Disadvantages:

  1. If the user calls Destroy() on an object and uses it afterwards, it will cause havoc.
  2. The engine will use a little more memory since we keep a small number of objects always

Misc:

  1. I can make a setting for this behavior to be turned on and off. Should there be issues with bodies used after Destroy(), it can easily be determined by turning off pooling.
  2. I will probably pre-load the pool with a couple of objects on World creation. This should also be configurable.

Any comments, improvements or votes (thumbs up/down) are appreciated.

@Genbox
Copy link
Owner Author

Genbox commented Jun 20, 2021

This is too much of a challenge to implement without any unwanted behavior. For now, the engine itself will pool aggressively for internal objects, but user objects will stay in userland. For large games, I suggest pooling bodies (etc.) manually.

@Genbox Genbox closed this as completed Jun 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant