Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Microsoft.Extensions.ObjectPool/DefaultObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class DefaultObjectPool<T> : ObjectPool<T> where T : class
private readonly bool _isDefaultPolicy;
private T _firstItem;

// This class was introduced in 2.1 to avoid the interface call where possible
private readonly PooledObjectPolicy<T> _fastPolicy;

public DefaultObjectPool(IPooledObjectPolicy<T> policy)
: this(policy, Environment.ProcessorCount * 2)
{
Expand All @@ -23,6 +26,7 @@ public DefaultObjectPool(IPooledObjectPolicy<T> policy)
public DefaultObjectPool(IPooledObjectPolicy<T> policy, int maximumRetained)
{
_policy = policy ?? throw new ArgumentNullException(nameof(policy));
_fastPolicy = policy as PooledObjectPolicy<T>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is worth a comment, explaining that this base class was introduced in 2.1 to avoid the interface call

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, now I added the comment above. Is the intention still clear or should I move the comment to this line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

_isDefaultPolicy = IsDefaultPolicy();

// -1 due to _firstItem
Expand Down Expand Up @@ -64,12 +68,16 @@ private T GetViaScan()
}
}

return item ?? _policy.Create();
return item ?? Create();
}

// Non-inline to improve its code quality as uncommon path
[MethodImpl(MethodImplOptions.NoInlining)]
private T Create() => _fastPolicy?.Create() ?? _policy.Create();

public override void Return(T obj)
{
if (_isDefaultPolicy || _policy.Return(obj))
if (_isDefaultPolicy || (_fastPolicy?.Return(obj) ?? _policy.Return(obj)))
{
if (_firstItem != null || Interlocked.CompareExchange(ref _firstItem, obj, null) != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

namespace Microsoft.Extensions.ObjectPool
{
public class DefaultPooledObjectPolicy<T> : IPooledObjectPolicy<T> where T : class, new()
public class DefaultPooledObjectPolicy<T> : PooledObjectPolicy<T> where T : class, new()
{
public T Create()
public override T Create()
{
return new T();
}

// DefaultObjectPool<T> doesn't call 'Return' for the default policy.
// So take care adding any logic to this method, as it might require changes elsewhere.
public bool Return(T obj)
public override bool Return(T obj)
{
return true;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Microsoft.Extensions.ObjectPool/PooledObjectPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Extensions.ObjectPool
{
public abstract class PooledObjectPolicy<T> : IPooledObjectPolicy<T>
{
public abstract T Create();

public abstract bool Return(T obj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

namespace Microsoft.Extensions.ObjectPool
{
public class StringBuilderPooledObjectPolicy : IPooledObjectPolicy<StringBuilder>
public class StringBuilderPooledObjectPolicy : PooledObjectPolicy<StringBuilder>
{
public int InitialCapacity { get; set; } = 100;

public int MaximumRetainedCapacity { get; set; } = 4 * 1024;

public StringBuilder Create()
public override StringBuilder Create()
{
return new StringBuilder(InitialCapacity);
}

public bool Return(StringBuilder obj)
public override bool Return(StringBuilder obj)
{
if (obj.Capacity > MaximumRetainedCapacity)
{
Expand Down
16 changes: 16 additions & 0 deletions test/Microsoft.Extensions.ObjectPool.Test/DefaultObjectPoolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ namespace Microsoft.Extensions.ObjectPool.Test
{
public class DefaultObjectPoolTest
{
[Fact]
public void DefaultObjectPoolWithDefaultPolicy_GetAnd_ReturnObject_SameInstance()
{
// Arrange
var pool = new DefaultObjectPool<object>(new DefaultPooledObjectPolicy<object>());

var obj1 = pool.Get();
pool.Return(obj1);

// Act
var obj2 = pool.Get();

// Assert
Assert.Same(obj1, obj2);
}

[Fact]
public void DefaultObjectPool_GetAndReturnObject_SameInstance()
{
Expand Down