MemoryPack icon indicating copy to clipboard operation
MemoryPack copied to clipboard

Can i get some help understanding the results of this benchmark ?

Open NicolaAtorino opened this issue 1 year ago • 1 comments

Apologies if this is not the right place to post this. Please let me know if there is a more relevant section.

I am running some benchmark in benchmark.net in order to try and understand better how to use the library.

The idea is to compare the method that returns a byteArray against the method that accepts an IBufferWriter<T> . For the IBufferWriter<T> i am using a RecyclableMemoryStream that implements the interface.

This is my benchmark :

using BenchmarkDotNet.Attributes;
using Bogus;
using MemoryPack;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.IO;
using Soenneker.Utils.AutoBogus;
using BenchmarkDotNet.Running;

namespace MyBenchmarks
{

     internal class Program
     {
         static async Task Main(string[] args)
          {
             var summary = BenchmarkRunner.Run<MemoryPackBenchmark>();
         }
     }

    [MemoryDiagnoser]
    public class MemoryPackBenchmark
    {
        private Dictionary<int, Entity>? _serializableData;
        static RecyclableMemoryStreamManager.Options _options = new RecyclableMemoryStreamManager.Options()
        {
            UseExponentialLargeBuffer = true,
            MaximumLargePoolFreeBytes = 145619402 * 2,
            MaximumSmallPoolFreeBytes = 145619402 * 2,
        };


        static RecyclableMemoryStreamManager _manager = new RecyclableMemoryStreamManager(_options);

        [GlobalSetup]
        public void Setup()
        {
            var index = 0;
            var faker = new AutoFaker<Entity>();
            _serializableData = faker.GenerateBetween(4000000,4000000).ToDictionary(x => ++index, v => v);
        }

        [Benchmark]
        public void SerializeBufferWriter()
        {
            MemoryStream _readStream = new MemoryStream();
            using var writeStream = _manager.GetStream();
            MemoryPackSerializer.Serialize(writeStream, _serializableData);
            writeStream.WriteTo(_readStream);
        }

        [Benchmark]
        public void SerializeNormal()
        {
            MemoryStream _readStream = new MemoryStream();
            var arrays = MemoryPackSerializer.Serialize(_serializableData);
            _readStream.Write(arrays);
            _readStream.Position = 0;
        }
    }
}

i was expecting the 'SerializeBufferWriter' to execute much faster than the 'SerializeNormal' method due to the use of the IBufferWriter-accepting overload.

However, the results of the benchmark is as follows :

Method Mean Error StdDev Median Allocated
SerializeBufferWriter 609.8 ms 65.68 ms 193.67 ms 527.6 ms 516.7 MB
SerializeNormal 334.3 ms 18.80 ms 55.43 ms 312.1 ms 277.79 MB

for some reason, the SerializeBufferWriter is almost double as slower than the SerializeNormal.

What am i missing ?

Thank you

NicolaAtorino avatar May 22 '24 18:05 NicolaAtorino

Is peformance of RecyclableMemoryStream poor?

neuecc avatar Jul 30 '24 11:07 neuecc

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jan 27 '25 00:01 github-actions[bot]