Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MemoryPack #22

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private void CreateSerializersToTest()
#endif

#if (NETCOREAPP3_0_OR_GREATER)
new MemoryPack<BookShelf>(Data, TouchAndVerify),
new SerializerTests.Serializers.BinaryPack<BookShelf>(Data, TouchAndVerify),
#endif

Expand Down Expand Up @@ -224,6 +225,11 @@ private void CreateSerializersToTest()
#endif

#if (NETCOREAPP3_0_OR_GREATER)
new MemoryPack<BookShelf>(Data, null),
new MemoryPack<BookShelf1>(Data1, null),
new MemoryPack<BookShelf2>(Data2, null),
new MemoryPack<LargeBookShelf>(DataLarge, null),

new SerializerTests.Serializers.BinaryPack<BookShelf>(Data, null),
new SerializerTests.Serializers.BinaryPack<BookShelf1>(Data1, null),
new SerializerTests.Serializers.BinaryPack<BookShelf2>(Data2, null),
Expand Down
1 change: 1 addition & 0 deletions SerializerTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<PackageReference Include="Hyperion" Version="0.11.2" />
<PackageReference Include="Jil" Version="2.17.0" />
<PackageReference Include="KdSoft.FlatBuffers" Version="1.12.0" />
<PackageReference Include="MemoryPack" Version="1.4.4" />
<PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="MsgPack.Cli" Version="1.0.1" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
Expand Down
49 changes: 49 additions & 0 deletions Serializers/MemoryPack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#if !NET48
using MessagePack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using MemoryPack;

namespace SerializerTests.Serializers
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
[SerializerType("https://github.com/Cysharp/MemoryPack", SerializerTypes.Binary | SerializerTypes.SupportsVersioning)]
public class MemoryPack<T> : TestBase<T, string> where T : class
{
public MemoryPack(Func<int, T> testData, Action<T,int,int> touchAndVerify) : base(testData, touchAndVerify)
{
}

[MethodImpl(MethodImplOptions.NoInlining)]
protected override void Serialize(T obj, Stream stream)
{
var valueTask = MemoryPackSerializer.SerializeAsync(stream, obj);
valueTask.GetAwaiter().GetResult();
if (!valueTask.IsCompletedSuccessfully)
{
throw new Exception("MemoryPack.Serialize should complete synchronously!");
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
protected override T Deserialize(Stream stream)
{
var valueTask = MemoryPackSerializer.DeserializeAsync<T>(stream);
valueTask.GetAwaiter().GetResult();
if (!valueTask.IsCompletedSuccessfully)
{
throw new Exception("MemoryPack.Deserialize should complete synchronously!");
}
return valueTask.Result;
}
}
}
#endif
19 changes: 11 additions & 8 deletions TypesToSerialize/BookShelf.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if NETCOREAPP3_1 || NETCOREAPP3_0 || NET5_0
#if NETCOREAPP3_0_OR_GREATER
using BinaryPack.Attributes;
using BinaryPack.Enums;
using MemoryPack;
#endif
using MessagePack;
using ProtoBuf;
Expand All @@ -14,11 +15,11 @@
namespace SerializerTests.TypesToSerialize
{
[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_1 || NETCOREAPP3_0 || NET5_0
, BinarySerialization(SerializationMode.Properties | SerializationMode.NonPublicMembers)
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable, BinarySerialization(SerializationMode.Properties | SerializationMode.NonPublicMembers)
#endif
]
public class BookShelf
public partial class BookShelf
{
[DataMember, ProtoMember(1), Key(0)]
public List<Book> Books
Expand All @@ -35,18 +36,20 @@ public BookShelf(string secret)
{
Secret = secret;
}

#if NETCOREAPP3_0_OR_GREATER
[MemoryPackConstructor]
Copy link
Contributor Author

@lbargaoanu lbargaoanu Nov 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A polyfill would also work. Or dropping .Net Framework :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. I will check and also update to .NET 7.0. Looks like you did again create the fastest serializer. But I will measure first ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish I did :) No, I'm not the author, I've just recently found out about the new project and I thought I'd add it.

#endif
public BookShelf() // Parameterless ctor is needed for every protocol buffer class during deserialization
{
}
}

[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_1 || NETCOREAPP3_0 || NET5_0
, BinarySerialization(SerializationMode.AllMembers)
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable, BinarySerialization(SerializationMode.AllMembers)
#endif
]
public class Book
public partial class Book
{
[DataMember, ProtoMember(1), Key(0)]
public string Title;
Expand Down
25 changes: 19 additions & 6 deletions TypesToSerialize/BookShelf1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MessagePack;
#if NETCOREAPP3_0_OR_GREATER
using MemoryPack;
#endif
using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
Expand All @@ -9,8 +12,12 @@

namespace SerializerTests.TypesToSerialize
{
[Serializable, DataContract, ProtoContract, MessagePackObject]
public class BookShelf1
[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class BookShelf1
{
[DataMember, ProtoMember(1), Key(0)]
public List<Book1> Books
Expand All @@ -26,13 +33,19 @@ public BookShelf1(string secret)
{
Secret = secret;
}

#if NETCOREAPP3_0_OR_GREATER
[MemoryPackConstructor]
#endif
public BookShelf1() // Parameterless ctor is needed for every protocol buffer class during deserialization
{ }
}

[Serializable, DataContract, ProtoContract, MessagePackObject]
public class Book1
[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class Book1
{
[DataMember, ProtoMember(1), Key(0)]
public string Title;
Expand Down
25 changes: 19 additions & 6 deletions TypesToSerialize/BookShelf2.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MessagePack;
#if NETCOREAPP3_0_OR_GREATER
using MemoryPack;
#endif
using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
Expand All @@ -9,8 +12,12 @@

namespace SerializerTests.TypesToSerialize
{
[Serializable, DataContract, ProtoContract, MessagePackObject]
public class BookShelf2
[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class BookShelf2
{
[DataMember, ProtoMember(1), Key(0)]
public List<Book2> Books
Expand All @@ -26,13 +33,19 @@ public BookShelf2(string secret)
{
Secret = secret;
}

#if NETCOREAPP3_0_OR_GREATER
[MemoryPackConstructor]
#endif
public BookShelf2() // Parameterless ctor is needed for every protocol buffer class during deserialization
{ }
}

[Serializable, DataContract, ProtoContract, MessagePackObject]
public class Book2
[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class Book2
{
[DataMember, ProtoMember(1), Key(0)]
public string Title;
Expand Down
27 changes: 20 additions & 7 deletions TypesToSerialize/LargeBookShelf.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MessagePack;
#if NETCOREAPP3_0_OR_GREATER
using MemoryPack;
#endif
using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
Expand All @@ -9,8 +12,12 @@

namespace SerializerTests.TypesToSerialize
{
[Serializable, DataContract, ProtoContract, MessagePackObject]
public class LargeBookShelf
[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class LargeBookShelf
{
[DataMember, ProtoMember(1), Key(0)]
public List<LargeBook> Books
Expand Down Expand Up @@ -80,15 +87,21 @@ public LargeBookShelf(string secret)
{
Secret = secret;
}

#if NETCOREAPP3_0_OR_GREATER
[MemoryPackConstructor]
#endif
public LargeBookShelf() // Parameterless ctor is needed for every protocol buffer class during deserialization
{ }


}

[Serializable, DataContract, ProtoContract, MessagePackObject]
public class LargeBook
[Serializable, DataContract, ProtoContract, MessagePackObject
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class LargeBook
{
[DataMember, ProtoMember(1), Key(0)]
public string Title;
Expand Down
21 changes: 16 additions & 5 deletions TypesToSerialize/ReferenceBookShelf.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MessagePack;
#if NETCOREAPP3_0_OR_GREATER
using MemoryPack;
#endif
using MessagePack;
using ProtoBuf;
using System;
using System.Collections.Generic;
Expand All @@ -8,16 +11,24 @@

namespace SerializerTests.TypesToSerialize
{
[Serializable, MessagePackObject, ZeroFormattable, DataContract]
public class ReferenceBookShelf
[Serializable, MessagePackObject, ZeroFormattable, DataContract
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class ReferenceBookShelf
{
[DataMember(Order = 0), ProtoMember(1), Key(0), Index(0)]
public virtual Dictionary<DateTime, ReferenceBook> Books
{ get; set; } = new Dictionary<DateTime, ReferenceBook>();
}

[Serializable, MessagePackObject, ZeroFormattable, DataContract]
public class ReferenceBook
[Serializable, MessagePackObject, ZeroFormattable, DataContract
#if NETCOREAPP3_0_OR_GREATER
, MemoryPackable
#endif
]
public partial class ReferenceBook
{
[DataMember(Order = 0), Key(0), Index(0)]
public virtual ReferenceBookShelf Container { get; set; } // Create object cycle if necessary
Expand Down