diff --git a/Pooling/Pool.cs b/Pooling/Pool.cs index f1a0d4c..a839999 100644 --- a/Pooling/Pool.cs +++ b/Pooling/Pool.cs @@ -41,15 +41,17 @@ public Pool( Action? onAcquire = null, Action? onRelease = null, Action? onDestroy = null, - int maxInactive = 100) + int initialMaxInactive = 100, + bool allowResizing = true) { - if (maxInactive <= 0) + if (initialMaxInactive <= 0) { - throw new ArgumentException("Max inactive must be greater than 0", nameof(maxInactive)); + throw new ArgumentException("initialMaxInactive must be greater than 0", nameof(initialMaxInactive)); } values = new Queue(); - usageInfo.MaxInactive = maxInactive; + usageInfo.MaxInactive = initialMaxInactive; + usageInfo.AllowResizing = allowResizing; this.create = () => { @@ -73,6 +75,12 @@ public Pool( { usageInfo.DestroyCount++; onDestroy?.Invoke(value); + + if (!IsDisposed && usageInfo.AllowResizing && usageInfo.DestroyCount > (ulong)usageInfo.MaxInactive) + { + usageInfo.MaxInactive *= 2; + usageInfo.MaxInactiveResizeCount++; + } }; Pools.AddPool(this, false); diff --git a/Pooling/PoolUsageInfo.cs b/Pooling/PoolUsageInfo.cs index 737c853..91337e4 100644 --- a/Pooling/PoolUsageInfo.cs +++ b/Pooling/PoolUsageInfo.cs @@ -4,6 +4,9 @@ public struct PoolUsageInfo { public int MaxInactive; + public bool AllowResizing; + public int MaxInactiveResizeCount; + public int TotalCount; public int ActiveCount; public int InactiveCount; diff --git a/Tests/PoolTests.cs b/Tests/PoolTests.cs index b4a7b12..785e976 100644 --- a/Tests/PoolTests.cs +++ b/Tests/PoolTests.cs @@ -74,7 +74,7 @@ public void Acquire_EnforcesMaxInactiveLimit() const int acquireCountA = 20; const int maxInactive = 5; - var pool = new Pool(create: () => new A(), maxInactive: maxInactive); + var pool = new Pool(create: () => new A(), initialMaxInactive: maxInactive, allowResizing: false); var active = new List(); Assert.AreEqual(maxInactive, pool.UsageInfo.MaxInactive);