Skip to content

Comments

feat: Add object pooling support with Poolable mixin and ComponentPool#3816

Open
ufrshubham wants to merge 9 commits intomainfrom
devkage/object-pooling
Open

feat: Add object pooling support with Poolable mixin and ComponentPool#3816
ufrshubham wants to merge 9 commits intomainfrom
devkage/object-pooling

Conversation

@ufrshubham
Copy link
Member

Description

This PR introduces object pooling functionality to Flame through the new Poolable mixin and ComponentPool class. Object pooling is a performance optimization technique that reduces garbage collection pressure by reusing component instances instead of creating and destroying them repeatedly.

Adds a Poolable mixin that provides a reset() method for components to return to their initial state, and a ComponentPool<T> class that manages pooled components with configurable sizing, automatic lifecycle handling, and LIFO behavior.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

NA

Make the pool's maximum size a public final field (maxSize) instead of a private _maxSize, and initialize it via the constructor. Add availableCount getter to report how many components are available. Simplify factory usage by removing the _create helper and calling the factory directly when populating or acquiring.
@ufrshubham ufrshubham self-assigned this Feb 22, 2026
Copy link
Member

@spydon spydon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! A few comments on the implementation.
As a follow-up it would be nice to be able to take a pool as an argument in the SpawnComponent.

double damage = 10.0;
@override
void reset() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just tell the users to do this in onMount, then we don't need a new method?

///
/// Note: The pool does not automatically manage the lifecycle of components, so
/// it is the responsibility of the developer to ensure that components are
/// properly released back to the pool when they are no longer needed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just listen to removed and add them back to the pool when that happens?

/// ```
/// This mixin is intended to be used with a [ComponentPool] to manage the
/// lifecycle of poolable components efficiently.
mixin Poolable on Component {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we tell the users to use onMount, this mixin shouldn't be needed and all components would be able to be poolable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants