1 | # Object Pool
|
2 |
|
3 | This package implements a custom-tailored object pool for PixiJS applications. It provides the
|
4 | following features:
|
5 |
|
6 | * **reserve**: You can preallocate the pool size to have a set amount of objects.
|
7 |
|
8 | * **limit**: You can reduce the pool size after a lot of allocations.
|
9 |
|
10 | * **auto-GC**: The GC will reduce your pool to the reserve size after allocation demand goes down
|
11 | per-frame.
|
12 |
|
13 | This package is can also be used as a _single-source_ of object pools. If two different libraries need
|
14 | a pool for say, `PIXI.Rectangle`, then the same object pool will be returned.
|
15 |
|
16 | ### Analysis
|
17 |
|
18 | * https://codepen.io/sukantpal/pen/zYvBOVw: This chart shows the pool capacity and the allocations done per frame. The GC
|
19 | is enabled and reserve is set to 100,000.
|
20 |
|
21 | <img src="https://i.ibb.co/jkNWHdR/Screen-Shot-2020-04-18-at-12-46-08-PM.png"></img>
|
22 |
|
23 | * You should use auto-GC only if allocations-per-frame is smooth (slowly increase & slowly decrease) or you know the upper
|
24 | limit of objects you need per frame.
|
25 |
|
26 | ## Usage
|
27 |
|
28 | ```
|
29 | import { ObjectPoolFactory } from 'pixi-object-pool';
|
30 |
|
31 | const rpool: ObjectPoolFactory = ObjectPoolFactory.build(PIXI.Rectangle);
|
32 |
|
33 | rpool.reserve(10000);
|
34 | rpool.startGC();// prevent pool from staying above 10,000 rectangles for too long
|
35 |
|
36 | const rect: PIXI.Rectangle = rpool.allocate();
|
37 |
|
38 | // do something
|
39 |
|
40 | rpool.release(rect);
|
41 |
|
42 | // Want to reduce pool size now?
|
43 | rpool.limit(11000);
|
44 | ```
|