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

Removing the dependency on Microsoft.IO.RecyclableStream and improving serializer performance #349

Open
wants to merge 3 commits into
base: release/v2_0_0
Choose a base branch
from

Conversation

stebet
Copy link
Contributor

@stebet stebet commented Dec 20, 2024

Main changes:

  • Remove dependency on Microsoft.IO.RecyclableMemoryStream and replaces that with implementations based on IBufferWriter<byte> and ArrayPool<byte>
  • Use AggressiveInlining where appropriate
  • Use better optimized methods when serializers support them (like using IBufferWriter<byte> instances where applicable etc.)
  • (De)SerializeAsync methods just call the non-async versions since this never does any I/O so async becomes pure overhead.
  • Use Stream/Array pooling where appropriate

Runner Information


BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.2605)
Unknown processor
.NET SDK 9.0.101
 [Host] : .NET 8.0.11 (8.0.1124.51707), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI

Toolchain=InProcessEmitToolchain 

CysharpMemoryPack

Method Mean Error StdDev P95 Gen0 Gen1 Gen2 Allocated
Serialize - Before 105.56 μs 2.094 μs 2.936 μs 109.40 μs 30.27 30.27 30.27 94.79 KB
Serialize - After 104.10 μs 2.077 μs 4.602 μs 109.93 μs 30.27 30.27 30.27 94.93 KB
Deserialize - Before 59.60 μs 1.184 μs 3.377 μs 63.59 μs 21.67 7.02 - 265.68 KB
Deserialize - After 53.55 μs 1.064 μs 2.590 μs 57.84 μs 21.67 7.02 - 265.68 KB
SerializeAsync - Before 96.91 μs 0.188 μs 0.166 μs 97.14 μs 30.27 30.27 30.27 94.79 KB
SerializeAsync - After 92.67 μs 1.757 μs 1.644 μs 93.85 μs 30.27 30.27 30.27 94.93 KB
DeserializeAsync - Before 52.47 μs 1.024 μs 1.219 μs 53.37 μs 21.67 7.02 - 265.68 KB
DeserializeAsync - After 50.08 μs 0.178 μs 0.139 μs 50.26 μs 21.67 7.02 - 265.68 KB

NeueccMessagePack

Method Mean Error StdDev P95 Gen0 Gen1 Gen2 Allocated
Serialize - Before 134.03 μs 2.584 μs 3.449 μs 141.49 μs 5.62 - - 71.59 KB
Serialize - After 130.03 μs 2.558 μs 3.829 μs 134.84 μs 5.62 0.73 - 71.62 KB
Deserialize - Before 228.95 μs 3.776 μs 3.347 μs 231.19 μs 21.48 6.84 - 265.68 KB
Deserialize - After 210.79 μs 4.098 μs 4.208 μs 213.72 μs 21.48 6.84 - 265.68 KB
SerializeAsync - Before 129.47 μs 1.940 μs 1.906 μs 132.96 μs 5.62 - - 71.38 KB
SerializeAsync - After 132.82 μs 2.454 μs 2.296 μs 134.70 μs 5.62 0.98 - 71.41 KB
DeserializeAsync - Before 212.83 μs 3.359 μs 4.248 μs 223.17 μs 21.48 6.84 - 265.68 KB
DeserializeAsync - After 220.15 μs 4.187 μs 6.394 μs 228.99 μs 21.48 6.84 - 265.68 KB

NewtonsoftJson

Method Mean Error StdDev P95 Gen0 Gen1 Gen2 Allocated
Serialize - Before 1,196.11 μs 8.743 μs 8.178 μs 1,206.85 μs 132.81 132.81 132.82 1058.87 KB
Serialize - After 982.74 μs 8.041 μs 6.715 μs 991.35 μs 44.92 44.92 44.92 472.39 KB
Deserialize - Before 1,714.11 μs 17.194 μs 14.358 μs 1,730.95 μs 89.84 89.84 89.84 956.01 KB
Deserialize - After 1,565.65 μs 30.581 μs 36.404 μs 1,593.10 μs 52.73 17.58 - 664.50 KB
SerializeAsync - Before 1,170.13 μs 15.676 μs 14.663 μs 1,194.89 μs 132.81 132.82 132.81 1058.84 KB
SerializeAsync - After 1,098.00 μs 21.695 μs 33.776 μs 1,150.39 μs 44.92 44.92 44.92 472.36 KB
DeserializeAsync - Before 1,873.96 μs 27.709 μs 25.919 μs 1,889.38 μs 89.84 89.84 89.84 956.37 KB
DeserializeAsync - After 1,503.74 μs 28.815 μs 29.591 μs 1,546.75 μs 52.73 17.58 - 664.73 KB

ProtoBufNet

Method Mean Error StdDev P95 Gen0 Gen1 Gen2 Allocated
Serialize - Before 272.91 μs 5.197 μs 5.104 μs 283.40 μs 5.86 - - 78.15 KB
Serialize - After 257.61 μs 1.529 μs 1.193 μs 258.70 μs 5.86 - - 77.94 KB
Deserialize - Before 415.35 μs 2.054 μs 1.604 μs 417.54 μs 21.48 6.84 - 265.99 KB
Deserialize - After 374.51 μs 4.100 μs 3.201 μs 379.65 μs 21.48 6.84 - 265.71 KB
SerializeAsync - Before 261.92 μs 3.084 μs 3.300 μs 268.50 μs 5.86 - - 78.13 KB
SerializeAsync - After 264.19 μs 1.083 μs 0.960 μs 265.26 μs 5.86 - - 77.95 KB
DeserializeAsync - Before 391.25 μs 6.817 μs 6.043 μs 400.76 μs 21.48 6.84 - 265.99 KB
DeserializeAsync - After 371.55 μs 1.868 μs 1.458 μs 373.18 μs 21.48 6.84 - 265.71 KB

ServiceStackJson

Method Mean Error StdDev P95 Gen0 Gen1 Gen2 Allocated
Serialize - Before 951.66 μs 8.324 μs 6.499 μs 960.35 μs 93.75 46.88 46.88 918.06 KB
Serialize - After 1,110.08 μs 21.414 μs 22.913 μs 1,129.70 μs 93.75 46.88 46.88 919.08 KB
Deserialize - Before 3,155.60 μs 16.833 μs 14.922 μs 3,172.18 μs 39.06 11.72 - 517.84 KB
Deserialize - After 3,072.51 μs 60.461 μs 82.760 μs 3,198.14 μs 39.06 11.72 - 516.22 KB
SerializeAsync - Before 1,261.15 μs 23.705 μs 21.014 μs 1,286.96 μs 103.52 41.02 41.02 918.45 KB
SerializeAsync - After 1,039.95 μs 5.062 μs 4.735 μs 1,045.45 μs 93.75 46.88 46.88 915.11 KB
DeserializeAsync - Before 3,021.69 μs 13.353 μs 11.837 μs 3,039.19 μs 39.06 11.72 - 517.84 KB
DeserializeAsync - After 3,378.36 μs 43.456 μs 40.649 μs 3,398.41 μs 39.06 11.72 - 516.22 KB

SystemTextJson

Method Mean Error StdDev P95 Gen0 Gen1 Gen2 Allocated
Serialize - Before 401.76 μs 1.727 μs 1.615 μs 403.71 μs 41.02 41.02 41.02 146.87 KB
Serialize - After 391.87 μs 3.665 μs 3.428 μs 396.84 μs 39.55 39.55 39.55 146.90 KB
Deserialize - Before 793.84 μs 6.847 μs 5.717 μs 799.79 μs 31.25 9.77 - 391.67 KB
Deserialize - After 782.21 μs 14.931 μs 14.665 μs 801.80 μs 31.25 9.77 - 391.67 KB
SerializeAsync - Before 377.48 μs 0.936 μs 0.830 μs 378.88 μs 42.48 42.48 42.48 147.40 KB
SerializeAsync - After 352.10 μs 0.950 μs 0.889 μs 353.30 μs 39.55 39.55 39.55 146.89 KB
DeserializeAsync - Before 1,117.59 μs 5.602 μs 4.966 μs 1,121.62 μs 31.25 9.77 - 391.98 KB
DeserializeAsync - After 823.95 μs 16.018 μs 22.455 μs 843.89 μs 31.25 9.77 - 391.67 KB

@jodydonetti jodydonetti self-assigned this Dec 20, 2024
@jodydonetti jodydonetti added the enhancement New feature or request label Dec 20, 2024
@jodydonetti jodydonetti added this to the v2.0.0 milestone Dec 20, 2024
@jodydonetti
Copy link
Collaborator

Hi @stebet , this looks glorious 😍

Will carefully look into it this evening or tomorrow and will ping back.

Thanks!

@stebet
Copy link
Contributor Author

stebet commented Dec 20, 2024

Hi @stebet , this looks glorious 😍

Will carefully look into it this evening or tomorrow and will ping back.

Thanks!

I'm looking at a few test issues, I'll let you know as soon as I'm done figuring them out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants