ArrayPool fixes (dotnet/coreclr#8774)
authorJan Kotas <jkotas@microsoft.com>
Tue, 3 Jan 2017 14:13:46 +0000 (06:13 -0800)
committerGitHub <noreply@github.com>
Tue, 3 Jan 2017 14:13:46 +0000 (06:13 -0800)
* Use Array.Empty<T>() to make the code identical to CoreRT copy
The internal EmptyArray<T> does not exist in CoreRT.
* Remove unnecessary helper type for lazy initialization
All generic types are beforefieldinit by design.
* Use | for clarity

Commit migrated from https://github.com/dotnet/coreclr/commit/65b50f1143cc0b0f1523f62f9c048cb3c89d4afb

src/coreclr/src/mscorlib/corefx/System/Buffers/ArrayPool.cs
src/coreclr/src/mscorlib/corefx/System/Buffers/ConfigurableArrayPool.cs
src/coreclr/src/mscorlib/corefx/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs
src/coreclr/src/mscorlib/src/System/Environment.cs

index 441e48d..77a07f7 100644 (file)
@@ -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.
index 1e0e769..f7b6034 100644 (file)
@@ -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;
index 9cf8bad..64c5ceb 100644 (file)
@@ -80,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;
index 81c1f09..0e2e7bd 100644 (file)
@@ -1168,7 +1168,7 @@ namespace System {
             Debug.Assert(ExecutionIdRefreshRate <= ExecutionIdCacheCountDownMask);
 
             // Mask with Int32.MaxValue to ensure the execution Id is not negative
-            t_executionIdCache = ((executionId << ExecutionIdCacheShift) & Int32.MaxValue) + ExecutionIdRefreshRate;
+            t_executionIdCache = ((executionId << ExecutionIdCacheShift) & Int32.MaxValue) | ExecutionIdRefreshRate;
 
             return executionId;
         }