You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://gitter.im/MessagePack-CSharp/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
The extremely fast [MessagePack](http://msgpack.org/) serializer for C#.
11
11
It is 10x faster than [MsgPack-Cli](https://github.com/msgpack/msgpack-cli) and outperforms other C# serializers. MessagePack for C# also ships with built-in support for LZ4 compression - an extremely fast compression algorithm. Performance is important, particularly in applications like games, distributed computing, microservices, or data caches.
@@ -34,6 +34,7 @@ MessagePack has a compact binary size and a full set of general purpose expressi
34
34
-[Security](#security)
35
35
-[Performance](#performance)
36
36
-[Deserialization Performance for different options](#deserialization-performance-for-different-options)
37
+
-[String interning](#string-interning)
37
38
-[LZ4 Compression](#lz4-compression)
38
39
-[Attributions](#attributions)
39
40
-[Comparison with protobuf, JSON, ZeroFormatter](#comparison-with-protobuf-json-zeroformatter)
@@ -184,7 +185,7 @@ These types can serialize by default:
When using `init` property setters in _generic_ classes, [a CLR bug](https://github.com/neuecc/MessagePack-CSharp/issues/1134) prevents our most efficient code generation from invoking the property setter.
434
+
As a result, you should avoid using `init` on property setters in generic classes when using the public-only `DynamicObjectResolver`/`StandardResolver`.
435
+
436
+
When using the `DynamicObjectResolverAllowPrivate`/`StandardResolverAllowPrivate` resolver the bug does not apply and you may use `init` without restriction.
437
+
417
438
## Serialization Callback
418
439
419
440
Objects implementing the `IMessagePackSerializationCallbackReceiver` interface will received `OnBeforeSerialize` and `OnAfterDeserialize` calls during serialization/deserialization.
@@ -679,7 +700,7 @@ Benchmarks comparing MessagePack For C# to other serializers were run on `Window
679
700
* Avoid string key decoding for lookup maps (string key and use automata based name lookup with inlined IL code generation, see: [AutomataDictionary](https://github.com/neuecc/MessagePack-CSharp/blob/bcedbce3fd98cb294210d6b4a22bdc4c75ccd916/src/MessagePack/Internal/AutomataDictionary.cs)
680
701
* To encode string keys, use pre-generated member name bytes and fixed sized byte array copies in IL, see: [UnsafeMemory.cs](https://github.com/neuecc/MessagePack-CSharp/blob/f17ddc5d107d3a2f66f60398b214ef87919ff892/src/MessagePack/Internal/UnsafeMemory.cs)
681
702
682
-
Before creating this library, I implemented a fast fast serializer with [ZeroFormatter#Performance](https://github.com/neuecc/ZeroFormatter#performance). This is a further evolved implementation. MessagePack for C# is always fast and optimized for all types (primitive, small struct, large object, any collections).
703
+
Before creating this library, I implemented a fast serializer with [ZeroFormatter#Performance](https://github.com/neuecc/ZeroFormatter#performance). This is a further evolved implementation. MessagePack for C# is always fast and optimized for all types (primitive, small struct, large object, any collections).
683
704
684
705
### <aname="deserialize-performance"></a>Deserialization Performance for different options
685
706
@@ -738,6 +759,49 @@ Extra note, this is serialization benchmark result.
738
759
739
760
Of course, `IntKey` is fastest but `StringKey` also performs reasonably well.
If you are writing your own formatter for some type that contains strings,
803
+
you can call on the `StringInterningFormatter` directly from your formatter as well for the strings.
804
+
741
805
## LZ4 Compression
742
806
743
807
MessagePack is a fast and *compact* format but it is not compression. [LZ4](https://github.com/lz4/lz4) is an extremely fast compression algorithm, and using it MessagePack for C# can achieve extremely fast performance as well as extremely compact binary sizes!
@@ -1068,7 +1132,7 @@ Here is an example of such a custom formatter implementation. Note its use of th
1068
1132
1069
1133
```csharp
1070
1134
/// <summary>Serializes a <seecref="FileInfo" /> by its full path as a string.</summary>
// If type can not get, must return null for fallback mechanism.
1352
1413
returnnull;
1353
1414
}
@@ -1435,30 +1496,44 @@ var resolver = MessagePack.Resolvers.CompositeResolver.Create(
1435
1496
1436
1497
## Reserved Extension Types
1437
1498
1438
-
MessagePack for C# already used some MessagePack extension type codes, be careful to use same ext code.
1499
+
MessagePack for C# already used some MessagePack extension type codes, be careful to avoid using the same ext code for other purposes.
1500
+
1501
+
Range | Reserved for
1502
+
--|--
1503
+
\[-128, -1\] | Reserved by the msgpack spec for predefined types
1504
+
\[30, 120) | Reserved for this library's use to support common types in .NET
1505
+
1506
+
This leaves the following ranges for your use:
1507
+
1508
+
-\[0, 30)
1509
+
-\[120, 127]
1510
+
1511
+
Within the *reserved* ranges, this library defines or implements extensions that use these type codes:
1439
1512
1440
1513
| Code | Type | Use by |
1441
-
| ---| ---| --- |
1442
-
| -1 | DateTime | MessagePack-spec reserved for timestamp |
1443
-
| 30 | Vector2[]| for Unity, UnsafeBlitFormatter |
1444
-
| 31 | Vector3[]| for Unity, UnsafeBlitFormatter |
1445
-
| 32 | Vector4[]| for Unity, UnsafeBlitFormatter |
1446
-
| 33 | Quaternion[]| for Unity, UnsafeBlitFormatter |
1447
-
| 34 | Color[]| for Unity, UnsafeBlitFormatter |
1448
-
| 35 | Bounds[]| for Unity, UnsafeBlitFormatter |
1449
-
| 36 | Rect[]| for Unity, UnsafeBlitFormatter |
1450
-
| 37 | Int[]| for Unity, UnsafeBlitFormatter |
1451
-
| 38 | Float[]| for Unity, UnsafeBlitFormatter |
1452
-
| 39 | Double[]| for Unity, UnsafeBlitFormatter |
1453
-
| 98 | All | MessagePackCompression.Lz4BlockArray |
1454
-
| 99 | All | MessagePackCompression.Lz4Block |
1455
-
| 100 | object | TypelessFormatter |
1514
+
| ---- | ----| --- |
1515
+
| -1 | DateTime | MessagePack-spec reserved for timestamp |
1516
+
| 30 | Vector2[]| for Unity, UnsafeBlitFormatter |
1517
+
| 31 | Vector3[]| for Unity, UnsafeBlitFormatter |
1518
+
| 32 | Vector4[]| for Unity, UnsafeBlitFormatter |
1519
+
| 33 | Quaternion[]| for Unity, UnsafeBlitFormatter |
1520
+
| 34 | Color[]| for Unity, UnsafeBlitFormatter |
1521
+
| 35 | Bounds[]| for Unity, UnsafeBlitFormatter |
1522
+
| 36 | Rect[]| for Unity, UnsafeBlitFormatter |
1523
+
| 37 | Int[]| for Unity, UnsafeBlitFormatter |
1524
+
| 38 | Float[]| for Unity, UnsafeBlitFormatter |
1525
+
| 39 | Double[]| for Unity, UnsafeBlitFormatter |
1526
+
| 98 | All | MessagePackCompression.Lz4BlockArray |
1527
+
| 99 | All | MessagePackCompression.Lz4Block |
1528
+
| 100 | object | TypelessFormatter |
1456
1529
1457
1530
## Unity support
1458
1531
1459
1532
Unity lowest supported version is `2018.3`, API Compatibility Level supports both `.NET 4.x` and `.NET Standard 2.0`.
1460
1533
1461
-
You can install the `unitypackage` from the [releases][Releases] page. If your build targets PC, you can use it as is, but if your build targets IL2CPP, you can not use `Dynamic***Resolver`, so it is required to use pre-code generation. Please see [pre-code generation section](#aot).
1534
+
You can install the `unitypackage` from the [releases][Releases] page.
1535
+
If your build targets .NET Framework 4.x and runs on mono, you can use it as is.
1536
+
But if your build targets IL2CPP, you can not use `Dynamic***Resolver`, so it is required to use pre-code generation. Please see [pre-code generation section](#aot).
1462
1537
1463
1538
MessagePack for C# includes some additional `System.*.dll` libraries that originally provides in NuGet. They are located under `Plugins`. If other packages use these libraries (e.g. Unity Collections package using `System.Runtime.CompilerServices.Unsafe.dll`), to avoid conflicts, please delete the DLL under `Plugins`.
1464
1539
@@ -1504,7 +1579,8 @@ If you want to share a class between Unity and a server, you can use `SharedProj
1504
1579
By default, MessagePack for C# serializes custom objects by [generating IL](https://msdn.microsoft.com/en-us/library/system.reflection.emit.ilgenerator.aspx) on the fly at runtime to create custom, highly tuned formatters for each type. This code generation has a minor upfront performance cost.
1505
1580
Because strict-AOT environments such as Xamarin and Unity IL2CPP forbid runtime code generation, MessagePack provides a way for you to run a code generator ahead of time as well.
1506
1581
1507
-
> Note: When Unity targets the PC it allows dynamic code generation, so AOT is not required.
1582
+
> Note: When using Unity, dynamic code generation only works when targeting .NET Framework 4.x + mono runtime.
1583
+
For all other Unity targets, AOT is required.
1508
1584
1509
1585
If you want to avoid the upfront dynamic generation cost or you need to run on Xamarin or Unity, you need AOT code generation. `mpc` (MessagePackCompiler) is the code generator of MessagePack for C#. mpc uses [Roslyn](https://github.com/dotnet/roslyn) to analyze source code.
1510
1586
@@ -1526,7 +1602,7 @@ Check in your `.config\dotnet-tools.json` file. On another machine you can "rest
1526
1602
Once you have the tool installed, simply invoke using `dotnet mpc` within your repo:
1527
1603
1528
1604
```
1529
-
dotnet mpc -h
1605
+
dotnet mpc --help
1530
1606
```
1531
1607
1532
1608
Alternatively, you can download mpc from the [releases][Releases] page, that includes platform native binaries (that don't require a separate dotnet runtime).
@@ -1535,7 +1611,7 @@ Alternatively, you can download mpc from the [releases][Releases] page, that inc
1535
1611
Usage: mpc [options...]
1536
1612
1537
1613
Options:
1538
-
-i, -input <String> Input path of analyze csproj or directory, if input multiple csproj split with ','. (Required)
1614
+
-i, -input <String> Input path to MSBuild project file or the directory containing Unity source files. (Required)
0 commit comments