From 368111012d3ccf235f0b653b2bdd8aeb498fb9db Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 6 Oct 2020 13:41:57 +0200 Subject: [PATCH] Removed callvirt-s in BufferWriterStream and AsStream cast --- .../ArrayPoolBufferWriterExtensions.cs | 32 ++++++++++++ .../Extensions/IBufferWriterExtensions.cs | 7 +-- ...=> IBufferWriterStream{TWriter}.Memory.cs} | 4 +- ...eam.cs => IBufferWriterStream{TWriter}.cs} | 12 +++-- .../Streams/Sources/ArrayBufferWriterOwner.cs | 52 +++++++++++++++++++ .../Streams/Sources/IBufferWriterOwner.cs | 52 +++++++++++++++++++ 6 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 Microsoft.Toolkit.HighPerformance/Extensions/ArrayPoolBufferWriterExtensions.cs rename Microsoft.Toolkit.HighPerformance/Streams/{IBufferWriterStream.Memory.cs => IBufferWriterStream{TWriter}.Memory.cs} (94%) rename Microsoft.Toolkit.HighPerformance/Streams/{IBufferWriterStream.cs => IBufferWriterStream{TWriter}.cs} (92%) create mode 100644 Microsoft.Toolkit.HighPerformance/Streams/Sources/ArrayBufferWriterOwner.cs create mode 100644 Microsoft.Toolkit.HighPerformance/Streams/Sources/IBufferWriterOwner.cs diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/ArrayPoolBufferWriterExtensions.cs b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayPoolBufferWriterExtensions.cs new file mode 100644 index 00000000..21026113 --- /dev/null +++ b/Microsoft.Toolkit.HighPerformance/Extensions/ArrayPoolBufferWriterExtensions.cs @@ -0,0 +1,32 @@ +// 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 System.Diagnostics.Contracts; +using System.IO; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.HighPerformance.Buffers; +using Microsoft.Toolkit.HighPerformance.Streams; +using Microsoft.Toolkit.HighPerformance.Streams.Sources; + +namespace Microsoft.Toolkit.HighPerformance.Extensions +{ + /// + /// Helpers for working with the type. + /// + public static class ArrayPoolBufferWriterExtensions + { + /// + /// Returns a that can be used to write to a target an of instance. + /// + /// The target instance. + /// A wrapping and writing data to its underlying buffer. + /// The returned can only be written to and does not support seeking. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Stream AsStream(this ArrayPoolBufferWriter writer) + { + return new IBufferWriterStream(new ArrayBufferWriterOwner(writer)); + } + } +} diff --git a/Microsoft.Toolkit.HighPerformance/Extensions/IBufferWriterExtensions.cs b/Microsoft.Toolkit.HighPerformance/Extensions/IBufferWriterExtensions.cs index 1d295dfc..e6ffb06f 100644 --- a/Microsoft.Toolkit.HighPerformance/Extensions/IBufferWriterExtensions.cs +++ b/Microsoft.Toolkit.HighPerformance/Extensions/IBufferWriterExtensions.cs @@ -9,6 +9,7 @@ using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Microsoft.Toolkit.HighPerformance.Streams; +using Microsoft.Toolkit.HighPerformance.Streams.Sources; namespace Microsoft.Toolkit.HighPerformance.Extensions { @@ -18,16 +19,16 @@ namespace Microsoft.Toolkit.HighPerformance.Extensions public static class IBufferWriterExtensions { /// - /// Returns a that can be used to write to a target of instance. + /// Returns a that can be used to write to a target an of instance. /// - /// The target of instance. + /// The target instance. /// A wrapping and writing data to its underlying buffer. /// The returned can only be written to and does not support seeking. [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Stream AsStream(this IBufferWriter writer) { - return new IBufferWriterStream(writer); + return new IBufferWriterStream(new IBufferWriterOwner(writer)); } /// diff --git a/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream.Memory.cs b/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream{TWriter}.Memory.cs similarity index 94% rename from Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream.Memory.cs rename to Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream{TWriter}.Memory.cs index eeb9e78b..f9b431fd 100644 --- a/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream.Memory.cs +++ b/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream{TWriter}.Memory.cs @@ -11,8 +11,8 @@ using System.Threading.Tasks; namespace Microsoft.Toolkit.HighPerformance.Streams { - /// - internal sealed partial class IBufferWriterStream + /// + internal sealed partial class IBufferWriterStream { /// public override void CopyTo(Stream destination, int bufferSize) diff --git a/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream.cs b/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream{TWriter}.cs similarity index 92% rename from Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream.cs rename to Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream{TWriter}.cs index d3fb4fac..2be38aa9 100644 --- a/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream.cs +++ b/Microsoft.Toolkit.HighPerformance/Streams/IBufferWriterStream{TWriter}.cs @@ -14,12 +14,14 @@ namespace Microsoft.Toolkit.HighPerformance.Streams /// /// A implementation wrapping an instance. /// - internal sealed partial class IBufferWriterStream : Stream + /// The type of buffer writer to use. + internal sealed partial class IBufferWriterStream : Stream + where TWriter : struct, IBufferWriter { /// - /// The target instance to use. + /// The target instance to use. /// - private readonly IBufferWriter bufferWriter; + private readonly TWriter bufferWriter; /// /// Indicates whether or not the current instance has been disposed @@ -27,10 +29,10 @@ namespace Microsoft.Toolkit.HighPerformance.Streams private bool disposed; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The target instance to use. - public IBufferWriterStream(IBufferWriter bufferWriter) + public IBufferWriterStream(TWriter bufferWriter) { this.bufferWriter = bufferWriter; } diff --git a/Microsoft.Toolkit.HighPerformance/Streams/Sources/ArrayBufferWriterOwner.cs b/Microsoft.Toolkit.HighPerformance/Streams/Sources/ArrayBufferWriterOwner.cs new file mode 100644 index 00000000..0a534dc2 --- /dev/null +++ b/Microsoft.Toolkit.HighPerformance/Streams/Sources/ArrayBufferWriterOwner.cs @@ -0,0 +1,52 @@ +// 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 System; +using System.Buffers; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.HighPerformance.Buffers; + +namespace Microsoft.Toolkit.HighPerformance.Streams.Sources +{ + /// + /// An implementation wrapping an instance. + /// + internal readonly struct ArrayBufferWriterOwner : IBufferWriter + { + /// + /// The wrapped array. + /// + private readonly ArrayPoolBufferWriter writer; + + /// + /// Initializes a new instance of the struct. + /// + /// The wrapped instance. + public ArrayBufferWriterOwner(ArrayPoolBufferWriter writer) + { + this.writer = writer; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Advance(int count) + { + this.writer.Advance(count); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Memory GetMemory(int sizeHint = 0) + { + return this.writer.GetMemory(sizeHint); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span GetSpan(int sizeHint = 0) + { + return this.writer.GetSpan(sizeHint); + } + } +} diff --git a/Microsoft.Toolkit.HighPerformance/Streams/Sources/IBufferWriterOwner.cs b/Microsoft.Toolkit.HighPerformance/Streams/Sources/IBufferWriterOwner.cs new file mode 100644 index 00000000..2580037f --- /dev/null +++ b/Microsoft.Toolkit.HighPerformance/Streams/Sources/IBufferWriterOwner.cs @@ -0,0 +1,52 @@ +// 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 System; +using System.Buffers; +using System.Runtime.CompilerServices; +using Microsoft.Toolkit.HighPerformance.Buffers; + +namespace Microsoft.Toolkit.HighPerformance.Streams.Sources +{ + /// + /// An implementation wrapping an instance. + /// + internal readonly struct IBufferWriterOwner : IBufferWriter + { + /// + /// The wrapped array. + /// + private readonly IBufferWriter writer; + + /// + /// Initializes a new instance of the struct. + /// + /// The wrapped instance. + public IBufferWriterOwner(IBufferWriter writer) + { + this.writer = writer; + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Advance(int count) + { + this.writer.Advance(count); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Memory GetMemory(int sizeHint = 0) + { + return this.writer.GetMemory(sizeHint); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span GetSpan(int sizeHint = 0) + { + return this.writer.GetSpan(sizeHint); + } + } +}