Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Try to devirtualize ArrayPool.Shared
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jan 3, 2018
1 parent 0950d37 commit dea46f4
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/mscorlib/shared/System/Buffers/ArrayPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;

namespace System.Buffers
{
internal static class ArrayPools
{
private static TlsOverPerCoreLockedStacksArrayPool<byte> s_bytePool = new TlsOverPerCoreLockedStacksArrayPool<byte>();
private static TlsOverPerCoreLockedStacksArrayPool<char> s_charPool = new TlsOverPerCoreLockedStacksArrayPool<char>();

internal static TlsOverPerCoreLockedStacksArrayPool<byte> BytePool => s_bytePool;
internal static TlsOverPerCoreLockedStacksArrayPool<char> CharPool => s_charPool;
}
/// <summary>
/// Provides a resource pool that enables reusing instances of type <see cref="T:T[]"/>.
/// </summary>
Expand All @@ -19,6 +29,7 @@ namespace System.Buffers
/// </remarks>
public abstract class ArrayPool<T>
{
private static ConfigurableArrayPool<T> s_generalPool = typeof(T) == typeof(byte) || typeof(T) == typeof(char) ? null : new ConfigurableArrayPool<T>();
/// <summary>
/// Retrieves a shared <see cref="ArrayPool{T}"/> instance.
/// </summary>
Expand All @@ -33,9 +44,25 @@ public abstract class ArrayPool<T>
/// optimized for very fast access speeds, at the expense of more memory consumption.
/// The shared pool instance is created lazily on first access.
/// </remarks>
public static ArrayPool<T> Shared { get; } =
typeof(T) == typeof(byte) || typeof(T) == typeof(char) ? new TlsOverPerCoreLockedStacksArrayPool<T>() :
Create();
public static ArrayPool<T> Shared
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (typeof(T) == typeof(byte))
{
return (ArrayPool<T>)(object)ArrayPools.BytePool;
}
else if (typeof(T) == typeof(char))
{
return (ArrayPool<T>)(object)ArrayPools.CharPool;
}
else
{
return s_generalPool;
}
}
}

/// <summary>
/// Creates a new <see cref="ArrayPool{T}"/> instance using default configuration options.
Expand Down

0 comments on commit dea46f4

Please sign in to comment.