Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadStreamToArraySegment use BufferListStream directly when available #19823

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,17 @@ private static ArraySegment<byte> ReadStreamToArraySegment(Stream stream)
return new ArraySegment<byte>();
}

using var memStream = new MemoryStream(StreamBufferSizeInBytes);
stream.CopyTo(memStream, StreamBufferSizeInBytes);

return new ArraySegment<byte>(memStream.ToArray());
switch (stream)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.19042
AMD Ryzen 9 3950X, 1 CPU, 32 logical and 16 physical cores
.NET Core SDK=5.0.201
  [Host]     : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT
  Job-AYYPIA : .NET Core 3.1.13 (CoreCLR 4.700.21.11102, CoreFX 4.700.21.11602), X64 RyuJIT

InvocationCount=800000  
Method Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
ConvertViaMemoryStream 306.6 ns 6.13 ns 15.94 ns 1.00 0.00 0.1100 - - 928 B
PatternMatch 132.8 ns 1.13 ns 0.88 ns 0.48 0.04 0.0325 - - 280 B

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    using System;
    using System.IO;
    using System.Text;
    using BenchmarkDotNet.Attributes;
    using BenchmarkDotNet.Configs;
    using BenchmarkDotNet.Diagnosers;
    using BenchmarkDotNet.Exporters;
    using BenchmarkDotNet.Jobs;
    using Microsoft.Azure.Amqp;

    [Config(typeof(Config))]
    public class BufferListStreamDirectly
    {
        private BufferListStream stream;
        private AmqpMessage amqpMessage;

        private class Config : ManualConfig
        {
            public Config()
            {
                AddExporter(MarkdownExporter.GitHub);
                AddDiagnoser(MemoryDiagnoser.Default);
                AddJob(Job.Default.WithInvocationCount(800000));
            }
        }

        [IterationSetup]
        public void Setup()
        {
            stream = new BufferListStream(new[] {new ArraySegment<byte>(Encoding.UTF8.GetBytes("Hello"))});
            amqpMessage = AmqpMessage.Create(stream, false);
        }

        [IterationCleanup]
        public void Cleanup()
        {
            amqpMessage.Dispose();
        }

        private const int StreamBufferSizeInBytes = 512;

        [Benchmark(Baseline = true)]
        public ArraySegment<byte> ConvertViaMemoryStream()
        {
            using var messageStream = amqpMessage.ToStream();
            using var memStream = new MemoryStream(StreamBufferSizeInBytes);
            messageStream.CopyTo(memStream, StreamBufferSizeInBytes);
            return new ArraySegment<byte>(memStream.ToArray());
        }

        [Benchmark]
        public ArraySegment<byte> PatternMatch()
        {
            using var messageStream = amqpMessage.ToStream();
            return messageStream switch
            {
                BufferListStream bufferListStream => bufferListStream.ReadBytes((int) stream.Length),
                _ => throw new InvalidOperationException()
            };
        }
    }

{
case BufferListStream bufferListStream:
return bufferListStream.ReadBytes((int)stream.Length);
default:
{
using var memStream = new MemoryStream(StreamBufferSizeInBytes);
stream.CopyTo(memStream, StreamBufferSizeInBytes);
return new ArraySegment<byte>(memStream.ToArray());
}
}
}

public static AmqpMessage SBMessageToAmqpMessage(SBMessage sbMessage)
Expand Down