diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..53a1987
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,144 @@
+# examples are here: http://editorconfig.org/
+root = true
+
+[*]
+end_of_line = lf
+insert_final_newline = true
+indent_style = space
+indent_size = 4
+trim_trailing_whitespace = true
+charset = utf-8
+
+[*.{yml,yaml,csproj}]
+indent_size = 2
+
+[*.cs]
+# .NET Code Style Settings
+## Use this or not
+dotnet_style_qualification_for_field = false:error
+dotnet_style_qualification_for_property = false:error
+dotnet_style_qualification_for_method = false:error
+dotnet_style_qualification_for_event = false:error
+
+## use int or Int32
+dotnet_style_predefined_type_for_locals_parameters_members = true:error
+dotnet_style_predefined_type_for_member_access = true:error
+
+## Modifier preferences
+dotnet_style_require_accessibility_modifiers = always:error
+csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:error
+dotnet_style_readonly_field = true:error
+
+## use of object initializers, collection initializers, explicit tuple names
+dotnet_style_object_initializer = true:error
+dotnet_style_collection_initializer = true:error
+dotnet_style_explicit_tuple_names = true:error
+dotnet_prefer_inferred_tuple_names = true:error
+dotnet_prefer_inferred_anonymous_type_member_names = true:error
+
+## "Null" checking preferences
+dotnet_style_coalesce_expression = true:error
+dotnet_style_null_propagation = true:error
+
+## to var or not to var
+csharp_style_var_for_built_in_types = true:error
+csharp_style_var_when_type_is_apparent = true:error
+csharp_style_var_elsewhere = true:error
+
+## => for bodies or not
+csharp_style_expression_bodied_methods = when_on_single_line:error
+csharp_style_expression_bodied_constructors = when_on_single_line:error
+csharp_style_expression_bodied_operators = when_on_single_line:error
+csharp_style_expression_bodied_properties = when_on_single_line:error
+csharp_style_expression_bodied_indexers = when_on_single_line:error
+csharp_style_expression_bodied_accessors = when_on_single_line:error
+
+## pattern matching
+csharp_style_pattern_matching_over_is_with_cast_check = true:error
+csharp_style_pattern_matching_over_as_with_null_check = true:error
+csharp_style_inlined_variable_declaration = true:error
+
+## default expressions, deconstructed variables, and local functions over anonymous functions.
+csharp_prefer_simple_default_expression = true:error
+csharp_style_deconstructed_variable_declaration = true:suggestion
+csharp_style_pattern_local_over_anonymous_function = true:suggestion
+
+## null check
+csharp_style_throw_expression = true:error
+csharp_style_conditional_delegate_call = true:error
+
+## braces around one liner
+csharp_prefer_braces = false:suggestion
+
+# .NET formatting settings
+## Organize usings
+dotnet_sort_system_directives_first = true
+
+## Newline Options
+csharp_new_line_before_open_brace = all
+csharp_new_line_before_else = true
+csharp_new_line_before_catch = true
+csharp_new_line_before_finally = true
+csharp_new_line_before_members_in_object_initializers = true
+csharp_new_line_before_members_in_anonymous_types = true
+csharp_new_line_between_query_expression_clauses = true
+
+## Indentation options
+csharp_indent_case_contents = true
+csharp_indent_switch_labels = true
+csharp_indent_labels = one_less_than_current
+
+## Spacing Options
+csharp_space_after_cast = false
+csharp_space_after_keywords_in_control_flow_statements = true
+csharp_space_between_method_declaration_parameter_list_parentheses = false
+csharp_space_between_method_call_parameter_list_parentheses = false
+csharp_space_between_parentheses = false
+
+## Wrapping options
+
+csharp_preserve_single_line_statements = true
+csharp_preserve_single_line_blocks = true
+
+# Naming style
+
+## Source: https://github.com/kentcb/EditorConfigReference/blob/master/.editorconfig
+dotnet_naming_symbols.private_field_symbol.applicable_kinds = field
+dotnet_naming_symbols.private_field_symbol.applicable_accessibilities = private
+dotnet_naming_style.private_field_style.required_prefix = _
+dotnet_naming_style.private_field_style.capitalization = camel_case
+dotnet_naming_rule.private_fields_are_camel_case.severity = error
+dotnet_naming_rule.private_fields_are_camel_case.symbols = private_field_symbol
+dotnet_naming_rule.private_fields_are_camel_case.style = private_field_style
+
+dotnet_naming_symbols.non_private_field_symbol.applicable_kinds = field
+dotnet_naming_symbols.non_private_field_symbol.applicable_accessibilities = public,internal,friend,protected,protected_internal,protected_friend
+dotnet_naming_style.non_private_field_style.capitalization = pascal_case
+dotnet_naming_rule.non_private_fields_are_pascal_case.severity = error
+dotnet_naming_rule.non_private_fields_are_pascal_case.symbols = non_private_field_symbol
+dotnet_naming_rule.non_private_fields_are_pascal_case.style = non_private_field_style
+
+dotnet_naming_symbols.parameter_symbol.applicable_kinds = parameter
+dotnet_naming_style.parameter_style.capitalization = camel_case
+dotnet_naming_rule.parameters_are_camel_case.severity = error
+dotnet_naming_rule.parameters_are_camel_case.symbols = parameter_symbol
+dotnet_naming_rule.parameters_are_camel_case.style = parameter_style
+
+dotnet_naming_symbols.non_interface_type_symbol.applicable_kinds = class,struct,enum,delegate
+dotnet_naming_style.non_interface_type_style.capitalization = pascal_case
+dotnet_naming_rule.non_interface_types_are_pascal_case.severity = error
+dotnet_naming_rule.non_interface_types_are_pascal_case.symbols = non_interface_type_symbol
+dotnet_naming_rule.non_interface_types_are_pascal_case.style = non_interface_type_style
+
+dotnet_naming_symbols.interface_type_symbol.applicable_kinds = interface
+dotnet_naming_style.interface_type_style.capitalization = pascal_case
+dotnet_naming_style.interface_type_style.required_prefix = I
+dotnet_naming_rule.interface_types_must_be_prefixed_with_I.severity = error
+dotnet_naming_rule.interface_types_must_be_prefixed_with_I.symbols = interface_type_symbol
+dotnet_naming_rule.interface_types_must_be_prefixed_with_I.style = interface_type_style
+
+dotnet_naming_symbols.member_symbol.applicable_kinds = method,property,event
+dotnet_naming_style.member_style.capitalization = pascal_case
+dotnet_naming_rule.members_are_pascal_case.severity = error
+dotnet_naming_rule.members_are_pascal_case.symbols = member_symbol
+dotnet_naming_rule.members_are_pascal_case.style = member_style
diff --git a/.gitignore b/.gitignore
index 3653bb7..8aa62b5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,3 +18,5 @@ Release
bin
obj
/.vs
+
+**/BenchmarkDotNet.Artifacts/
diff --git a/azure-pipelines.yml b/azure-pipelines.yml
index 27ffa11..49c59ce 100644
--- a/azure-pipelines.yml
+++ b/azure-pipelines.yml
@@ -1,47 +1,13 @@
-jobs:
-- job: macOs
- pool:
- name: Hosted macOS
- steps:
- - template: .azure/tests.yml
-
-- job: linux
- pool:
- name: Hosted Ubuntu 1604
- steps:
- - script: curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin -channel 1.1
- displayName: Installing .netcore 1.1
- - template: .azure/tests.yml
- parameters:
- netcore1Global: false
-
-- job: win
- dependsOn:
- - macOs
- - linux
- pool:
- name: Hosted VS2017
- steps:
- - template: .azure/tests.yml
+variables:
+ sln: msgpack.sln
+ tests: tests/msgpack.light.tests/msgpack.light.tests.csproj
- - task: PowerShell@2
- displayName: pack nuget package
- inputs:
- targetType: inline
- script: |
- $version = $(git describe --tags | %{$_ -replace '-([^g])', '.$1'})
- dotnet pack --no-build -v minimal -c Release /property:Version=$version /property:PackageOutputPath=$(Build.ArtifactStagingDirectory)
+resources:
+ repositories:
+ - repository: templates
+ type: github
+ name: progaudi/azure-pipelines
+ endpoint: progaudi
- - ${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
- - task: NuGetCommand@2
- displayName: push nuget packages
- inputs:
- command: push
- packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg'
- nuGetFeedType: external
- publishFeedCredentials: api.nuget.org
-
- - task: PublishBuildArtifacts@1
- inputs:
- pathtoPublish: '$(Build.ArtifactStagingDirectory)'
- artifactName: .nupkgs
+jobs:
+- template: library/nuget.yml@templates
diff --git a/msgpack.sln b/msgpack.sln
index 31ffd25..fb1734c 100644
--- a/msgpack.sln
+++ b/msgpack.sln
@@ -1,4 +1,4 @@
-Microsoft Visual Studio Solution File, Format Version 12.00
+Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.10
MinimumVisualStudioVersion = 10.0.40219.1
@@ -12,8 +12,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "msgpack.light", "src\msgpac
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "msgpack.light.tests", "tests\msgpack.light.tests\msgpack.light.tests.csproj", "{4DD9051D-548F-4E11-86EA-746F443CEA67}"
EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "msgpack.light.benchmark", "src\msgpack.light.benchmark\msgpack.light.benchmark.csproj", "{62C5CC43-A029-4529-BBCA-6D8CC69E6B11}"
-EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -28,10 +26,6 @@ Global
{4DD9051D-548F-4E11-86EA-746F443CEA67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DD9051D-548F-4E11-86EA-746F443CEA67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DD9051D-548F-4E11-86EA-746F443CEA67}.Release|Any CPU.Build.0 = Release|Any CPU
- {62C5CC43-A029-4529-BBCA-6D8CC69E6B11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {62C5CC43-A029-4529-BBCA-6D8CC69E6B11}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {62C5CC43-A029-4529-BBCA-6D8CC69E6B11}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {62C5CC43-A029-4529-BBCA-6D8CC69E6B11}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/msgpack.sln.DotSettings b/msgpack.sln.DotSettings
index 1a1f4e0..3117fce 100644
--- a/msgpack.sln.DotSettings
+++ b/msgpack.sln.DotSettings
@@ -68,6 +68,7 @@
True
True
True
+ True
True
True
True
diff --git a/src/msgpack.light.benchmark/BeerDeserializeBenchmark.cs b/src/msgpack.light.benchmark/BeerDeserializeBenchmark.cs
index 6dcb7e4..658b230 100644
--- a/src/msgpack.light.benchmark/BeerDeserializeBenchmark.cs
+++ b/src/msgpack.light.benchmark/BeerDeserializeBenchmark.cs
@@ -41,91 +41,70 @@ private MemoryStream PrepareJson(BeerSerializeBenchmark serializer)
}
[Benchmark]
- public void JsonNet()
+ public object JsonNet()
{
_json.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(_json, Encoding.UTF8, false, 1024, true))
{
- var beer = Serializers.Newtonsoft.Deserialize(reader, typeof(Beer));
+ return Serializers.Newtonsoft.Deserialize(reader, typeof(Beer));
}
}
-
+
[Benchmark(Baseline = true)]
- public void MPCli_Stream()
+ public Beer MPCli_Stream()
{
_msgPack.Seek(0, SeekOrigin.Begin);
- var beer = Serializers.MsgPack.GetSerializer().Unpack(_msgPack);
+ return Serializers.MsgPack.GetSerializer().Unpack(_msgPack);
}
[Benchmark]
- public void MPCli_Array()
+ public Beer MPCli_Array()
{
- var beer = Serializers.MsgPack.GetSerializer().UnpackSingleObject(_msgPackArray);
+ return Serializers.MsgPack.GetSerializer().UnpackSingleObject(_msgPackArray);
}
[Benchmark]
- public void MPSharp_Stream()
+ public Beer MPSharp_Stream()
{
_msgPack.Seek(0, SeekOrigin.Begin);
- var beer = MessagePackSerializer.Deserialize(_msgPack);
- }
-
- [Benchmark]
- public void MPSharp_Array()
- {
- var beer = MessagePackSerializer.Deserialize(_msgPackArray);
+ return MessagePackSerializer.Deserialize(_msgPack);
}
[Benchmark]
- public void MPLight_Stream()
+ public Beer MPSharp_Array()
{
- _msgPack.Seek(0, SeekOrigin.Begin);
- var beer = MsgPackSerializer.Deserialize(_msgPack, Serializers.MsgPackLight);
+ return MessagePackSerializer.Deserialize(_msgPackArray);
}
[Benchmark]
- public void MPLight_Array()
+ public Beer MPLight_Array()
{
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLight);
+ return MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLight);
}
[Benchmark]
- public void MPCliH_Stream()
+ public Beer MPCliH_Stream()
{
_msgPack.Seek(0, SeekOrigin.Begin);
- var beer = Serializers.MsgPackHardcore.GetSerializer().Unpack(_msgPack);
+ return Serializers.MsgPackHardcore.GetSerializer().Unpack(_msgPack);
}
[Benchmark]
- public void MPCliH_Array()
+ public Beer MPCliH_Array()
{
- var beer = Serializers.MsgPackHardcore.GetSerializer().UnpackSingleObject(_msgPackArray);
+ return Serializers.MsgPackHardcore.GetSerializer().UnpackSingleObject(_msgPackArray);
}
[Benchmark]
- public void MPLightH_Stream()
+ public Beer MPLightH_Array()
{
- _msgPack.Seek(0, SeekOrigin.Begin);
- var beer = MsgPackSerializer.Deserialize(_msgPack, Serializers.MsgPackLightHardcore);
+ return MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightHardcore, out _);
}
- [Benchmark]
- public void MPLightH_Array()
- {
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightHardcore);
- }
-
- [Benchmark]
- public void MPLightH_Stream_AutoMap()
- {
- _msgPack.Seek(0, SeekOrigin.Begin);
- var beer = MsgPackSerializer.Deserialize(_msgPack, Serializers.MsgPackLightMapAutoGeneration);
- }
-
- [Benchmark]
- public void MPLightH_Array_AutoMap()
- {
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightMapAutoGeneration);
- }
+// [Benchmark]
+// public void MPLightH_Array_AutoMap()
+// {
+// var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightMapAutoGeneration);
+// }
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/BeerListDeserializeBenchmark.cs b/src/msgpack.light.benchmark/BeerListDeserializeBenchmark.cs
index d34d032..44251c1 100644
--- a/src/msgpack.light.benchmark/BeerListDeserializeBenchmark.cs
+++ b/src/msgpack.light.benchmark/BeerListDeserializeBenchmark.cs
@@ -31,13 +31,6 @@ private MemoryStream PrepareMsgPack(BeerListSerializeBenchmark serializer)
return memoryStream;
}
- private MemoryStream PrepareMsgPackLight(BeerListSerializeBenchmark serializer)
- {
- var memoryStream = new MemoryStream();
- serializer.MsgPackLightSerialize(memoryStream);
- return memoryStream;
- }
-
private MemoryStream PrepareJson(BeerListSerializeBenchmark serializer)
{
var memoryStream = new MemoryStream();
@@ -46,78 +39,57 @@ private MemoryStream PrepareJson(BeerListSerializeBenchmark serializer)
}
[Benchmark]
- public void JsonNet()
+ public object JsonNet()
{
_json.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(_json, Encoding.UTF8, false, 1024, true))
{
- var beer = Serializers.Newtonsoft.Deserialize(reader, typeof(Beer[]));
+ return Serializers.Newtonsoft.Deserialize(reader, typeof(Beer[]));
}
}
-
- [Benchmark(Baseline = true)]
- public void MPCli_Stream()
- {
- _msgPack.Seek(0, SeekOrigin.Begin);
- var beer = Serializers.MsgPack.GetSerializer().Unpack(_msgPack);
- }
-
- [Benchmark]
- public void MPCli_Array()
- {
- var beer = Serializers.MsgPack.GetSerializer().UnpackSingleObject(_msgPackArray);
- }
- [Benchmark]
- public void MPLight_Stream()
+ [Benchmark(Baseline = true)]
+ public Beer[] MPCli_Stream()
{
_msgPack.Seek(0, SeekOrigin.Begin);
- var beer = MsgPackSerializer.Deserialize(_msgPack, Serializers.MsgPackLight);
- }
-
- [Benchmark]
- public void MPLight_Array()
- {
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLight);
+ return Serializers.MsgPack.GetSerializer().Unpack(_msgPack);
}
[Benchmark]
- public void MPCliH_Stream()
+ public Beer[] MPCli_Array()
{
- _msgPack.Seek(0, SeekOrigin.Begin);
- var beer = Serializers.MsgPackHardcore.GetSerializer().Unpack(_msgPack);
+ return Serializers.MsgPack.GetSerializer().UnpackSingleObject(_msgPackArray);
}
[Benchmark]
- public void MPCliH_Array()
+ public Beer[] MPLight_Array()
{
- var beer = Serializers.MsgPackHardcore.GetSerializer().UnpackSingleObject(_msgPackArray);
+ return MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLight);
}
[Benchmark]
- public void MPLightH_Stream()
+ public Beer[] MPCliH_Stream()
{
_msgPack.Seek(0, SeekOrigin.Begin);
- var beer = MsgPackSerializer.Deserialize(_msgPack, Serializers.MsgPackLightHardcore);
+ return Serializers.MsgPackHardcore.GetSerializer().Unpack(_msgPack);
}
[Benchmark]
- public void MPLightH_Array()
+ public Beer[] MPCliH_Array()
{
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightHardcore);
+ return Serializers.MsgPackHardcore.GetSerializer().UnpackSingleObject(_msgPackArray);
}
[Benchmark]
- public void MPLightH_Stream_AutoMap()
+ public Beer[] MPLightH_Array()
{
- _msgPack.Seek(0, SeekOrigin.Begin);
- var beer = MsgPackSerializer.Deserialize(_msgPack, Serializers.MsgPackLightMapAutoGeneration);
+ return MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightHardcore);
}
- [Benchmark]
- public void MPLightH_Array_AutoMap()
- {
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightMapAutoGeneration);
- }
+// [Benchmark]
+// public void MPLightH_Array_AutoMap()
+// {
+// var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightMapAutoGeneration);
+// }
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/BeerListSerializeBenchmark.cs b/src/msgpack.light.benchmark/BeerListSerializeBenchmark.cs
index a663b9e..78d1dfa 100644
--- a/src/msgpack.light.benchmark/BeerListSerializeBenchmark.cs
+++ b/src/msgpack.light.benchmark/BeerListSerializeBenchmark.cs
@@ -1,4 +1,5 @@
-using System.IO;
+using System.Buffers;
+using System.IO;
using System.Text;
using BenchmarkDotNet.Attributes;
@@ -10,11 +11,16 @@ namespace ProGaudi.MsgPack.Light.Benchmark
[Config(typeof(BenchmarkConfig))]
public class BeerListSerializeBenchmark
{
+ private readonly MemoryStream _memoryStream = Serializers.CreateStream();
+
+ private readonly IMemoryOwner _buffer = MsgPackSerializer.Serialize(BenchmarkData.Belgium, Serializers.MsgPackLight, out _);
+
[Benchmark]
- public void JsonNet()
+ public long JsonNet()
{
- var memoryStream = new MemoryStream();
- JsonSerialize(memoryStream);
+ _memoryStream.Seek(0, SeekOrigin.Begin);
+ JsonSerialize(_memoryStream);
+ return _memoryStream.Position;
}
internal void JsonSerialize(MemoryStream memoryStream)
@@ -25,12 +31,13 @@ internal void JsonSerialize(MemoryStream memoryStream)
writer.Flush();
}
}
-
+
[Benchmark(Baseline = true)]
- public void MPCli_Stream()
+ public long MPCli_Stream()
{
- var memoryStream = new MemoryStream();
- MsgPackSerialize(memoryStream);
+ _memoryStream.Seek(0, SeekOrigin.Begin);
+ MsgPackSerialize(_memoryStream);
+ return _memoryStream.Position;
}
internal void MsgPackSerialize(MemoryStream memoryStream)
@@ -39,66 +46,41 @@ internal void MsgPackSerialize(MemoryStream memoryStream)
}
[Benchmark]
- public void MPCli_Array()
- {
- var bytes = Serializers.MsgPack.GetSerializer().PackSingleObject(BenchmarkData.Belgium);
- }
-
- [Benchmark]
- public void MPLight_Stream()
- {
- var memoryStream = new MemoryStream();
- MsgPackLightSerialize(memoryStream);
- }
-
- internal void MsgPackLightSerialize(MemoryStream memoryStream)
- {
- MsgPackSerializer.Serialize(BenchmarkData.Belgium, memoryStream, Serializers.MsgPackLight);
- }
-
- [Benchmark]
- public void MPLight_Array()
- {
- var bytes = MsgPackSerializer.Serialize(BenchmarkData.Belgium, Serializers.MsgPackLight);
- }
-
- [Benchmark]
- public void MPCliH_Stream()
- {
- var memoryStream = new MemoryStream();
- Serializers.MsgPackHardcore.GetSerializer().Pack(memoryStream, BenchmarkData.Belgium);
- }
-
- [Benchmark]
- public void MPCliH_Array()
+ public byte[] MPCli_Array()
{
- var bytes = Serializers.MsgPackHardcore.GetSerializer().PackSingleObject(BenchmarkData.Belgium);
+ return Serializers.MsgPack.GetSerializer().PackSingleObject(BenchmarkData.Belgium);
}
[Benchmark]
- public void MPLightH_Stream()
+ public int MPLight_Array()
{
- var memoryStream = new MemoryStream();
- MsgPackSerializer.Serialize(BenchmarkData.Belgium, memoryStream, Serializers.MsgPackLightHardcore);
+ return MsgPackSerializer.Serialize(BenchmarkData.Belgium, _buffer.Memory.Span, Serializers.MsgPackLight);
}
[Benchmark]
- public void MPLightH_Array()
+ public long MPCliH_Stream()
{
- var bytes = MsgPackSerializer.Serialize(BenchmarkData.Belgium, Serializers.MsgPackLightHardcore);
+ _memoryStream.Seek(0, SeekOrigin.Begin);
+ Serializers.MsgPackHardcore.GetSerializer().Pack(_memoryStream, BenchmarkData.Belgium);
+ return _memoryStream.Position;
}
[Benchmark]
- public void MPLightH_Stream_AutoMap()
+ public byte[] MPCliH_Array()
{
- var memoryStream = new MemoryStream();
- MsgPackSerializer.Serialize(BenchmarkData.Belgium, memoryStream, Serializers.MsgPackLightMapAutoGeneration);
+ return Serializers.MsgPackHardcore.GetSerializer().PackSingleObject(BenchmarkData.Belgium);
}
[Benchmark]
- public void MPLightH_Array_AutoMap()
+ public int MPLightH_Array()
{
- var bytes = MsgPackSerializer.Serialize(BenchmarkData.Belgium, Serializers.MsgPackLightMapAutoGeneration);
+ return MsgPackSerializer.Serialize(BenchmarkData.Belgium, _buffer.Memory.Span, Serializers.MsgPackLightHardcore);
}
+//
+// [Benchmark]
+// public void MPLightH_Array_AutoMap()
+// {
+// var bytes = MsgPackSerializer.Serialize(BenchmarkData.Belgium, Serializers.MsgPackLightMapAutoGeneration);
+// }
}
}
diff --git a/src/msgpack.light.benchmark/BeerSerializeBenchmark.cs b/src/msgpack.light.benchmark/BeerSerializeBenchmark.cs
index cf1d64f..53d271e 100644
--- a/src/msgpack.light.benchmark/BeerSerializeBenchmark.cs
+++ b/src/msgpack.light.benchmark/BeerSerializeBenchmark.cs
@@ -1,4 +1,6 @@
-using System.Collections.Generic;
+using System;
+using System.Buffers;
+using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -15,6 +17,10 @@ public class BeerSerializeBenchmark
{
private readonly Beer _testBeer;
+ private readonly MemoryStream _memoryStream = new MemoryStream();
+
+ private readonly IMemoryOwner _buffer;
+
public BeerSerializeBenchmark()
{
_testBeer = new Beer
@@ -24,13 +30,15 @@ public BeerSerializeBenchmark()
Brewery = "Sint-Sixtusabdij van Westvleteren",
Sort = new List {"trappist"}
};
+ _buffer = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightHardcore, out _);
}
[Benchmark]
- public void JsonNet()
+ public long JsonNet()
{
- var memoryStream = new MemoryStream();
- JsonSerialize(memoryStream);
+ _memoryStream.Seek(0, SeekOrigin.Begin);
+ JsonSerialize(_memoryStream);
+ return _memoryStream.Position;
}
internal void JsonSerialize(MemoryStream memoryStream)
@@ -41,12 +49,13 @@ internal void JsonSerialize(MemoryStream memoryStream)
writer.Flush();
}
}
-
+
[Benchmark(Baseline = true)]
- public void MPCli_Stream()
+ public long MPCli_Stream()
{
- var memoryStream = new MemoryStream();
- MsgPackSerialize(memoryStream);
+ _memoryStream.Seek(0, SeekOrigin.Begin);
+ MsgPackSerialize(_memoryStream);
+ return _memoryStream.Position;
}
internal void MsgPackSerialize(MemoryStream memoryStream)
@@ -55,93 +64,67 @@ internal void MsgPackSerialize(MemoryStream memoryStream)
}
[Benchmark]
- public void MPSharp_Stream()
- {
- var stream = new MemoryStream();
- MessagePackSerializer.Serialize(stream, _testBeer);
- }
-
- [Benchmark]
- public void MPSharp_Array()
- {
- MessagePackSerializer.Serialize(_testBeer);
- }
-
- [Benchmark]
- public void MPSharp_Array_Unsafe()
- {
- MessagePackSerializer.SerializeUnsafe(_testBeer);
- }
-
- [Benchmark]
- public void MPCli_Array()
+ public long MPSharp_Stream()
{
- var bytes = Serializers.MsgPack.GetSerializer().PackSingleObject(_testBeer);
+ _memoryStream.Seek(0, SeekOrigin.Begin);
+ MessagePackSerializer.Serialize(_memoryStream, _testBeer);
+ return _memoryStream.Position;
}
[Benchmark]
- public void MPLight_Stream()
+ public byte[] MPSharp_Array()
{
- var memoryStream = new MemoryStream();
- MsgPackSerializer.Serialize(_testBeer, memoryStream, Serializers.MsgPackLight);
+ return MessagePackSerializer.Serialize(_testBeer);
}
[Benchmark]
- public void MPLight_Array()
+ public ArraySegment MPSharp_Array_Unsafe()
{
- var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLight);
+ return MessagePackSerializer.SerializeUnsafe(_testBeer);
}
[Benchmark]
- public void MPCliH_Stream()
+ public byte[] MPCli_Array()
{
- var memoryStream = new MemoryStream();
- Serializers.MsgPackHardcore.GetSerializer().Pack(memoryStream, _testBeer);
+ return Serializers.MsgPack.GetSerializer().PackSingleObject(_testBeer);
}
[Benchmark]
- public void MPCliH_Array()
+ public int MPLight_Array()
{
- var bytes = Serializers.MsgPackHardcore.GetSerializer().PackSingleObject(_testBeer);
+ return MsgPackSerializer.Serialize(_testBeer, _buffer.Memory.Span, Serializers.MsgPackLight);
}
[Benchmark]
- public void MPLightH_Stream()
+ public long MPCliH_Stream()
{
- var memoryStream = new MemoryStream();
- MsgPackSerializer.Serialize(_testBeer, memoryStream, Serializers.MsgPackLightHardcore);
+ _memoryStream.Seek(0, SeekOrigin.Begin);
+ Serializers.MsgPackHardcore.GetSerializer().Pack(_memoryStream, _testBeer);
+ return _memoryStream.Position;
}
[Benchmark]
- public void MPLightH_Array()
+ public byte[] MPCliH_Array()
{
- var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightHardcore);
+ return Serializers.MsgPackHardcore.GetSerializer().PackSingleObject(_testBeer);
}
[Benchmark]
- public void MPLightH_Stream_AutoMap()
- {
- var memoryStream = new MemoryStream();
- MsgPackSerializer.Serialize(_testBeer, memoryStream, Serializers.MsgPackLightMapAutoGeneration);
- }
-
- [Benchmark]
- public void MPLightH_Array_AutoMap()
- {
- var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightMapAutoGeneration);
- }
-
- [Benchmark]
- public void MPLightH_Stream_AutoArray()
- {
- var memoryStream = new MemoryStream();
- MsgPackSerializer.Serialize(_testBeer, memoryStream, Serializers.MsgPackLightArrayAutoGeneration);
- }
-
- [Benchmark]
- public void MPLightH_Array_AutoArray()
- {
- var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightArrayAutoGeneration);
- }
+ public int MPLightH_Array()
+ {
+ return MsgPackSerializer.Serialize(_testBeer, _buffer.Memory.Span, Serializers.MsgPackLightHardcore);
+ }
+
+// [Benchmark]
+// public void MPLightH_Array_AutoMap()
+// {
+// var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightMapAutoGeneration);
+// }
+//
+// [Benchmark]
+// public void MPLightH_Array_AutoArray()
+// {
+// var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightArrayAutoGeneration);
+// }
}
}
diff --git a/src/msgpack.light.benchmark/BeerSkip.cs b/src/msgpack.light.benchmark/BeerSkip.cs
index 75cc241..e219546 100644
--- a/src/msgpack.light.benchmark/BeerSkip.cs
+++ b/src/msgpack.light.benchmark/BeerSkip.cs
@@ -21,43 +21,29 @@ public class BeerSkip
public BeerSkip()
{
- var serialize = new BeerSerializeBenchmark();
- _inputStream = PrepareMsgPack(serialize);
+ _inputStream = Serializers.CreateStream();
+ new BeerSerializeBenchmark().MsgPackSerialize(_inputStream);
_inputBytes = _inputStream.ToArray();
_unpacker = Unpacker.Create(_inputStream);
_msgPackContext = new MsgPackContext();
- _msgPackContext.RegisterConverter(new SkipConverter());
+ _msgPackContext.RegisterParser(_ => new SkipConverter());
}
- private MemoryStream PrepareMsgPack(BeerSerializeBenchmark serializer)
- {
- var memoryStream = new MemoryStream();
- serializer.MsgPackSerialize(memoryStream);
- return memoryStream;
- }
-
[Benchmark(Baseline = true)]
- public void MPackCli_Skip()
- {
- _inputStream.Position = 0;
- var result = _unpacker.Skip();
- }
-
- [Benchmark]
- public void MsgPackLight_Skip_Stream()
+ public long? MPackCli_Skip()
{
_inputStream.Position = 0;
- var beer = MsgPackSerializer.Deserialize(_inputStream, _msgPackContext);
+ return _unpacker.Skip();
}
[Benchmark]
- public void MsgPackLight_Skip_Array()
+ public Beer MsgPackLight_Skip_Array()
{
_inputStream.Position = 0;
- var beer = MsgPackSerializer.Deserialize(_inputBytes, _msgPackContext);
+ return MsgPackSerializer.Deserialize(_inputBytes, _msgPackContext, out _);
}
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/BeerSkipList.cs b/src/msgpack.light.benchmark/BeerSkipList.cs
index a53e1c5..01da2d8 100644
--- a/src/msgpack.light.benchmark/BeerSkipList.cs
+++ b/src/msgpack.light.benchmark/BeerSkipList.cs
@@ -21,43 +21,28 @@ public class BeerSkipList
public BeerSkipList()
{
- var serialize = new BeerListSerializeBenchmark();
- _inputStream = PrepareMsgPack(serialize);
+ _inputStream = Serializers.CreateStream();
+ new BeerListSerializeBenchmark().MsgPackSerialize(_inputStream);
_inputBytes = _inputStream.ToArray();
_unpacker = Unpacker.Create(_inputStream);
_msgPackContext = new MsgPackContext();
- _msgPackContext.RegisterConverter(new SkipConverter());
-
- }
-
- private MemoryStream PrepareMsgPack(BeerListSerializeBenchmark serializer)
- {
- var memoryStream = new MemoryStream();
- serializer.MsgPackSerialize(memoryStream);
- return memoryStream;
+ _msgPackContext.RegisterParser(new SkipConverter());
}
[Benchmark(Baseline = true)]
- public void MPackCli_Skip()
- {
- _inputStream.Position = 0;
- var result = _unpacker.Skip();
- }
-
- [Benchmark]
- public void MsgPackLight_Skip_Stream()
+ public long? MPackCli_Skip()
{
_inputStream.Position = 0;
- var beer = MsgPackSerializer.Deserialize(_inputStream, _msgPackContext);
+ return _unpacker.Skip();
}
[Benchmark]
- public void MsgPackLight_Skip_Array()
+ public Beer MsgPackLight_Skip_Array()
{
_inputStream.Position = 0;
- var beer = MsgPackSerializer.Deserialize(_inputBytes, _msgPackContext);
+ return MsgPackSerializer.Deserialize(_inputBytes, _msgPackContext, out _);
}
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/BenchmarkConfig.cs b/src/msgpack.light.benchmark/BenchmarkConfig.cs
index 0ba1a50..17d270d 100644
--- a/src/msgpack.light.benchmark/BenchmarkConfig.cs
+++ b/src/msgpack.light.benchmark/BenchmarkConfig.cs
@@ -5,8 +5,6 @@
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Exporters.Csv;
using BenchmarkDotNet.Jobs;
-using BenchmarkDotNet.Order;
-using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Toolchains.CsProj;
namespace ProGaudi.MsgPack.Light.Benchmark
@@ -25,14 +23,13 @@ public BenchmarkConfig()
//Add(Job.ShortRun.With(Jit.RyuJit).With(Platform.X64).With(Runtime.Clr));
- // RyuJit for .NET Core 1.1
- Add(Job.Default.With(Jit.RyuJit).With(Platform.X64).With(Runtime.Core).With(CsProjCoreToolchain.NetCoreApp11).WithId("netcore1.1"));
-
// RyuJit for .NET Core 2.0
Add(Job.Default.With(Jit.RyuJit).With(Platform.X64).With(Runtime.Core).With(CsProjCoreToolchain.NetCoreApp20).WithId("netcore2.0"));
+ Add(Job.Default.With(Jit.RyuJit).With(Platform.X64).With(Runtime.Core).With(CsProjCoreToolchain.NetCoreApp21).WithId("netcore2.1"));
+ Add(Job.Default.With(Jit.RyuJit).With(Platform.X64).With(Runtime.Core).With(CsProjCoreToolchain.NetCoreApp22).WithId("netcore2.2"));
Add(MarkdownExporter.GitHub);
Add(CsvMeasurementsExporter.Default);
}
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/Data/Beer.cs b/src/msgpack.light.benchmark/Data/Beer.cs
index ce79be6..38f6c01 100644
--- a/src/msgpack.light.benchmark/Data/Beer.cs
+++ b/src/msgpack.light.benchmark/Data/Beer.cs
@@ -5,7 +5,7 @@
namespace ProGaudi.MsgPack.Light.Benchmark.Data
{
[MessagePackObject(true)]
- public class Beer
+ public sealed class Beer
{
[MsgPackMapElement(nameof(Brand))]
[MsgPackArrayElement(0)]
@@ -23,4 +23,4 @@ public class Beer
[MsgPackArrayElement(3)]
public string Brewery { get; set; }
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/Data/BeerConverter.cs b/src/msgpack.light.benchmark/Data/BeerConverter.cs
index 859bff1..b85e7aa 100644
--- a/src/msgpack.light.benchmark/Data/BeerConverter.cs
+++ b/src/msgpack.light.benchmark/Data/BeerConverter.cs
@@ -1,70 +1,92 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.Runtime.Serialization;
namespace ProGaudi.MsgPack.Light.Benchmark.Data
{
- internal class BeerConverter : IMsgPackConverter
+ internal sealed class BeerConverter : IMsgPackFormatter, IMsgPackParser
{
- private IMsgPackConverter _stringConverter;
+ private readonly IMsgPackFormatter _stringFormatter;
+ private readonly IMsgPackFormatter> _listStringFormatter;
+ private readonly IMsgPackFormatter _floatFormatter;
- private IMsgPackConverter> _listStringConverter;
+ private readonly IMsgPackParser _stringParser;
+ private readonly IMsgPackParser> _listStringParser;
+ private readonly IMsgPackParser _floatParser;
- private IMsgPackConverter _floatConverter;
+ public BeerConverter(MsgPackContext context)
+ {
+ _stringFormatter = context.GetRequiredFormatter();
+ _listStringFormatter = context.GetRequiredFormatter>();
+ _floatFormatter = context.GetRequiredFormatter();
+
+ _stringParser = context.GetRequiredParser();
+ _listStringParser = context.GetRequiredParser>();
+ _floatParser = context.GetRequiredParser();
+ }
+
+ public int GetBufferSize(Beer value) => value == null
+ ? DataLengths.Nil
+ : _stringFormatter.GetBufferSize(value.Brand)
+ + _stringFormatter.GetBufferSize(value.Brewery)
+ + _listStringFormatter.GetBufferSize(value.Sort)
+ + _floatFormatter.GetBufferSize(value.Alcohol)
+ + 27;
- private MsgPackContext _context;
+ public bool HasConstantSize => false;
- public void Write(Beer value, IMsgPackWriter writer)
+ public int Format(Span destination, Beer value)
{
- if (value == null)
- {
- _context.NullConverter.Write(null, writer);
- return;
- }
+ if (value == null) return MsgPackSpec.WriteNil(destination);
+
+ var result = MsgPackSpec.WriteMapHeader(destination, 4);
+ result += _stringFormatter.Format(destination.Slice(result), nameof(value.Brand));
+ result += _stringFormatter.Format(destination.Slice(result), value.Brand);
- writer.WriteMapHeader(4);
- _stringConverter.Write(nameof(value.Brand), writer);
- _stringConverter.Write(value.Brand, writer);
+ result += _stringFormatter.Format(destination.Slice(result), nameof(value.Sort));
+ result += _listStringFormatter.Format(destination.Slice(result), value.Sort);
- _stringConverter.Write(nameof(value.Sort), writer);
- _listStringConverter.Write(value.Sort, writer);
+ result += _stringFormatter.Format(destination.Slice(result), nameof(value.Alcohol));
+ result += _floatFormatter.Format(destination.Slice(result), value.Alcohol);
- _stringConverter.Write(nameof(value.Alcohol), writer);
- _floatConverter.Write(value.Alcohol, writer);
+ result += _stringFormatter.Format(destination.Slice(result), nameof(value.Brewery));
+ result += _stringFormatter.Format(destination.Slice(result), value.Brewery);
- _stringConverter.Write(nameof(value.Brewery), writer);
- _stringConverter.Write(value.Brewery, writer);
+ return result;
}
- public Beer Read(IMsgPackReader reader)
+ public Beer Parse(ReadOnlySpan source, out int readSize)
{
- var length = reader.ReadMapLength();
- if (length == null)
- {
- return null;
- }
+ if (MsgPackSpec.TryReadNil(source, out readSize)) return null;
+ var length = MsgPackSpec.ReadMapHeader(source, out readSize);
if (length != 4)
{
throw new SerializationException("Bad format");
}
var result = new Beer();
- for (var i = 0; i < length.Value; i++)
+ for (var i = 0; i < length; i++)
{
- var propertyName = _stringConverter.Read(reader);
+ var propertyName = _stringParser.Parse(source, out var temp);
+ readSize += temp;
switch (propertyName)
{
case nameof(result.Brand):
- result.Brand = _stringConverter.Read(reader);
+ result.Brand = _stringParser.Parse(source, out temp);
+ readSize += temp;
break;
case nameof(result.Sort):
- result.Sort = _listStringConverter.Read(reader);
+ result.Sort = _listStringParser.Parse(source, out temp);
+ readSize += temp;
break;
case nameof(result.Alcohol):
- result.Alcohol = _floatConverter.Read(reader);
+ result.Alcohol = _floatParser.Parse(source, out temp);
+ readSize += temp;
break;
case nameof(result.Brewery):
- result.Brewery = _stringConverter.Read(reader);
+ result.Brewery = _stringParser.Parse(source, out temp);
+ readSize += temp;
break;
default:
throw new SerializationException("Bad format");
@@ -73,13 +95,5 @@ public Beer Read(IMsgPackReader reader)
return result;
}
-
- public void Initialize(MsgPackContext context)
- {
- _stringConverter = context.GetConverter();
- _listStringConverter = context.GetConverter>();
- _floatConverter = context.GetConverter();
- _context = context;
- }
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/Data/BeerConverterHardcore.cs b/src/msgpack.light.benchmark/Data/BeerConverterHardcore.cs
index ab938f0..a44c652 100644
--- a/src/msgpack.light.benchmark/Data/BeerConverterHardcore.cs
+++ b/src/msgpack.light.benchmark/Data/BeerConverterHardcore.cs
@@ -1,78 +1,114 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using System.Runtime.Serialization;
namespace ProGaudi.MsgPack.Light.Benchmark.Data
{
- internal class BeerConverterHardCore : IMsgPackConverter
+ internal sealed class BeerConverterHardCore : IMsgPackFormatter, IMsgPackParser
{
- private IMsgPackConverter _stringConverter;
+ private static readonly byte[] Brand;
+ private static readonly byte[] Alcohol;
+ private static readonly byte[] Sort;
+ private static readonly byte[] Brewery;
- private IMsgPackConverter> _listStringConverter;
+ private readonly IMsgPackFormatter _stringFormatter;
+ private readonly IMsgPackFormatter> _listStringFormatter;
+ private readonly IMsgPackFormatter _floatFormatter;
- private IMsgPackConverter _floatConverter;
+ private readonly IMsgPackParser _stringParser;
+ private readonly IMsgPackParser> _listStringParser;
+ private readonly IMsgPackParser _floatParser;
- private MsgPackContext _context;
+ static BeerConverterHardCore()
+ {
+ Brand = new byte[6];
+ MsgPackSpec.WriteFixString(Brand, nameof(Beer.Brand).AsSpan());
+ Alcohol = new byte[8];
+ MsgPackSpec.WriteFixString(Alcohol, nameof(Beer.Alcohol).AsSpan());
+ Sort = new byte[5];
+ MsgPackSpec.WriteFixString(Sort, nameof(Beer.Sort).AsSpan());
+ Brewery = new byte[8];
+ MsgPackSpec.WriteFixString(Brewery, nameof(Beer.Brewery).AsSpan());
+ }
- private byte[] _brand;
+ public BeerConverterHardCore(MsgPackContext context)
+ {
+ _stringFormatter = context.GetRequiredFormatter();
+ _listStringFormatter = context.GetRequiredFormatter>();
+ _floatFormatter = context.GetRequiredFormatter();
- private byte[] _alcohol;
+ _stringParser = context.GetRequiredParser();
+ _listStringParser = context.GetRequiredParser>();
+ _floatParser = context.GetRequiredParser();
+ }
- private byte[] _sort;
+ public int GetBufferSize(Beer value) => value == null
+ ? DataLengths.Nil
+ : _stringFormatter.GetBufferSize(value.Brand)
+ + _stringFormatter.GetBufferSize(value.Brewery)
+ + _listStringFormatter.GetBufferSize(value.Sort)
+ + _floatFormatter.GetBufferSize(value.Alcohol)
+ + 27;
- private byte[] _brewery;
+ public bool HasConstantSize => false;
- public void Write(Beer value, IMsgPackWriter writer)
+ public int Format(Span destination, Beer value)
{
- if (value == null)
- {
- _context.NullConverter.Write(null, writer);
- return;
- }
+ if (value == null) return MsgPackSpec.WriteNil(destination);
+
+ var result = MsgPackSpec.WriteMapHeader(destination, 4);
- writer.WriteMapHeader(4);
- writer.Write(_brand);
- _stringConverter.Write(value.Brand, writer);
+ Brand.CopyTo(destination.Slice(result));
+ result += Brand.Length;
+ result += _stringFormatter.Format(destination, value.Brand);
- writer.Write(_sort);
- _listStringConverter.Write(value.Sort, writer);
+ Sort.CopyTo(destination.Slice(result));
+ result += Sort.Length;
+ result += _listStringFormatter.Format(destination, value.Sort);
- writer.Write(_alcohol);
- _floatConverter.Write(value.Alcohol, writer);
+ Alcohol.CopyTo(destination.Slice(result));
+ result += Alcohol.Length;
+ result += _floatFormatter.Format(destination, value.Alcohol);
- writer.Write(_brewery);
- _stringConverter.Write(value.Brewery, writer);
+ Brewery.CopyTo(destination.Slice(result));
+ result += Brewery.Length;
+ result += _stringFormatter.Format(destination, value.Brewery);
+
+ return result;
}
- public Beer Read(IMsgPackReader reader)
+ public Beer Parse(ReadOnlySpan source, out int readSize)
{
- var length = reader.ReadMapLength();
- if (length == null)
- {
- return null;
- }
+ if (MsgPackSpec.TryReadNil(source, out readSize)) return null;
+ var length = MsgPackSpec.ReadMapHeader(source, out readSize);
if (length != 4)
{
throw new SerializationException("Bad format");
}
var result = new Beer();
- for (var i = 0; i < length.Value; i++)
+ for (var i = 0; i < length; i++)
{
- var propertyName = _stringConverter.Read(reader);
+ var propertyName = _stringParser.Parse(source, out var temp);
+ readSize += temp;
switch (propertyName)
{
case nameof(result.Brand):
- result.Brand = _stringConverter.Read(reader);
+ result.Brand = _stringParser.Parse(source, out temp);
+ readSize += temp;
break;
case nameof(result.Sort):
- result.Sort = _listStringConverter.Read(reader);
+ result.Sort = _listStringParser.Parse(source, out temp);
+ readSize += temp;
break;
case nameof(result.Alcohol):
- result.Alcohol = _floatConverter.Read(reader);
+ result.Alcohol = _floatParser.Parse(source, out temp);
+ readSize += temp;
break;
case nameof(result.Brewery):
- result.Brewery = _stringConverter.Read(reader);
+ result.Brewery = _stringParser.Parse(source, out temp);
+ readSize += temp;
break;
default:
throw new SerializationException("Bad format");
@@ -81,17 +117,5 @@ public Beer Read(IMsgPackReader reader)
return result;
}
-
- public void Initialize(MsgPackContext context)
- {
- _stringConverter = context.GetConverter();
- _listStringConverter = context.GetConverter>();
- _floatConverter = context.GetConverter();
- _brand = MsgPackSerializer.Serialize(nameof(Beer.Brand));
- _alcohol = MsgPackSerializer.Serialize(nameof(Beer.Alcohol));
- _sort = MsgPackSerializer.Serialize(nameof(Beer.Sort));
- _brewery = MsgPackSerializer.Serialize(nameof(Beer.Brewery));
- _context = context;
- }
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/Data/BeerType.cs b/src/msgpack.light.benchmark/Data/BeerType.cs
index a3898d6..d1bfc9a 100644
--- a/src/msgpack.light.benchmark/Data/BeerType.cs
+++ b/src/msgpack.light.benchmark/Data/BeerType.cs
@@ -2,10 +2,10 @@
{
public enum BeerType
{
- Ale,
- Lager,
- Stout,
- Porter,
- Malt
+ Ale = 0,
+ Lager = 1,
+ Stout = 2,
+ Porter = 3,
+ Malt = 4
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/Data/BeerTypeConverter.cs b/src/msgpack.light.benchmark/Data/BeerTypeConverter.cs
index 1844ee5..a501eef 100644
--- a/src/msgpack.light.benchmark/Data/BeerTypeConverter.cs
+++ b/src/msgpack.light.benchmark/Data/BeerTypeConverter.cs
@@ -2,23 +2,14 @@
namespace ProGaudi.MsgPack.Light.Benchmark.Data
{
- public class BeerTypeConverter : IMsgPackConverter
+ public class BeerTypeConverter : IMsgPackFormatter, IMsgPackParser
{
- private Lazy> _intConverter;
+ public int GetBufferSize(BeerType value) => DataLengths.PositiveFixInt;
- public void Initialize(MsgPackContext context)
- {
- _intConverter = new Lazy>(context.GetConverter);
- }
+ public bool HasConstantSize => true;
- public void Write(BeerType value, IMsgPackWriter writer)
- {
- _intConverter.Value.Write((int)value, writer);
- }
+ public int Format(Span destination, BeerType value) => MsgPackSpec.WritePositiveFixInt(destination, (byte)value);
- public BeerType Read(IMsgPackReader reader)
- {
- return (BeerType)_intConverter.Value.Read(reader);
- }
+ public BeerType Parse(ReadOnlySpan source, out int readSize) => (BeerType) MsgPackSpec.ReadPositiveFixInt(source, out readSize);
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/EnumGeneratedDeserializeBenchmark.cs b/src/msgpack.light.benchmark/EnumGeneratedDeserializeBenchmark.cs
deleted file mode 100644
index 9ddc9d6..0000000
--- a/src/msgpack.light.benchmark/EnumGeneratedDeserializeBenchmark.cs
+++ /dev/null
@@ -1,48 +0,0 @@
-using System.IO;
-
-using BenchmarkDotNet.Attributes;
-
-using ProGaudi.MsgPack.Light.Benchmark.Data;
-
-namespace ProGaudi.MsgPack.Light.Benchmark
-{
- [Config(typeof(BenchmarkConfig))]
- public class EnumGeneratedDeserializeBenchmark
- {
- private readonly byte[] _msgPackForInt;
-
- private readonly byte[] _msgPackArray;
-
- public EnumGeneratedDeserializeBenchmark()
- {
- var beerType = BeerType.Lager;
- _msgPackArray = PrepareMsgPack(beerType).ToArray();
- _msgPackForInt = PrepareMsgPack((int)beerType).ToArray();
- }
-
- private MemoryStream PrepareMsgPack(T item)
- {
- var memoryStream = new MemoryStream();
- MsgPackSerializer.Serialize(item, memoryStream, Serializers.MsgPackLight);
- return memoryStream;
- }
-
- [Benchmark]
- public void MPLight_AutoEnum()
- {
- var beerType = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLight);
- }
-
- [Benchmark(Baseline = true)]
- public void MPLight_IntEnum()
- {
- var beerType = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightIntEnum);
- }
-
- [Benchmark]
- public void MPLight_Int()
- {
- var beerType = MsgPackSerializer.Deserialize(_msgPackForInt, Serializers.MsgPackLight);
- }
- }
-}
\ No newline at end of file
diff --git a/src/msgpack.light.benchmark/EnumGeneratedSerializeBenchmark.cs b/src/msgpack.light.benchmark/EnumGeneratedSerializeBenchmark.cs
deleted file mode 100644
index 2dc7b6e..0000000
--- a/src/msgpack.light.benchmark/EnumGeneratedSerializeBenchmark.cs
+++ /dev/null
@@ -1,38 +0,0 @@
-using BenchmarkDotNet.Attributes;
-
-using ProGaudi.MsgPack.Light.Benchmark.Data;
-
-namespace ProGaudi.MsgPack.Light.Benchmark
-{
- [Config(typeof(BenchmarkConfig))]
- public class EnumGeneratedSerializeBenchmark
- {
- private readonly BeerType _beerType;
-
- private readonly int _beerTypeIntRepresentation;
-
- public EnumGeneratedSerializeBenchmark()
- {
- _beerType = BeerType.Malt;
- _beerTypeIntRepresentation = (int) _beerType;
- }
-
- [Benchmark(Baseline = true)]
- public void MPLight_IntEnum()
- {
- var bytes = MsgPackSerializer.Serialize(_beerType, Serializers.MsgPackLightIntEnum);
- }
-
- [Benchmark]
- public void MPLight_AutoEnum()
- {
- var bytes = MsgPackSerializer.Serialize(_beerType, Serializers.MsgPackLight);
- }
-
- [Benchmark]
- public void MPLight_Int()
- {
- var bytes = MsgPackSerializer.Serialize(_beerTypeIntRepresentation, Serializers.MsgPackLight);
- }
- }
-}
diff --git a/src/msgpack.light.benchmark/MapGeneratedDeserializeBenchmark.cs b/src/msgpack.light.benchmark/MapGeneratedDeserializeBenchmark.cs
deleted file mode 100644
index bc41ba2..0000000
--- a/src/msgpack.light.benchmark/MapGeneratedDeserializeBenchmark.cs
+++ /dev/null
@@ -1,75 +0,0 @@
-using System.IO;
-using System.Text;
-
-using BenchmarkDotNet.Attributes;
-
-using ProGaudi.MsgPack.Light.Benchmark.Data;
-
-namespace ProGaudi.MsgPack.Light.Benchmark
-{
- [Config(typeof(BenchmarkConfig))]
- public class MapGeneratedDeserializeBenchmark
- {
- private readonly MemoryStream _json;
-
- private readonly MemoryStream _msgPack;
-
- private readonly byte[] _msgPackArray;
-
- public MapGeneratedDeserializeBenchmark()
- {
- var serializer = new BeerSerializeBenchmark();
- _json = PrepareJson(serializer);
- _msgPack = PrepareMsgPack(serializer);
- _msgPackArray = _msgPack.ToArray();
- }
-
- private MemoryStream PrepareMsgPack(BeerSerializeBenchmark serializer)
- {
- var memoryStream = new MemoryStream();
- serializer.MsgPackSerialize(memoryStream);
- return memoryStream;
- }
-
- private MemoryStream PrepareJson(BeerSerializeBenchmark serializer)
- {
- var memoryStream = new MemoryStream();
- serializer.JsonSerialize(memoryStream);
- return memoryStream;
- }
-
- [Benchmark]
- public void JsonNet()
- {
- _json.Seek(0, SeekOrigin.Begin);
- using (var reader = new StreamReader(_json, Encoding.UTF8, false, 1024, true))
- {
- var beer = Serializers.Newtonsoft.Deserialize(reader, typeof(Beer));
- }
- }
-
- [Benchmark(Baseline = true)]
- public void MPCli_Array()
- {
- var beer = Serializers.MsgPack.GetSerializer().UnpackSingleObject(_msgPackArray);
- }
-
- [Benchmark]
- public void MPLight_Array()
- {
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLight);
- }
-
- [Benchmark]
- public void MPLight_Array_AutoMap()
- {
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightMapAutoGeneration);
- }
-
- [Benchmark]
- public void MPLight_Array_AutoArray()
- {
- var beer = MsgPackSerializer.Deserialize(_msgPackArray, Serializers.MsgPackLightArrayAutoGeneration);
- }
- }
-}
\ No newline at end of file
diff --git a/src/msgpack.light.benchmark/MapGeneratedSerializeBenchmark.cs b/src/msgpack.light.benchmark/MapGeneratedSerializeBenchmark.cs
deleted file mode 100644
index 0b6ecb4..0000000
--- a/src/msgpack.light.benchmark/MapGeneratedSerializeBenchmark.cs
+++ /dev/null
@@ -1,62 +0,0 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Text;
-
-using BenchmarkDotNet.Attributes;
-
-using ProGaudi.MsgPack.Light.Benchmark.Data;
-
-namespace ProGaudi.MsgPack.Light.Benchmark
-{
- [Config(typeof(BenchmarkConfig))]
- public class MapGeneratedSerializeBenchmark
- {
- private readonly Beer _testBeer;
-
- public MapGeneratedSerializeBenchmark()
- {
- _testBeer = new Beer
- {
- Brand = "Westvleteren Zes",
- Alcohol = 6.2F,
- Brewery = "Sint-Sixtusabdij van Westvleteren",
- Sort = new List {"trappist"}
- };
- }
-
- [Benchmark]
- public void JsonNet()
- {
- var memoryStream = new MemoryStream();
- using (var writer = new StreamWriter(memoryStream, Encoding.UTF8, 1024, true))
- {
- Serializers.Newtonsoft.Serialize(writer, _testBeer);
- writer.Flush();
- }
- }
-
- [Benchmark(Baseline = true)]
- public void MPCli_Array()
- {
- var bytes = Serializers.MsgPack.GetSerializer().PackSingleObject(_testBeer);
- }
-
- [Benchmark]
- public void MPLight_Array()
- {
- var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLight);
- }
-
- [Benchmark]
- public void MPLight_Array_AutoMap()
- {
- var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightMapAutoGeneration);
- }
-
- [Benchmark]
- public void MPLight_Array_AutoArray()
- {
- var bytes = MsgPackSerializer.Serialize(_testBeer, Serializers.MsgPackLightArrayAutoGeneration);
- }
- }
-}
diff --git a/src/msgpack.light.benchmark/NumberDeserialize.cs b/src/msgpack.light.benchmark/NumberDeserialize.cs
index 6904bc5..a6b881b 100644
--- a/src/msgpack.light.benchmark/NumberDeserialize.cs
+++ b/src/msgpack.light.benchmark/NumberDeserialize.cs
@@ -15,7 +15,7 @@ public abstract class NumberDeserialize
private readonly MemoryStream _stream;
- private readonly MsgPackContext _mplightContext;
+ private readonly MsgPackContext _mpLightContext;
protected abstract T[] Numbers { get; }
@@ -24,33 +24,26 @@ protected NumberDeserialize()
_messagePackSerializer = SerializationContext.Default.GetSerializer();
_bytes = _messagePackSerializer.PackSingleObject(Numbers);
_stream = new MemoryStream(_bytes);
- _mplightContext = new MsgPackContext();
+ _mpLightContext = new MsgPackContext();
}
[Benchmark]
- public void MPCli_Array()
+ public T[] MPCli_Array()
{
- var data = _messagePackSerializer.UnpackSingleObject(_bytes);
+ return _messagePackSerializer.UnpackSingleObject(_bytes);
}
[Benchmark(Baseline = true)]
- public void MPCli_Stream()
+ public T[] MPCli_Stream()
{
_stream.Seek(0, SeekOrigin.Begin);
- var data = _messagePackSerializer.Unpack(_stream);
+ return _messagePackSerializer.Unpack(_stream);
}
[Benchmark]
- public void MPLight_Array()
+ public T[] MPLight_Array()
{
- var data = MsgPackSerializer.Deserialize(_bytes, _mplightContext);
- }
-
- [Benchmark]
- public void MPLight_Stream()
- {
- _stream.Seek(0, SeekOrigin.Begin);
- var data = MsgPackSerializer.Deserialize(_stream, _mplightContext);
+ return MsgPackSerializer.Deserialize(_bytes, _mpLightContext, out _);
}
}
}
diff --git a/src/msgpack.light.benchmark/NumberSerialize.cs b/src/msgpack.light.benchmark/NumberSerialize.cs
index 91ce90f..bace8a0 100644
--- a/src/msgpack.light.benchmark/NumberSerialize.cs
+++ b/src/msgpack.light.benchmark/NumberSerialize.cs
@@ -1,4 +1,5 @@
-using System.IO;
+using System.Buffers;
+using System.IO;
using BenchmarkDotNet.Attributes;
@@ -11,40 +12,39 @@ public abstract class NumberSerialize
{
private readonly MessagePackSerializer _messagePackSerializer;
- private readonly MsgPackContext _mplightContext;
+ private readonly MsgPackContext _mpLightContext;
+
+ private readonly MemoryStream _stream = Serializers.CreateStream();
+
+ private readonly IMemoryOwner _buffer;
protected abstract T[] Numbers { get; }
protected NumberSerialize()
{
_messagePackSerializer = SerializationContext.Default.GetSerializer();
- _mplightContext = new MsgPackContext();
+ _mpLightContext = new MsgPackContext();
+ _buffer = MsgPackSerializer.Serialize(Numbers, _mpLightContext, out _);
}
[Benchmark]
- public void MPCli_Array()
+ public byte[] MPCli_Array()
{
- var bytes = _messagePackSerializer.PackSingleObject(Numbers);
+ return _messagePackSerializer.PackSingleObject(Numbers);
}
[Benchmark(Baseline = true)]
- public void MPCli_Stream()
- {
- var stream = new MemoryStream();
- _messagePackSerializer.Pack(stream, Numbers);
- }
-
- [Benchmark]
- public void MPLight_Array()
+ public long MPCli_Stream()
{
- var bytes = MsgPackSerializer.Serialize(Numbers, _mplightContext);
+ _stream.Seek(0, SeekOrigin.Begin);
+ _messagePackSerializer.Pack(_stream, Numbers);
+ return _stream.Position;
}
[Benchmark]
- public void MPLight_Stream()
+ public int MPLight_Array()
{
- var stream = new MemoryStream();
- MsgPackSerializer.Serialize(Numbers, stream, _mplightContext);
+ return MsgPackSerializer.Serialize(Numbers, _buffer.Memory.Span, _mpLightContext);
}
}
}
diff --git a/src/msgpack.light.benchmark/Program.cs b/src/msgpack.light.benchmark/Program.cs
index 679f7be..26f57e6 100644
--- a/src/msgpack.light.benchmark/Program.cs
+++ b/src/msgpack.light.benchmark/Program.cs
@@ -9,8 +9,8 @@ public static void Main(string[] args)
var switcher = new BenchmarkSwitcher(
new[]
{
- typeof (MapGeneratedSerializeBenchmark),
- typeof (MapGeneratedDeserializeBenchmark),
+// typeof (MapGeneratedSerializeBenchmark),
+// typeof (MapGeneratedDeserializeBenchmark),
typeof (BeerSerializeBenchmark),
typeof (BeerDeserializeBenchmark),
typeof (BeerListSerializeBenchmark),
@@ -21,8 +21,8 @@ public static void Main(string[] args)
typeof (DoubleSerialize),
typeof (BeerSkip),
typeof (BeerSkipList),
- typeof (EnumGeneratedDeserializeBenchmark),
- typeof (EnumGeneratedSerializeBenchmark)
+// typeof (EnumGeneratedDeserializeBenchmark),
+// typeof (EnumGeneratedSerializeBenchmark)
});
switcher.Run(args);
}
diff --git a/src/msgpack.light.benchmark/Serializers.cs b/src/msgpack.light.benchmark/Serializers.cs
index ef8b25c..7cc77cb 100644
--- a/src/msgpack.light.benchmark/Serializers.cs
+++ b/src/msgpack.light.benchmark/Serializers.cs
@@ -1,4 +1,6 @@
-using MsgPack.Serialization;
+using System.IO;
+
+using MsgPack.Serialization;
using ProGaudi.MsgPack.Light.Benchmark.Data;
@@ -11,21 +13,21 @@ public static class Serializers
public static readonly JsonSerializer Newtonsoft = new JsonSerializer();
public static readonly SerializationContext MsgPack = new SerializationContext();
public static readonly SerializationContext MsgPackHardcore = new SerializationContext();
- public static readonly MsgPackContext MsgPackLight = new MsgPackContext(convertEnumsAsStrings: false);
+ public static readonly MsgPackContext MsgPackLight = new MsgPackContext();
public static readonly MsgPackContext MsgPackLightHardcore = new MsgPackContext();
- public static readonly MsgPackContext MsgPackLightMapAutoGeneration = new MsgPackContext();
- public static readonly MsgPackContext MsgPackLightArrayAutoGeneration = new MsgPackContext();
- public static readonly MsgPackContext MsgPackLightIntEnum = new MsgPackContext();
static Serializers()
{
- MsgPackLight.RegisterConverter(new BeerConverter());
- MsgPackLightHardcore.RegisterConverter(new BeerConverterHardCore());
+ MsgPackLight.RegisterFormatter(x => new BeerConverter(x));
+ MsgPackLight.RegisterParser(x => new BeerConverter(x));
+
+ MsgPackLightHardcore.RegisterFormatter(x => new BeerConverterHardCore(x));
+ MsgPackLightHardcore.RegisterParser(x => new BeerConverterHardCore(x));
+
MsgPack.Serializers.Register(new BeerSerializer(MsgPack));
MsgPackHardcore.Serializers.Register(new BeerSerializer(MsgPackHardcore));
- MsgPackLightMapAutoGeneration.GenerateAndRegisterMapConverter();
- MsgPackLightArrayAutoGeneration.GenerateAndRegisterArrayConverter();
- MsgPackLightIntEnum.RegisterConverter(new BeerTypeConverter());
}
+
+ public static MemoryStream CreateStream() => new MemoryStream(1_000_000);
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/SkipConverter.cs b/src/msgpack.light.benchmark/SkipConverter.cs
index 70099bd..baf66e9 100644
--- a/src/msgpack.light.benchmark/SkipConverter.cs
+++ b/src/msgpack.light.benchmark/SkipConverter.cs
@@ -1,21 +1,14 @@
-namespace ProGaudi.MsgPack.Light.Benchmark
+using System;
+
+namespace ProGaudi.MsgPack.Light.Benchmark
{
- public class SkipConverter :IMsgPackConverter
+ public class SkipConverter : IMsgPackParser
{
- public void Initialize(MsgPackContext context)
- {
-
- }
-
- public void Write(T value, IMsgPackWriter writer)
- {
- throw new System.NotImplementedException();
- }
-
- public T Read(IMsgPackReader reader)
+ public T Parse(ReadOnlySpan source, out int readSize)
{
- reader.SkipToken();
- return default(T);
+ var token = MsgPackSpec.ReadToken(source);
+ readSize = token.Length;
+ return default;
}
}
-}
\ No newline at end of file
+}
diff --git a/src/msgpack.light.benchmark/msgpack.light.benchmark.csproj b/src/msgpack.light.benchmark/msgpack.light.benchmark.csproj
index 4fd5cf0..ee65332 100644
--- a/src/msgpack.light.benchmark/msgpack.light.benchmark.csproj
+++ b/src/msgpack.light.benchmark/msgpack.light.benchmark.csproj
@@ -3,7 +3,7 @@
msgpack.light.benchmark Console Application
aensidhe
- netcoreapp1.1;netcoreapp2.0
+ netcoreapp2.0;netcoreapp2.1
msgpack.light.benchmark
ProGaudi.MsgPack.Light.Benchmark
Exe
@@ -18,13 +18,14 @@
false
false
+ latest
-
-
-
-
+
+
+
+
diff --git a/src/msgpack.light/BadCodeConstraintException.cs b/src/msgpack.light/BadCodeConstraintException.cs
new file mode 100644
index 0000000..1148ab8
--- /dev/null
+++ b/src/msgpack.light/BadCodeConstraintException.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace ProGaudi.MsgPack
+{
+ [Serializable]
+ public class BadCodeConstraintException : ConstraintViolationException
+ {
+ public byte DataCode { get; }
+
+ public HashSet AllowedCodes { get; }
+
+ public BadCodeConstraintException(byte dataCode, HashSet allowedCodes, Exception innerException = null)
+ : base($"{dataCode:x2} should be one of ({string.Join(", ", allowedCodes.Select(x => $"{x:x2}"))}), but it doesn't.", innerException)
+ {
+ DataCode = dataCode;
+ AllowedCodes = allowedCodes;
+ }
+ }
+}
diff --git a/src/msgpack.light/BadSizeConstraintException.cs b/src/msgpack.light/BadSizeConstraintException.cs
new file mode 100644
index 0000000..a610c50
--- /dev/null
+++ b/src/msgpack.light/BadSizeConstraintException.cs
@@ -0,0 +1,28 @@
+using System;
+
+namespace ProGaudi.MsgPack
+{
+ [Serializable]
+ public class BadSizeConstraintException : ConstraintViolationException
+ {
+ public DataFamily DataFamily { get; }
+
+ public byte DataCode { get; }
+
+ public int RequestedSize { get; }
+
+ public BadSizeConstraintException(DataFamily dataFamily, byte dataCode, int requestedSize, Exception innerException = null)
+ : base(M(dataFamily, dataCode, requestedSize), innerException)
+ {
+ DataFamily = dataFamily;
+ DataCode = dataCode;
+ RequestedSize = requestedSize;
+ }
+
+ private static string M(DataFamily dataFamily, byte dataCode, int requestedSize)
+ {
+ var (min, max) = DataLengths.GetMinAndMaxLength(dataCode);
+ return $"Length of {dataCode} of {dataFamily} should belong to [{min}, {max}]. {requestedSize} doesn't";
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/msgpack.light/BaseMsgPackReader.cs b/src/msgpack.light/BaseMsgPackReader.cs
deleted file mode 100644
index 1823685..0000000
--- a/src/msgpack.light/BaseMsgPackReader.cs
+++ /dev/null
@@ -1,211 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-
-using ProGaudi.MsgPack.Light.Converters;
-
-namespace ProGaudi.MsgPack.Light
-{
- internal abstract class BaseMsgPackReader : IMsgPackReader
- {
- public abstract byte ReadByte();
-
- public abstract ArraySegment ReadBytes(uint length);
-
- public abstract void Seek(long offset, SeekOrigin origin);
-
- public uint? ReadArrayLength()
- {
- var type = ReadDataType();
- switch (type)
- {
- case DataTypes.Null:
- return null;
- case DataTypes.Array16:
- return NumberConverter.ReadUInt16(this);
-
- case DataTypes.Array32:
- return NumberConverter.ReadUInt32(this);
- }
-
- var length = TryGetLengthFromFixArray(type);
-
- if (length.HasValue)
- {
- return length;
- }
-
- throw ExceptionUtils.BadTypeException(type, DataTypes.Array16, DataTypes.Array32, DataTypes.FixArray, DataTypes.Null);
- }
-
- public uint? ReadMapLength()
- {
- var type = ReadDataType();
-
- switch (type)
- {
- case DataTypes.Null:
- return null;
- case DataTypes.Map16:
- return NumberConverter.ReadUInt16(this);
-
- case DataTypes.Map32:
- return NumberConverter.ReadUInt32(this);
- }
-
- var length = TryGetLengthFromFixMap(type);
- if (length.HasValue)
- return length.Value;
-
- throw ExceptionUtils.BadTypeException(type, DataTypes.Map16, DataTypes.Map32, DataTypes.FixMap, DataTypes.Null);
- }
-
- public virtual DataTypes ReadDataType()
- {
- return (DataTypes)ReadByte();
- }
-
- public void SkipToken()
- {
- var dataType = ReadDataType();
-
- switch (dataType)
- {
- case DataTypes.Null:
- case DataTypes.False:
- case DataTypes.True:
- return;
- case DataTypes.UInt8:
- case DataTypes.Int8:
- SkipBytes(1);
- return;
- case DataTypes.UInt16:
- case DataTypes.Int16:
- SkipBytes(2);
- return;
- case DataTypes.UInt32:
- case DataTypes.Int32:
- case DataTypes.Single:
- SkipBytes(4);
- return;
- case DataTypes.UInt64:
- case DataTypes.Int64:
- case DataTypes.Double:
- SkipBytes(8);
- return;
- case DataTypes.Array16:
- SkipArrayItems(NumberConverter.ReadUInt16(this));
- return;
- case DataTypes.Array32:
- SkipArrayItems(NumberConverter.ReadUInt32(this));
- return;
- case DataTypes.Map16:
- SkipMapItems(NumberConverter.ReadUInt16(this));
- return;
- case DataTypes.Map32:
- SkipMapItems(NumberConverter.ReadUInt32(this));
- return;
- case DataTypes.Str8:
- SkipBytes(NumberConverter.ReadUInt8(this));
- return;
- case DataTypes.Str16:
- SkipBytes(NumberConverter.ReadUInt16(this));
- return;
- case DataTypes.Str32:
- SkipBytes(NumberConverter.ReadUInt32(this));
- return;
- case DataTypes.Bin8:
- SkipBytes(NumberConverter.ReadUInt8(this));
- return;
- case DataTypes.Bin16:
- SkipBytes(NumberConverter.ReadUInt16(this));
- return;
- case DataTypes.Bin32:
- SkipBytes(NumberConverter.ReadUInt32(this));
- return;
- }
-
- if (dataType.GetHighBits(1) == DataTypes.PositiveFixNum.GetHighBits(1) ||
- dataType.GetHighBits(3) == DataTypes.NegativeFixNum.GetHighBits(3))
- {
- return;
- }
-
- var arrayLength = TryGetLengthFromFixArray(dataType);
- if (arrayLength.HasValue)
- {
- SkipArrayItems(arrayLength.Value);
- return;
- }
-
- var mapLength = TryGetLengthFromFixMap(dataType);
- if (mapLength.HasValue)
- {
- SkipMapItems(mapLength.Value);
- return;
- }
-
- var stringLength = TryGetLengthFromFixStr(dataType);
- if (stringLength.HasValue)
- {
- SkipBytes(stringLength.Value);
- return;
- }
-
- throw new ArgumentOutOfRangeException();
- }
-
- private void SkipMapItems(uint count)
- {
- for (var i = 0; i < count; i++)
- {
- SkipToken();
- SkipToken();
- }
- }
-
- private void SkipArrayItems(uint count)
- {
- for (var i = 0; i < count; i++)
- {
- SkipToken();
- }
- }
-
- private void SkipBytes(uint bytesCount)
- {
- Seek(bytesCount, SeekOrigin.Current);
- }
-
- private static uint? TryGetLengthFromFixStr(DataTypes type)
- {
- var length = type - DataTypes.FixStr;
- return type.GetHighBits(3) == DataTypes.FixStr.GetHighBits(3) ? length : (uint?)null;
- }
-
- protected static uint? TryGetLengthFromFixArray(DataTypes type)
- {
- var length = type - DataTypes.FixArray;
- return type.GetHighBits(4) == DataTypes.FixArray.GetHighBits(4) ? length : (uint?)null;
- }
-
- protected static uint? TryGetLengthFromFixMap(DataTypes type)
- {
- var length = type - DataTypes.FixMap;
- return type.GetHighBits(4) == DataTypes.FixMap.GetHighBits(4) ? length : (uint?)null;
- }
-
- public byte[] ReadToken()
- {
- StartTokenGathering();
- SkipToken();
- var gatheredBytes = StopTokenGathering();
- return gatheredBytes.ToArray();
- }
-
- protected abstract IList StopTokenGathering();
-
- protected abstract void StartTokenGathering();
- }
-}
\ No newline at end of file
diff --git a/src/msgpack.light/CodeDoesntBelongFamilyException.cs b/src/msgpack.light/CodeDoesntBelongFamilyException.cs
new file mode 100644
index 0000000..e274018
--- /dev/null
+++ b/src/msgpack.light/CodeDoesntBelongFamilyException.cs
@@ -0,0 +1,19 @@
+using System;
+
+namespace ProGaudi.MsgPack
+{
+ [Serializable]
+ public class CodeDoesntBelongFamilyException : ConstraintViolationException
+ {
+ public byte DataCode { get; }
+
+ public DataFamily Binary { get; }
+
+ public CodeDoesntBelongFamilyException(byte dataCode, DataFamily binary, Exception innerException = null)
+ : base($"We expect {binary} data family, but we have {dataCode}, it belongs to {MsgPackSpec.GetDataFamily(dataCode)}.", innerException)
+ {
+ DataCode = dataCode;
+ Binary = binary;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/msgpack.light/CompiledLambdaActivatorFactory.cs b/src/msgpack.light/CompiledLambdaActivatorFactory.cs
index 7a072e2..e6bd764 100644
--- a/src/msgpack.light/CompiledLambdaActivatorFactory.cs
+++ b/src/msgpack.light/CompiledLambdaActivatorFactory.cs
@@ -1,26 +1,65 @@
using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
-namespace ProGaudi.MsgPack.Light
+namespace ProGaudi.MsgPack
{
public static class CompiledLambdaActivatorFactory
{
- public static Func