ZeroFormatter — Fastest C# Serializer and Infinitely Fast Deserializer for .NET, .NET Core and Unity

I have created a new serialization format dedicated to C# for communicating C# Server to C# Server, Unity Client to C# Server and Save Unity client data to the local file. It is open-sourced on GitHub.

GitHub — neuecc/ZeroFormatter

The deserialization speed is zero so yes infinitely fast.

Image for post
Image for post

Of course, this is unfair. ZeroFormatter is similar as FlatBuffers. When deserializing an object, it returns a byte[] wrapper object.

Motivation

FlatBuffers is good, but API is too ugly, I can not use it regularly. For example, this official sample, if normal code then new Monster { Hp =… Name = … Weapon… }.

ZeroFormatter API is very smart, only on Serialize<T> and Deserialize<T>.

Another serializer is too slow especially in Unity because there no think about Unity, also did not optimize for Unity. I’ve creating UniRx — Reactive Extensions for Unity https://github.com/neuecc/UniRx for three years. I know how to write fast code on Unity, how to write works with IL2CPP.

Performance

Deserialize is infinitely fast so unfair but serialize speed is fair-comparison. Here is the result of ZeroFormatter vs .NET Serialziers.

Image for post
Image for post
Image for post
Image for post

ZeroFormatter is fastest(compare to protobuf-net, 2~3x fast) for sure. ZeroFormatter has many reasons why fast.

  • Serializer uses only ref byte[] and int offset, don't use MemoryStream(call MemoryStream api is overhead)
  • Don’t use variable-length number when encode number so there has encode cost(for example; protobuf uses ZigZag Encoding)
  • Acquire strict length of byte[] when knows final serialized length(for example; int, fixed-length list, string, etc…)
  • Avoid boxing all codes, all platforms(include Unity/IL2CPP)
  • Reduce native string encoder methods
  • Don’t create intermediate utility instance(XxxWriter/Reader, XxxContext, etc…)
  • Heavyly tuned dynamic il code generation: DynamicObjectFormatter.cs
  • Getting cached generated formatter on static generic field(don’t use dictinary-cache because dictionary lookup is overhead): Formatter.cs
  • Enum serialize underlying value only and uses fastest cast technique: EnumFormatter.cs

In Unity

Image for post
Image for post

ZeroFormatter is faster than JsonUtility so yes, faster than native serializer! Why MsgPack-Cli is slow? MsgPack-Cli’s Unity implemntation has a lot of hack of avoid AOT issues, it causes performance impact(especially struct, all codes pass boxing). ZeroFormatter codes is full tuned for Unity with/without IL2CPP.

Single Integer(1), Large String(represents HTML), Vector3 Struct(float, float, float), Vector3[100]

Image for post
Image for post
Image for post
Image for post

ZeroFormatter is optimized for all types(small struct to large object!). I know why protobuf-net is slow on integer test, currently protobuf-net’s internal serialize method has only object value so it causes boxing and critical for performance. Anyway, ZeroFormatter's simple struct and struct array(struct array is serialized FixedSizeList format internally, it is faster than class array)'s serialization/deserialization speed is very fast that effective storing value to KeyValueStore(like Redis) or network gaming(transport many transform position), etc.

Conclusion

ZeroFormatter is next-generation of C# serializer. Let’s check and try it!

GitHub — neuecc/ZeroFormatter

If you want to ask anything, I’ve opened the Reddit.

I have next plan; it is network-framework. It is useful for Unity and normal Web API C#. That is also the next generation framework. Please wait a moment.

Written by

a.k.a. neuecc. Creator of UniRx, UniTask, MessagePack for C#, MagicOnion etc. Microsoft MVP for C#. CEO/CTO of Cysharp Inc. Live and work in Tokyo, Japan.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store