msgpack / msgpack-cli

MessagePack implementation for Common Language Infrastructure / msgpack.org[C#]
http://msgpack.org
Apache License 2.0
834 stars 175 forks source link

Performance optimization when creating the serializers #179

Closed samcragg closed 8 years ago

samcragg commented 8 years ago

We were optimizing the memory usage during start up of our application and noticed there were lots of string allocations when creating the serializers.

I profiled the BasicUsage sample in Visual Studio and 7,211,352 total bytes were allocated in 1.9 seconds (slow because the profiler was attached). After the changes in this changeset these figures changed to 1,164,024 total bytes allocated and it finished in 1.1 seconds (again, this is slower than normal because the profiler was attached), so quicker and using a lot less memory during the creation of the serializers (less pressure on the GC, which was what we were investigating in our app in general so thought I'd pass the savings on to others ).

The cause is because the TextWriter.Null property returns an internal NullTextWriter class that only overloads the Write(string) method, meaning that anytime you call TextWriter.Null.Write("format", object) there's a call to string.Format which then gets thrown away. All I've done is generated a class that implements TextWriter and overrides all the methods it can and does nothing in them.

I've only modified the MsgPack.sln solution - there looks like there's an automated process for syncing the changes to the other solutions?