Skip to content

Commit

Permalink
Use a separate pool
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Feb 5, 2025
1 parent bb10dfd commit 53b67f1
Showing 1 changed file with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Microsoft.CodeAnalysis.PooledObjects;

internal sealed partial class ArrayBuilder<T> : IPooled
{
private static readonly ObjectPool<ArrayBuilder<T>> s_keepLargeInstancesPool = CreatePool();

public static PooledDisposer<ArrayBuilder<T>> GetInstance(out ArrayBuilder<T> instance)
=> GetInstance(discardLargeInstances: true, out instance);

Expand All @@ -23,28 +25,24 @@ public static PooledDisposer<ArrayBuilder<T>> GetInstance(int capacity, T fillWi

public static PooledDisposer<ArrayBuilder<T>> GetInstance(bool discardLargeInstances, out ArrayBuilder<T> instance)
{
instance = GetInstance();
// If we're discarding large instances (the default behavior), then just use the normal pool. If we're not, use
// a specific pool so that *other* normal callers don't accidentally get it and discard it.
instance = discardLargeInstances ? GetInstance() : s_keepLargeInstancesPool.Allocate();
return new PooledDisposer<ArrayBuilder<T>>(instance, discardLargeInstances);
}

void IPooled.Free(bool discardLargeInstances)
{
var pool = _pool;
if (pool != null)
// If we're discarding large instances, use the default behavior (which already does that). Otherwise, always
// clear and free the instance back to its originating pool.
if (discardLargeInstances)
{
if (!discardLargeInstances || _builder.Capacity < PooledArrayLengthLimitExclusive)
{
if (this.Count != 0)
{
this.Clear();
}

pool.Free(this);
}
else
{
pool.ForgetTrackedObject(this);
}
Free();
}
else
{
this.Clear();
_pool?.Free(this);
}
}
}

0 comments on commit 53b67f1

Please sign in to comment.