Created
January 12, 2019 13:51
-
-
Save benaadams/a82c406024b8e74a4b075d3dfce0d220 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Buffers; | |
using System.Runtime.CompilerServices; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
public class Program | |
{ | |
public static void Main(string[] args) | |
=> BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); | |
private BufferWriter _writer = new BufferWriter(); | |
[Benchmark()] | |
public void WriterGenericWrapper() | |
{ | |
var writer = GetWriter(_writer); | |
writer.Advance(1); | |
writer.GetMemory(); | |
writer.GetSpan(); | |
} | |
[Benchmark()] | |
public void WriterGeneric() | |
{ | |
var writer = new Writer<BufferWriter>(_writer); | |
writer.Advance(1); | |
writer.GetMemory(); | |
writer.GetSpan(); | |
} | |
[Benchmark()] | |
public void WriterInterface() | |
{ | |
var writer = new Writer(_writer); | |
writer.Advance(1); | |
writer.GetMemory(); | |
writer.GetSpan(); | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
private static Writer<WriterAdapter> GetWriter(BufferWriter pipeWriter) | |
=> new Writer<WriterAdapter>(new WriterAdapter(pipeWriter)); | |
private struct WriterAdapter : IBufferWriter<byte> | |
{ | |
public BufferWriter Writer; | |
public WriterAdapter(BufferWriter writer) => Writer = writer; | |
public void Advance(int count) => Writer.Advance(count); | |
public Memory<byte> GetMemory(int sizeHint = 0) => Writer.GetMemory(sizeHint); | |
public Span<byte> GetSpan(int sizeHint = 0) => Writer.GetSpan(sizeHint); | |
} | |
} | |
public struct Writer<TBufferWriter> | |
where TBufferWriter : IBufferWriter<byte> | |
{ | |
private TBufferWriter _writer; | |
public Writer(TBufferWriter writer) => _writer = writer; | |
public void Advance(int count) => _writer.Advance(count); | |
public Memory<byte> GetMemory(int sizeHint = 0) => _writer.GetMemory(sizeHint); | |
public Span<byte> GetSpan(int sizeHint = 0) => _writer.GetSpan(sizeHint); | |
} | |
public struct Writer | |
{ | |
private IBufferWriter<byte> _writer; | |
public Writer(IBufferWriter<byte> writer) => _writer = writer; | |
public void Advance(int count) => _writer.Advance(count); | |
public Memory<byte> GetMemory(int sizeHint = 0) => _writer.GetMemory(sizeHint); | |
public Span<byte> GetSpan(int sizeHint = 0) => _writer.GetSpan(sizeHint); | |
} | |
public class BufferWriter : IBufferWriter<byte> | |
{ | |
public void Advance(int count) { } | |
public Memory<byte> GetMemory(int sizeHint = 0) => default; | |
public Span<byte> GetSpan(int sizeHint = 0) => default; | |
} |
Author
benaadams
commented
Jan 12, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment