From 9571f36beb22a088d7137a1b54673d0459d831cb Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 29 Jan 2018 15:31:13 -0800 Subject: [PATCH 1/5] Remove MemoryPool and fix System.Buffers version --- DotnetCLIVersion.txt | 2 +- SharedRuntimeVersion.txt | 2 +- .../System/Buffers/Native/NativeMemoryPool.cs | 2 +- .../System.Buffers.Primitives.csproj | 2 +- .../System/Buffers/Pooling/ArrayMemoryPool.cs | 4 +-- .../System/Buffers/Pooling/MemoryPool.cs | 29 ------------------- .../Internal/Pool/MemoryPool.cs | 4 +-- .../System/Buffers/MemoryPool.cs | 4 +-- .../System/IO/Pipelines/PipeOptions.cs | 2 +- .../System/Text/Json/JsonParser.cs | 2 +- .../BufferReferenceTests.cs | 2 +- tools/dependencies.props | 9 +++--- 12 files changed, 18 insertions(+), 46 deletions(-) delete mode 100644 src/System.Buffers.Primitives/System/Buffers/Pooling/MemoryPool.cs diff --git a/DotnetCLIVersion.txt b/DotnetCLIVersion.txt index d04fdeffa7e..91ef6c3a1d4 100644 --- a/DotnetCLIVersion.txt +++ b/DotnetCLIVersion.txt @@ -1 +1 @@ -2.1.300-preview2-008019 +2.1.300-preview2-008020 diff --git a/SharedRuntimeVersion.txt b/SharedRuntimeVersion.txt index 4e86791333d..284e4fedc05 100644 --- a/SharedRuntimeVersion.txt +++ b/SharedRuntimeVersion.txt @@ -1 +1 @@ -2.1.0-preview2-26124-07 \ No newline at end of file +2.1.0-preview2-26126-04 \ No newline at end of file diff --git a/src/System.Buffers.Experimental/System/Buffers/Native/NativeMemoryPool.cs b/src/System.Buffers.Experimental/System/Buffers/Native/NativeMemoryPool.cs index befa96efa40..f5a7d2c43fe 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Native/NativeMemoryPool.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Native/NativeMemoryPool.cs @@ -45,7 +45,7 @@ protected override void Dispose(bool disposing) public override OwnedMemory Rent(int numberOfBytes = DefaultSize) { - if (numberOfBytes == AnySize) numberOfBytes = DefaultSize; + if (numberOfBytes == -1) numberOfBytes = DefaultSize; if (numberOfBytes < 1 || numberOfBytes > MaxBufferSize) throw new ArgumentOutOfRangeException(nameof(numberOfBytes)); if (numberOfBytes > _bufferSize) new NotSupportedException(); diff --git a/src/System.Buffers.Primitives/System.Buffers.Primitives.csproj b/src/System.Buffers.Primitives/System.Buffers.Primitives.csproj index 9bdef720ed3..0a0b9731dcb 100644 --- a/src/System.Buffers.Primitives/System.Buffers.Primitives.csproj +++ b/src/System.Buffers.Primitives/System.Buffers.Primitives.csproj @@ -9,7 +9,7 @@ - + diff --git a/src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs b/src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs index 3ef7dd82db5..35ee091da3b 100644 --- a/src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs +++ b/src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs @@ -17,9 +17,9 @@ internal sealed class ArrayMemoryPool : MemoryPool public override int MaxBufferSize => 1024 * 1024 * 1024; - public override OwnedMemory Rent(int minimumBufferSize = AnySize) + public override OwnedMemory Rent(int minimumBufferSize = -1) { - if (minimumBufferSize == AnySize) minimumBufferSize = DefaultSize; + if (minimumBufferSize == -1) minimumBufferSize = DefaultSize; else if (minimumBufferSize > MaxBufferSize || minimumBufferSize < 1) throw new ArgumentOutOfRangeException(nameof(minimumBufferSize)); return new ArrayPoolMemory(minimumBufferSize); } diff --git a/src/System.Buffers.Primitives/System/Buffers/Pooling/MemoryPool.cs b/src/System.Buffers.Primitives/System/Buffers/Pooling/MemoryPool.cs deleted file mode 100644 index 5cb668439ec..00000000000 --- a/src/System.Buffers.Primitives/System/Buffers/Pooling/MemoryPool.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace System.Buffers -{ - public abstract class MemoryPool : IDisposable - { - public const int AnySize = int.MinValue; - - public static MemoryPool Default => Internal.ArrayMemoryPool.Shared; - - public abstract OwnedMemory Rent(int minimumBufferSize = AnySize); - - public abstract int MaxBufferSize { get; } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~MemoryPool() - { - Dispose(false); - } - - protected abstract void Dispose(bool disposing); - } -} diff --git a/src/System.IO.Pipelines.Networking.Windows.RIO/Internal/Pool/MemoryPool.cs b/src/System.IO.Pipelines.Networking.Windows.RIO/Internal/Pool/MemoryPool.cs index dc9b252ec8d..91eab71ad15 100644 --- a/src/System.IO.Pipelines.Networking.Windows.RIO/Internal/Pool/MemoryPool.cs +++ b/src/System.IO.Pipelines.Networking.Windows.RIO/Internal/Pool/MemoryPool.cs @@ -73,9 +73,9 @@ public RioMemoryPool(Action allocationCallback = null, Action _slabDeallocationCallback = deallocationCallback; } - public override OwnedMemory Rent(int size = AnySize) + public override OwnedMemory Rent(int size = -1) { - if (size == AnySize) size = _blockLength; + if (size == -1) size = _blockLength; else if (size > _blockLength) { RioPipelinesThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockLength); diff --git a/src/System.IO.Pipelines/System/Buffers/MemoryPool.cs b/src/System.IO.Pipelines/System/Buffers/MemoryPool.cs index 498f4d7bada..0c0a70ad04c 100644 --- a/src/System.IO.Pipelines/System/Buffers/MemoryPool.cs +++ b/src/System.IO.Pipelines/System/Buffers/MemoryPool.cs @@ -63,9 +63,9 @@ public class MemoryPool : MemoryPool /// private bool _disposedValue = false; // To detect redundant calls - public override OwnedMemory Rent(int size = AnySize) + public override OwnedMemory Rent(int size = -1) { - if (size == AnySize) size = _blockLength; + if (size == -1) size = _blockLength; else if (size > _blockLength) { PipelinesThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockLength); diff --git a/src/System.IO.Pipelines/System/IO/Pipelines/PipeOptions.cs b/src/System.IO.Pipelines/System/IO/Pipelines/PipeOptions.cs index 8280743310a..1c8dd738eae 100644 --- a/src/System.IO.Pipelines/System/IO/Pipelines/PipeOptions.cs +++ b/src/System.IO.Pipelines/System/IO/Pipelines/PipeOptions.cs @@ -19,7 +19,7 @@ public PipeOptions( long resumeWriterThreshold = 0, int minimumSegmentSize = 2048) { - Pool = pool ?? MemoryPool.Default; + Pool = pool ?? MemoryPool.Shared; ReaderScheduler = readerScheduler; WriterScheduler = writerScheduler; PauseWriterThreshold = pauseWriterThreshold; diff --git a/src/System.Text.Json/System/Text/Json/JsonParser.cs b/src/System.Text.Json/System/Text/Json/JsonParser.cs index 4423e07d510..debfaca3a55 100644 --- a/src/System.Text.Json/System/Text/Json/JsonParser.cs +++ b/src/System.Text.Json/System/Text/Json/JsonParser.cs @@ -146,7 +146,7 @@ private enum JsonTokenType public JsonObject Parse(ReadOnlySpan utf8Json, MemoryPool pool = null) { _pool = pool; - if (_pool == null) _pool = MemoryPool.Default; + if (_pool == null) _pool = MemoryPool.Shared; _scratchManager = _pool.Rent(utf8Json.Length * 4); _scratchMemory = _scratchManager.Memory; diff --git a/tests/System.Buffers.Primitives.Tests/BufferReferenceTests.cs b/tests/System.Buffers.Primitives.Tests/BufferReferenceTests.cs index 61e25b2e999..99528e525c4 100644 --- a/tests/System.Buffers.Primitives.Tests/BufferReferenceTests.cs +++ b/tests/System.Buffers.Primitives.Tests/BufferReferenceTests.cs @@ -18,7 +18,7 @@ public void OwnedArrayReferenceTests() public void PooledBufferReferenceTests() { BufferReferenceTests.TestOwnedBuffer(() => { - return MemoryPool.Default.Rent(1000); + return MemoryPool.Shared.Rent(1000); }); } diff --git a/tools/dependencies.props b/tools/dependencies.props index e2cbd0b26b4..205a9a0c5df 100644 --- a/tools/dependencies.props +++ b/tools/dependencies.props @@ -1,11 +1,12 @@ - 2.1.0-preview2-26124-07 + 2.1.0-preview2-26126-04 4.3.0 2.6.0-beta3-62316-02 - 4.5.0-preview2-26125-06 - 4.5.0-preview2-26125-06 - 4.5.0-preview2-26125-06 + 4.5.0-preview2-26127-04 + 4.5.0-preview2-26127-04 + 4.5.0-preview2-26127-04 + 4.5.0-preview2-26127-04 1.9.1 15.0.0 2.2.0 From 629a4bb843592091669ff4504e5012938f7a73d8 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 29 Jan 2018 15:35:59 -0800 Subject: [PATCH 2/5] Update package versions and update-dependencies script --- DotnetCLIVersion.txt | 2 +- SharedRuntimeVersion.txt | 2 +- scripts/update-dependencies/Program.cs | 1 + tools/dependencies.props | 10 +++++----- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/DotnetCLIVersion.txt b/DotnetCLIVersion.txt index 91ef6c3a1d4..c3dd321d427 100644 --- a/DotnetCLIVersion.txt +++ b/DotnetCLIVersion.txt @@ -1 +1 @@ -2.1.300-preview2-008020 +2.1.300-preview2-008022 diff --git a/SharedRuntimeVersion.txt b/SharedRuntimeVersion.txt index 284e4fedc05..e11d1d3eabc 100644 --- a/SharedRuntimeVersion.txt +++ b/SharedRuntimeVersion.txt @@ -1 +1 @@ -2.1.0-preview2-26126-04 \ No newline at end of file +2.1.0-preview2-26129-05 \ No newline at end of file diff --git a/scripts/update-dependencies/Program.cs b/scripts/update-dependencies/Program.cs index 4f34a1e93e8..0a3e1a7aa25 100644 --- a/scripts/update-dependencies/Program.cs +++ b/scripts/update-dependencies/Program.cs @@ -100,6 +100,7 @@ private static IEnumerable GetUpdaters() yield return CreateRegexPropertyUpdater(config.DependencyFilePath, "SystemCompilerServicesUnsafeVersion", "System.Runtime.CompilerServices.Unsafe"); yield return CreateRegexPropertyUpdater(config.DependencyFilePath, "SystemMemoryVersion", "System.Memory"); yield return CreateRegexPropertyUpdater(config.DependencyFilePath, "SystemNumericsVectorsVersion", "System.Numerics.Vectors"); + yield return CreateRegexPropertyUpdater(config.DependencyFilePath, "SystemBuffersVersion", "System.Buffers"); yield return CreateFileUpdater(config.CLIVersionFilePath, "Microsoft.DotNet.Cli.Utils"); // Temporary workaround until CLI, Core-Setup, CoreFx are all in sync with the shared runtime. diff --git a/tools/dependencies.props b/tools/dependencies.props index 205a9a0c5df..48e4be321ce 100644 --- a/tools/dependencies.props +++ b/tools/dependencies.props @@ -1,12 +1,12 @@ - 2.1.0-preview2-26126-04 + 2.1.0-preview2-26129-05 4.3.0 2.6.0-beta3-62316-02 - 4.5.0-preview2-26127-04 - 4.5.0-preview2-26127-04 - 4.5.0-preview2-26127-04 - 4.5.0-preview2-26127-04 + 2.1.0-preview2-26129-05 + 2.1.0-preview2-26129-05 + 2.1.0-preview2-26129-05 + 2.1.0-preview2-26129-05 1.9.1 15.0.0 2.2.0 From 77906934645466751634aaef3350116b4bdfc77c Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 29 Jan 2018 15:39:05 -0800 Subject: [PATCH 3/5] Fix typo in package versions. --- tools/dependencies.props | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/dependencies.props b/tools/dependencies.props index 48e4be321ce..c777b5db891 100644 --- a/tools/dependencies.props +++ b/tools/dependencies.props @@ -3,10 +3,10 @@ 2.1.0-preview2-26129-05 4.3.0 2.6.0-beta3-62316-02 - 2.1.0-preview2-26129-05 - 2.1.0-preview2-26129-05 - 2.1.0-preview2-26129-05 - 2.1.0-preview2-26129-05 + 4.5.0-preview2-26129-05 + 4.5.0-preview2-26129-05 + 4.5.0-preview2-26129-05 + 4.5.0-preview2-26129-05 1.9.1 15.0.0 2.2.0 From dbe65fad2efd59ea8e5c0274ecfe4baad11d4b16 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 29 Jan 2018 15:42:05 -0800 Subject: [PATCH 4/5] Update package versions --- tools/dependencies.props | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/dependencies.props b/tools/dependencies.props index c777b5db891..9789eb75337 100644 --- a/tools/dependencies.props +++ b/tools/dependencies.props @@ -3,10 +3,10 @@ 2.1.0-preview2-26129-05 4.3.0 2.6.0-beta3-62316-02 - 4.5.0-preview2-26129-05 - 4.5.0-preview2-26129-05 - 4.5.0-preview2-26129-05 - 4.5.0-preview2-26129-05 + 4.5.0-preview2-26127-04 + 4.5.0-preview2-26127-04 + 4.5.0-preview2-26127-04 + 4.5.0-preview2-26127-04 1.9.1 15.0.0 2.2.0 From e07aa74e85ea742d7291921d389188347b15f7cb Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Mon, 29 Jan 2018 15:49:08 -0800 Subject: [PATCH 5/5] Remove ArrayMemoryPool and ArrayPoolMemory --- .../System/Buffers/Pooling/ArrayMemoryPool.cs | 109 ------------------ 1 file changed, 109 deletions(-) delete mode 100644 src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs diff --git a/src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs b/src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs deleted file mode 100644 index 35ee091da3b..00000000000 --- a/src/System.Buffers.Primitives/System/Buffers/Pooling/ArrayMemoryPool.cs +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Threading; - -namespace System.Buffers.Internal -{ - internal sealed class ArrayMemoryPool : MemoryPool - { - const int DefaultSize = 4096; - - readonly static ArrayMemoryPool s_shared = new ArrayMemoryPool(); - - public static ArrayMemoryPool Shared => s_shared; - - public override int MaxBufferSize => 1024 * 1024 * 1024; - - public override OwnedMemory Rent(int minimumBufferSize = -1) - { - if (minimumBufferSize == -1) minimumBufferSize = DefaultSize; - else if (minimumBufferSize > MaxBufferSize || minimumBufferSize < 1) throw new ArgumentOutOfRangeException(nameof(minimumBufferSize)); - return new ArrayPoolMemory(minimumBufferSize); - } - - protected override void Dispose(bool disposing) - { - } - - private sealed class ArrayPoolMemory : OwnedMemory - { - T[] _array; - bool _disposed; - int _referenceCount; - - public ArrayPoolMemory(int size) - { - _array = ArrayPool.Shared.Rent(size); - } - - public override int Length => _array.Length; - - public override bool IsDisposed => _disposed; - - protected override bool IsRetained => _referenceCount > 0; - - public override Span Span - { - get - { - if (IsDisposed) ThrowObjectDisposedException(nameof(ArrayPoolMemory)); - return _array; - } - } - - protected override void Dispose(bool disposing) - { - var array = Interlocked.Exchange(ref _array, null); - if (array != null) - { - _disposed = true; - ArrayPool.Shared.Return(array); - } - } - - protected override bool TryGetArray(out ArraySegment arraySegment) - { - if (IsDisposed) ThrowObjectDisposedException(nameof(ArrayMemoryPool)); - arraySegment = new ArraySegment(_array); - return true; - } - - public override MemoryHandle Pin(int byteOffset = 0) - { - unsafe - { - Retain(); // this checks IsDisposed - if (byteOffset != 0 && (((uint)byteOffset) - 1) / Unsafe.SizeOf() >= _array.Length) throw new ArgumentOutOfRangeException(nameof(byteOffset)); - var handle = GCHandle.Alloc(_array, GCHandleType.Pinned); - void* pointer = Unsafe.Add((void*)handle.AddrOfPinnedObject(), byteOffset); - return new MemoryHandle(this, pointer, handle); - } - } - - public override void Retain() - { - if (IsDisposed) ThrowObjectDisposedException(nameof(ArrayPoolMemory)); - Interlocked.Increment(ref _referenceCount); - } - - public override bool Release() - { - int newRefCount = Interlocked.Decrement(ref _referenceCount); - if (newRefCount < 0) throw new InvalidOperationException(); - if (newRefCount == 0) - { - Dispose(); - return false; - } - return true; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - public static void ThrowObjectDisposedException(string objectName) - => throw new ObjectDisposedException(objectName); - } - } -}