diff options
Diffstat (limited to 'src/mscorlib/corefx/System/Buffers')
5 files changed, 12 insertions, 109 deletions
diff --git a/src/mscorlib/corefx/System/Buffers/ArrayPool.cs b/src/mscorlib/corefx/System/Buffers/ArrayPool.cs index 441e48dab4..77a07f7fa5 100644 --- a/src/mscorlib/corefx/System/Buffers/ArrayPool.cs +++ b/src/mscorlib/corefx/System/Buffers/ArrayPool.cs @@ -29,26 +29,13 @@ namespace System.Buffers /// array than was requested. Renting a buffer from it with <see cref="Rent"/> will result in an /// existing buffer being taken from the pool if an appropriate buffer is available or in a new /// buffer being allocated if one is not available. + /// byte[] and char[] are the most commonly pooled array types. For these we use a special pool type + /// 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 => SharedPool.Value; - - /// <summary>Stores a cached pool instance for T[].</summary> - /// <remarks> - /// Separated out into a nested class to enable lazy-initialization of the pool provided by - /// the runtime, only forced when Shared is used (and not when Create is called or when - /// other non-Shared accesses happen). - /// </remarks> - private static class SharedPool - { - /// <summary>Per-type cached pool.</summary> - /// <remarks> - /// byte[] and char[] are the most commonly pooled array types. For these we use a special pool type - /// optimized for very fast access speeds, at the expense of more memory consumption. - /// </remarks> - internal readonly static ArrayPool<T> Value = - typeof(T) == typeof(byte) || typeof(T) == typeof(char) ? new TlsOverPerCoreLockedStacksArrayPool<T>() : - Create(); - } + public static ArrayPool<T> Shared { get; } = + typeof(T) == typeof(byte) || typeof(T) == typeof(char) ? new TlsOverPerCoreLockedStacksArrayPool<T>() : + Create(); /// <summary> /// Creates a new <see cref="ArrayPool{T}"/> instance using default configuration options. diff --git a/src/mscorlib/corefx/System/Buffers/ConfigurableArrayPool.cs b/src/mscorlib/corefx/System/Buffers/ConfigurableArrayPool.cs index 1e0e769530..f7b6034d20 100644 --- a/src/mscorlib/corefx/System/Buffers/ConfigurableArrayPool.cs +++ b/src/mscorlib/corefx/System/Buffers/ConfigurableArrayPool.cs @@ -70,7 +70,7 @@ namespace System.Buffers { // No need for events with the empty array. Our pool is effectively infinite // and we'll never allocate for rents and never store for returns. - return EmptyArray<T>.Value; + return Array.Empty<T>(); } var log = ArrayPoolEventSource.Log; diff --git a/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.Unix.cs b/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.Unix.cs deleted file mode 100644 index 8a1d006b12..0000000000 --- a/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.Unix.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Microsoft.Win32; -using System.Runtime.CompilerServices; - -namespace System.Buffers -{ - internal sealed partial class TlsOverPerCoreLockedStacksArrayPool<T> - { - /// <summary>Get an identifier for the current thread to use to index into the stacks.</summary> - private static int ExecutionId - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - // On Unix, CurrentProcessorNumber is implemented in terms of sched_getcpu, which - // doesn't exist on all platforms. On those it doesn't exist on, GetCurrentProcessorNumber - // returns -1. As a fallback in that case and to spread the threads across the buckets - // by default, we use the current managed thread ID as a proxy. - int id = CurrentProcessorNumber; - if (id < 0) id = Environment.CurrentManagedThreadId; - return id; - } - } - } -} diff --git a/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.Windows.cs b/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.Windows.cs deleted file mode 100644 index d42242c910..0000000000 --- a/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.Windows.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using Microsoft.Win32; -using System.Runtime.CompilerServices; -using System.Threading; - -namespace System.Buffers -{ - internal sealed partial class TlsOverPerCoreLockedStacksArrayPool<T> : ArrayPool<T> - { - /// <summary>Get an identifier for the current thread to use to index into the stacks.</summary> - private static int ExecutionId - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return CurrentProcessorNumber; } - } - } -} diff --git a/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs b/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs index debc33615f..64c5cebe85 100644 --- a/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs +++ b/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs @@ -45,11 +45,6 @@ namespace System.Buffers /// <summary>A per-thread array of arrays, to cache one array per array size per thread.</summary> [ThreadStatic] private static T[][] t_tlsBuckets; - /// <summary> - /// Cached processor number used as a hint for which per-core stack to access. - /// </summary> - [ThreadStatic] - private static int? t_cachedProcessorNumber; /// <summary>Initialize the pool.</summary> public TlsOverPerCoreLockedStacksArrayPool() @@ -72,22 +67,6 @@ namespace System.Buffers /// <summary>Gets an ID for the pool to use with events.</summary> private int Id => GetHashCode(); - /// <summary>Gets the processor number associated with the current thread.</summary> - /// <remarks>Uses a cached value if one exists on the current thread.</remarks> - private static int CurrentProcessorNumber - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - int? num = t_cachedProcessorNumber; - if (!num.HasValue) - { - t_cachedProcessorNumber = num = Environment.CurrentProcessorNumber; - } - return num.GetValueOrDefault(); - } - } - public override T[] Rent(int minimumLength) { // Arrays can't be smaller than zero. We allow requesting zero-length arrays (even though @@ -101,7 +80,7 @@ namespace System.Buffers { // No need to log the empty array. Our pool is effectively infinite // and we'll never allocate for rents and never store for returns. - return EmptyArray<T>.Value; + return Array.Empty<T>(); } ArrayPoolEventSource log = ArrayPoolEventSource.Log; @@ -249,7 +228,7 @@ namespace System.Buffers // Try to push on to the associated stack first. If that fails, // round-robin through the other stacks. LockedStack[] stacks = _perCoreStacks; - int index = ExecutionId % stacks.Length; + int index = Environment.CurrentExecutionId % stacks.Length; for (int i = 0; i < stacks.Length; i++) { if (stacks[index].TryPush(array)) return; @@ -265,7 +244,7 @@ namespace System.Buffers // round-robin through the other stacks. T[] arr; LockedStack[] stacks = _perCoreStacks; - int index = ExecutionId % stacks.Length; + int index = Environment.CurrentExecutionId % stacks.Length; for (int i = 0; i < stacks.Length; i++) { if ((arr = stacks[index].TryPop()) != null) return arr; @@ -285,7 +264,7 @@ namespace System.Buffers public bool TryPush(T[] array) { bool enqueued = false; - MonitorEnterWithProcNumberFlush(this); + Monitor.Enter(this); if (_count < MaxBuffersPerArraySizePerCore) { _arrays[_count++] = array; @@ -299,7 +278,7 @@ namespace System.Buffers public T[] TryPop() { T[] arr = null; - MonitorEnterWithProcNumberFlush(this); + Monitor.Enter(this); if (_count > 0) { arr = _arrays[--_count]; @@ -308,21 +287,6 @@ namespace System.Buffers Monitor.Exit(this); return arr; } - - /// <summary> - /// Enters the monitor on the object. If there is any contention while trying - /// to acquire the monitor, it flushes the cached processor number so that subsequent - /// attempts to access the per-core stacks will use an updated processor number. - /// </summary> - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void MonitorEnterWithProcNumberFlush(object obj) - { - if (!Monitor.TryEnter(obj)) - { - t_cachedProcessorNumber = null; - Monitor.Enter(obj); - } - } } } } |