AdrianStrugala / AvroConvert

Rapid Avro serializer for C# .NET
Other
102 stars 27 forks source link

Improve string serialization #123

Closed gmanvel closed 11 months ago

gmanvel commented 11 months ago

This is a draft PR as it builds upon previous PR making use of Span based WriteBytes / WriteBytesRaw methods and using CoreBenchmarks project.

Changing string serialization to use Span based API. For strings that exceed 512 bytes (chosen arbitrarily) uses pooled byte arrays as a buffer otherwise uses stackalloc byte[byteCount].

Running String benchmark for various length string dotnet run -c Release --filter "*WriterBenchmarksString*" produces the following output

Method StringLength (bytes) Mean Error StdDev Median Ratio RatioSD Gen0 Allocated Alloc Ratio
WriteString_Old 128 44.40 ns 0.875 ns 1.008 ns 44.26 ns 1.00 0.00 0.0363 152 B 1.00
WriteString_New 128 28.19 ns 0.475 ns 0.397 ns 28.19 ns 0.63 0.02 - - 0.00
WriteString_Old 256 60.11 ns 1.504 ns 4.241 ns 58.39 ns 1.00 0.00 0.0669 280 B 1.00
WriteString_New 256 46.69 ns 0.827 ns 1.404 ns 46.22 ns 0.76 0.06 - - 0.00
WriteString_Old 512 88.43 ns 1.697 ns 2.927 ns 87.36 ns 1.00 0.00 0.1281 536 B 1.00
WriteString_New 512 63.88 ns 1.314 ns 3.098 ns 63.05 ns 0.72 0.04 - - 0.00
WriteString_Old 1024 160.01 ns 0.772 ns 0.645 ns 160.20 ns 1.00 0.00 0.2503 1048 B 1.00
WriteString_New 1024 84.57 ns 1.708 ns 1.426 ns 85.04 ns 0.53 0.01 - - 0.00
WriteString_Old 2048 284.60 ns 5.521 ns 9.813 ns 280.61 ns 1.00 0.00 0.4950 2072 B 1.00
WriteString_New 2048 129.80 ns 2.553 ns 6.261 ns 128.34 ns 0.46 0.03 - - 0.00
WriteString_Old 4096 519.27 ns 9.825 ns 10.090 ns 518.72 ns 1.00 0.00 0.9842 4120 B 1.00
WriteString_New 4096 217.34 ns 3.843 ns 6.096 ns 216.54 ns 0.42 0.02 - - 0.00