Skip to content

Commit 005f0f6

Browse files
authored
Merge pull request #5 from agocke/p5
Update to work with preview 5
2 parents 5aa7216 + 50f9b4a commit 005f0f6

24 files changed

+805
-476
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# This is only for local build
2+
PackageRedirects.props
3+
14
## Ignore Visual Studio temporary files, build results, and
25
## files generated by popular Visual Studio add-ons.
36
##

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
"program": "${workspaceFolder}/artifacts/bin/bench/debug/bench.dll",
13+
"args": [],
14+
"cwd": "${workspaceFolder}/bench",
15+
"console": "internalConsole",
16+
"stopAtEntry": false
17+
},
18+
{
19+
"name": ".NET Core Attach",
20+
"type": "coreclr",
21+
"request": "attach"
22+
}
23+
]
24+
}

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/serde.msgpack.sln",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary;ForceNoAlign"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/serde.msgpack.sln",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary;ForceNoAlign"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/serde.msgpack.sln"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

Directory.Build.targets

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project>
2+
<Import Project="$(MSBuildThisFileDirectory)PackageRedirects.props" Condition="Exists('$(MSBuildThisFileDirectory)PackageRedirects.props')" />
3+
<ItemGroup>
4+
<!-- Remove all items from PackageReference if they are in the PackageRedirect ItemGroup with a matching PackageName
5+
element. Then add a ProjectReference to the project with the item identity in PackageRedirect. -->
6+
<_ExcludePackage Include="@(PackageRedirect)" Exclude="@(PackageReference)">
7+
<MatchOnMetadata>PackageName</MatchOnMetadata>
8+
<ProjectPath>%(PackageName)</ProjectPath>
9+
</_ExcludePackage>
10+
<_ExcludeProjects Include ="@(_ExcludePackage->Metadata('ProjectPath'))" />
11+
<ProjectReference Include="@(PackageRedirect)" Exclude="@(_ExcludeProjects)">
12+
<ReferenceOutputAssembly>%(ReferenceOutputAssembly)</ReferenceOutputAssembly>
13+
<OutputItemType>%(OutputItemType)</OutputItemType>
14+
</ProjectReference>
15+
16+
<PackageReference Remove="%(PackageRedirect.PackageName)" />
17+
</ItemGroup>
18+
</Project>

bench/DataGenerator.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,6 @@ public static T GenerateSerialize<T>() where T : Serde.ISerializeProvider<T>
2424

2525
}
2626

27-
public static Location CreateLocation() => new Location
28-
{
29-
Id = 1234,
30-
Address1 = "The Street Name",
31-
Address2 = "20/11",
32-
City = "The City",
33-
State = "The State",
34-
PostalCode = "abc-12",
35-
Name = "Nonexisting",
36-
PhoneNumber = "+0 11 222 333 44",
37-
Country = "The Greatest"
38-
};
39-
4027
public static byte[] GenerateDeserialize<T>()
4128
{
4229
if (typeof(T) == typeof(LoginViewModel))

bench/DeserializeFromString.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88

99
namespace Benchmarks
1010
{
11-
[GenericTypeArguments(typeof(Location), typeof(LocationWrap))]
12-
public class DeserializeFromString<T, U>
11+
[GenericTypeArguments(typeof(Location))]
12+
public class DeserializeFromString<T>
1313
where T : Serde.IDeserializeProvider<T>
14-
where U : Serde.IDeserializeProvider<T>
1514
{
1615
private byte[] value = null!;
1716

18-
private readonly IDeserialize<T> _proxy = T.DeserializeInstance;
19-
private readonly IDeserialize<T> _manualProxy = U.DeserializeInstance;
17+
private readonly IDeserialize<T> _proxy = T.Instance;
2018

2119
[GlobalSetup]
2220
public void Setup()
@@ -32,11 +30,5 @@ public void Setup()
3230

3331
[Benchmark]
3432
public T SerdeMsgPack() => Serde.MsgPack.MsgPackSerializer.Deserialize<T, IDeserialize<T>>(value, _proxy);
35-
36-
[Benchmark]
37-
public T SerdeMsgPackManual() => Serde.MsgPack.MsgPackSerializer.Deserialize<T, IDeserialize<T>>(value, _manualProxy);
38-
39-
// DataContractJsonSerializer does not provide an API to serialize to string
40-
// so it's not included here (apples vs apples thing)
4133
}
4234
}

bench/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616

1717
var loc1 = MessagePackSerializer.Deserialize<Location>(msg1);
18-
var loc2 = Serde.MsgPack.MsgPackSerializer.Deserialize<Location, LocationWrap>(msg1, LocationWrap.Instance);
18+
var loc2 = Serde.MsgPack.MsgPackSerializer.Deserialize<Location>(msg1);
1919

2020
Console.WriteLine("Checking correctness of serialization: " + (loc1 == loc2));
2121
if (loc1 != loc2)
@@ -31,5 +31,5 @@ Serialization is not correct
3131
}
3232

3333
var config = DefaultConfig.Instance.AddDiagnoser(MemoryDiagnoser.Default);
34-
var summary = BenchmarkSwitcher.FromAssembly(typeof(DeserializeFromString<,>).Assembly)
34+
var summary = BenchmarkSwitcher.FromAssembly(typeof(DeserializeFromString<>).Assembly)
3535
.Run(args, config);

bench/SampleTypes.cs

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,18 @@ public partial class LoginViewModel
1717
}
1818

1919
[GenerateSerialize, GenerateDeserialize]
20-
[MessagePackObject]
20+
[MessagePackObject(keyAsPropertyName: true)]
21+
[SerdeTypeOptions(MemberFormat = MemberFormat.None)]
2122
public partial record Location
2223
{
23-
[Key(0)]
2424
public int Id { get; set; }
25-
[Key(1)]
2625
public string Address1 { get; set; }
27-
[Key(2)]
2826
public string Address2 { get; set; }
29-
[Key(3)]
3027
public string City { get; set; }
31-
[Key(4)]
3228
public string State { get; set; }
33-
[Key(5)]
3429
public string PostalCode { get; set; }
35-
[Key(6)]
3630
public string Name { get; set; }
37-
[Key(7)]
3831
public string PhoneNumber { get; set; }
39-
[Key(8)]
4032
public string Country { get; set; }
4133

4234
public static Location Sample => new Location
@@ -52,110 +44,4 @@ public partial record Location
5244
Country = "The Greatest"
5345
};
5446
}
55-
56-
public sealed partial class LocationWrap : IDeserialize<Location>, IDeserializeProvider<Location>
57-
{
58-
public static LocationWrap Instance { get; } = new();
59-
static IDeserialize<Location> IDeserializeProvider<Location>.DeserializeInstance => Instance;
60-
private LocationWrap() { }
61-
62-
public static ISerdeInfo SerdeInfo { get; } = Serde.SerdeInfo.MakeCustom(
63-
"Location",
64-
typeof(Location).GetCustomAttributesData(),
65-
[
66-
("id", Int32Proxy.SerdeInfo, typeof(Location).GetProperty("Id")!),
67-
("address1", StringProxy.SerdeInfo, typeof(Location).GetProperty("Address1")!),
68-
("address2", StringProxy.SerdeInfo, typeof(Location).GetProperty("Address2")!),
69-
("city", StringProxy.SerdeInfo, typeof(Location).GetProperty("City")!),
70-
("state", StringProxy.SerdeInfo, typeof(Location).GetProperty("State")!),
71-
("postalCode", StringProxy.SerdeInfo, typeof(Location).GetProperty("PostalCode")!),
72-
("name", StringProxy.SerdeInfo, typeof(Location).GetProperty("Name")!),
73-
("phoneNumber", StringProxy.SerdeInfo, typeof(Location).GetProperty("PhoneNumber")!),
74-
("country", StringProxy.SerdeInfo, typeof(Location).GetProperty("Country")!)
75-
]);
76-
77-
Benchmarks.Location Serde.IDeserialize<Benchmarks.Location>.Deserialize(IDeserializer deserializer)
78-
{
79-
int _l_id = default !;
80-
string _l_address1 = default !;
81-
string _l_address2 = default !;
82-
string _l_city = default !;
83-
string _l_state = default !;
84-
string _l_postalcode = default !;
85-
string _l_name = default !;
86-
string _l_phonenumber = default !;
87-
string _l_country = default !;
88-
ushort _r_assignedValid = 0b0;
89-
90-
var _l_serdeInfo = SerdeInfo;
91-
var typeDeserialize = deserializer.ReadType(_l_serdeInfo);
92-
int index;
93-
while ((index = typeDeserialize.TryReadIndex(_l_serdeInfo, out _)) != IDeserializeType.EndOfType)
94-
{
95-
switch (index)
96-
{
97-
case 0:
98-
_l_id = typeDeserialize.ReadI32(index);
99-
_r_assignedValid |= ((ushort)1) << 0;
100-
break;
101-
case 1:
102-
_l_address1 = typeDeserialize.ReadString(index);
103-
_r_assignedValid |= ((ushort)1) << 1;
104-
break;
105-
case 2:
106-
_l_address2 = typeDeserialize.ReadString(index);
107-
_r_assignedValid |= ((ushort)1) << 2;
108-
break;
109-
case 3:
110-
_l_city = typeDeserialize.ReadString(index);
111-
_r_assignedValid |= ((ushort)1) << 3;
112-
break;
113-
case 4:
114-
_l_state = typeDeserialize.ReadString(index);
115-
_r_assignedValid |= ((ushort)1) << 4;
116-
break;
117-
case 5:
118-
_l_postalcode = typeDeserialize.ReadString(index);
119-
_r_assignedValid |= ((ushort)1) << 5;
120-
break;
121-
case 6:
122-
_l_name = typeDeserialize.ReadString(index);
123-
_r_assignedValid |= ((ushort)1) << 6;
124-
break;
125-
case 7:
126-
_l_phonenumber = typeDeserialize.ReadString(index);
127-
_r_assignedValid |= ((ushort)1) << 7;
128-
break;
129-
case 8:
130-
_l_country = typeDeserialize.ReadString(index);
131-
_r_assignedValid |= ((ushort)1) << 8;
132-
break;
133-
case Serde.IDeserializeType.IndexNotFound:
134-
typeDeserialize.SkipValue();
135-
break;
136-
default:
137-
throw new InvalidOperationException("Unexpected index: " + index);
138-
}
139-
}
140-
141-
if (_r_assignedValid != 0b111111111)
142-
{
143-
throw Serde.DeserializeException.UnassignedMember();
144-
}
145-
146-
var newType = new Benchmarks.Location()
147-
{
148-
Id = _l_id,
149-
Address1 = _l_address1,
150-
Address2 = _l_address2,
151-
City = _l_city,
152-
State = _l_state,
153-
PostalCode = _l_postalcode,
154-
Name = _l_name,
155-
PhoneNumber = _l_phonenumber,
156-
Country = _l_country,
157-
};
158-
return newType;
159-
}
160-
}
16147
}

bench/SerializeToBytes.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using BenchmarkDotNet.Attributes;
6+
using MessagePack;
7+
using Serde;
8+
9+
namespace Benchmarks
10+
{
11+
[GenericTypeArguments(typeof(Location))]
12+
public class SerializeToBytes<T>
13+
where T : Serde.ISerializeProvider<T>
14+
{
15+
private T value = default!;
16+
17+
private readonly ISerialize<T> _proxy = T.Instance;
18+
19+
[GlobalSetup]
20+
public void Setup()
21+
{
22+
value = DataGenerator.GenerateSerialize<T>();
23+
}
24+
25+
[Benchmark]
26+
public byte[] MessagePack()
27+
{
28+
return MessagePackSerializer.Serialize(value);
29+
}
30+
31+
[Benchmark]
32+
public byte[] SerdeMsgPack() => Serde.MsgPack.MsgPackSerializer.Serialize(value, _proxy);
33+
}
34+
}

src/MsgPackSerializer.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@ public static class MsgPackSerializer
77
{
88
public static byte[] Serialize<T>(T value)
99
where T : ISerializeProvider<T>
10-
{
11-
using var buffer = new ScratchBuffer();
12-
var writer = new MsgPackWriter(buffer);
13-
var serializeObject = T.SerializeInstance;
14-
serializeObject.Serialize(value, writer);
15-
return buffer.Span.ToArray();
16-
}
10+
=> Serialize(value, T.Instance);
1711

18-
public static byte[] Serialize<T, U>(T value, U proxy)
19-
where U : ISerialize<T>
12+
public static byte[] Serialize<T>(T value, ISerialize<T> proxy)
2013
{
21-
using var buffer = new ScratchBuffer();
14+
using var buffer = new ScratchBuffer(1024);
2215
var writer = new MsgPackWriter(buffer);
2316
proxy.Serialize(value, writer);
2417
return buffer.Span.ToArray();
@@ -31,4 +24,11 @@ public static T Deserialize<T, U>(byte[] bytes, U proxy)
3124
using var reader = new MsgPackReader<ArrayBufReader>(byteBuffer);
3225
return proxy.Deserialize(reader);
3326
}
27+
public static T Deserialize<T>(byte[] bytes)
28+
where T : IDeserializeProvider<T>
29+
{
30+
var byteBuffer = new ArrayBufReader(bytes);
31+
using var reader = new MsgPackReader<ArrayBufReader>(byteBuffer);
32+
return T.Instance.Deserialize(reader);
33+
}
3434
}

0 commit comments

Comments
 (0)