ropool
    Preparing search index...

    Class ObjectPool<T>

    Manages a pool of reusable objects of type T. This helps reduce the overhead of creating and garbage collecting objects by recycling instances.

    Type Parameters

    • T

      The type of objects managed by the pool. ObjectPool

    Index

    Constructors

    • Creates an instance of ObjectPool.

      Type Parameters

      • T

      Parameters

      • createObject: () => T

        A factory function that returns a new instance of the object type T. This function is called when the pool needs to create new objects.

      • OptionalinitialLength: number = 8

        The initial number of objects to create in the pool. If 0 is provided, the pool will default to a minimum growth size (currently 8) on the first acquire.

      Returns ObjectPool<T>

      // Create a pool for Vector2 objects with an initial size of 16
      const vectorPool = new ObjectPool(() => ({ x: 0, y: 0 }), 16);

      // Create a pool with default initial size (8)
      const defaultPool = new ObjectPool(() => ({ id: 0 }));

    Methods

    • Acquires an object handle from the pool.

      Returns ObjectHandle<T>

      An ObjectHandle wrapping the acquired object.

      If the pool is exhausted (at MAX_POOL_SIZE) and cannot provide a new object.

      const handle = pool.acquire();
      const obj = handle.data; // Access the object
    • Internal

      Releases an object handle back to the pool using its ID. This method is typically called internally by ObjectHandle#free. It pushes the object's ID back onto the stack of available IDs.

      Parameters

      • id: number

        The ID of the object handle to release. Intended for use by ObjectHandle#free. Direct use is discouraged.

      Returns void

    • Releases all acquired objects back to the pool. This iterates through all objects managed by the pool, marks their handles as released, and resets the internal stack and pointer so that all objects are available for re-acquisition in their original order (0, 1, 2, ...). Note: This does NOT reset the state of the objects themselves.

      Returns void

      pool.releaseAll(); // All objects previously acquired are now available