@@ -18,53 +18,49 @@ Thread-safe, lock-free generic object pooling for Unity using CAS operations. Wo
1818### Constructor
1919``` csharp
2020new JObjectPool <T >(
21- Func < T > createFunc = null , // Factory (defaults to Activator.CreateInstance<T>() )
22- Action < T > onRent = null , // Called when renting
23- Action < T > onReturn = null // Called when returning
21+ int maxSize = 64 , // Maximum pooled objects (excess discarded )
22+ Action < T > onRent = null , // Called when renting (for initialization)
23+ Action < T > onReturn = null // Called when returning (for cleanup)
2424 )
2525```
26+ Note: ` T ` must be a reference type with a parameterless constructor (` where T : class, new() ` ).
2627
2728### Methods
2829- ` .Rent() ` - Get object from pool or create new
29- - ` .Return(T obj) ` - Return object to pool
30+ - ` .Return(T obj) ` - Return object to pool (null values ignored)
3031- ` .Clear() ` - Remove all pooled objects
31- - ` .Prewarm(int count) ` - Pre-allocate objects
32- - ` .Count ` - Current available count
32+ - ` .Prewarm(int count) ` - Pre-allocate objects (won't exceed maxSize)
33+ - ` .Count ` - Current available count (approximate, thread-safe)
3334
3435### Static Access
35- - ` JObjectPool.Shared<T>() ` - Global shared pool per type (simple objects only )
36+ - ` JObjectPool.Shared<T>() ` - Global shared pool per type (default config: maxSize=64 )
3637
3738## Patterns
3839
3940### Basic Pool
4041``` csharp
41- var pool = new JObjectPool <Bullet >();
42+ var pool = new JObjectPool <Bullet >(maxSize : 100 );
4243var bullet = pool .Rent ();
4344// ... use bullet ...
4445pool .Return (bullet );
4546```
4647
47- ### With Custom Factory
48+ ### With Initialization on Rent
4849``` csharp
4950var pool = new JObjectPool <Enemy >(
50- createFunc : () => new Enemy (defaultHealth : 100 )
51+ maxSize : 50 ,
52+ onRent : static enemy => enemy .Reset ()
5153);
5254```
5355
5456### Reset State on Return (RECOMMENDED)
5557``` csharp
5658var pool = new JObjectPool <List <int >>(
59+ maxSize : 32 ,
5760 onReturn : static list => list .Clear ()
5861);
5962```
6063
61- ### Initialize on Rent
62- ``` csharp
63- var pool = new JObjectPool <Projectile >(
64- onRent : static p => p .Reset ()
65- );
66- ```
67-
6864### Prewarm During Loading
6965``` csharp
7066var pool = new JObjectPool <Effect >();
@@ -73,21 +69,23 @@ pool.Prewarm(50); // Pre-create during loading screen
7369
7470### Shared Pool (Simple Objects)
7571``` csharp
76- // Good for simple reusable objects without custom initialization
72+ // Good for simple reusable objects without custom callbacks
7773var sb = JObjectPool .Shared <StringBuilder >().Rent ();
7874sb .Append (" Hello" );
75+ sb .Clear (); // Clean up before returning
7976JObjectPool .Shared <StringBuilder >().Return (sb );
8077```
8178
8279## Best Practices
83801 . ** Pre-allocate during loading** to prevent in-game allocation spikes
84812 . ** Reset state on return** to prevent data leaks between reuses
85- 3 . ** Use custom pools ** for complex objects requiring specific initialization
86- 4 . ** Use shared pools only ** for simple value objects with default construction
82+ 3 . ** Set appropriate maxSize ** based on expected concurrent usage
83+ 4 . ** Use shared pools** for simple objects without custom callbacks
87845 . ** Monitor pool Count** to optimize sizing
8885
8986## Common Mistakes
9087- Returning null to pool (ignored, but wasteful)
9188- Not clearing object state on return (causes bugs from stale data)
9289- Forgetting to return objects (pool becomes ineffective)
93- - Using shared pool for complex objects needing initialization
90+ - Setting maxSize too low (causes frequent allocations)
91+ - Setting maxSize too high (wastes memory)
0 commit comments