-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add MemoryPack Serializer #9838
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
241 changes: 241 additions & 0 deletions
241
src/Orleans.Serialization.MemoryPack/MemoryPackCodec.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Reflection; | ||
| using MemoryPack; | ||
| using Microsoft.Extensions.Options; | ||
| using Orleans.Serialization.Buffers; | ||
| using Orleans.Serialization.Buffers.Adaptors; | ||
| using Orleans.Serialization.Cloning; | ||
| using Orleans.Serialization.Codecs; | ||
| using Orleans.Serialization.Serializers; | ||
| using Orleans.Serialization.WireProtocol; | ||
|
|
||
| namespace Orleans.Serialization; | ||
|
|
||
| /// <summary> | ||
| /// A serialization codec which uses <see cref="MemoryPackSerializer"/>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// MemoryPack codec performs slightly worse than default Orleans serializer, if performance is critical for your application, consider using default serialization. | ||
| /// </remarks> | ||
| [Alias(WellKnownAlias)] | ||
| public class MemoryPackCodec : IGeneralizedCodec, IGeneralizedCopier, ITypeFilter | ||
| { | ||
| private static readonly ConcurrentDictionary<Type, bool> SupportedTypes = new(); | ||
| private static readonly Type SelfType = typeof(MemoryPackCodec); | ||
|
|
||
| private readonly ICodecSelector[] _serializableTypeSelectors; | ||
| private readonly ICopierSelector[] _copyableTypeSelectors; | ||
| private readonly MemoryPackCodecOptions _options; | ||
|
|
||
| /// <summary> | ||
| /// The well-known type alias for this codec. | ||
| /// </summary> | ||
| public const string WellKnownAlias = "memorypack"; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MemoryPackCodec"/> class. | ||
| /// </summary> | ||
| /// /// <param name="serializableTypeSelectors">Filters used to indicate which types should be serialized by this codec.</param> | ||
| /// <param name="copyableTypeSelectors">Filters used to indicate which types should be copied by this codec.</param> | ||
| /// <param name="options">The MemoryPack codec options.</param> | ||
| public MemoryPackCodec( | ||
| IEnumerable<ICodecSelector> serializableTypeSelectors, | ||
| IEnumerable<ICopierSelector> copyableTypeSelectors, | ||
| IOptions<MemoryPackCodecOptions> options) | ||
| { | ||
| _serializableTypeSelectors = serializableTypeSelectors.Where(t => string.Equals(t.CodecName, WellKnownAlias, StringComparison.Ordinal)).ToArray(); | ||
| _copyableTypeSelectors = copyableTypeSelectors.Where(t => string.Equals(t.CopierName, WellKnownAlias, StringComparison.Ordinal)).ToArray(); | ||
| _options = options.Value; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| void IFieldCodec.WriteField<TBufferWriter>(ref Writer<TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value) | ||
| { | ||
| if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // The schema type when serializing the field is the type of the codec. | ||
| writer.WriteFieldHeader(fieldIdDelta, expectedType, SelfType, WireType.TagDelimited); | ||
|
|
||
| // Write the type name | ||
| ReferenceCodec.MarkValueField(writer.Session); | ||
| writer.WriteFieldHeaderExpected(0, WireType.LengthPrefixed); | ||
| writer.Session.TypeCodec.WriteLengthPrefixed(ref writer, value.GetType()); | ||
|
|
||
| var bufferWriter = new BufferWriterBox<PooledBuffer>(new()); | ||
| try | ||
| { | ||
| MemoryPackSerializer.Serialize(value.GetType(), bufferWriter, value, _options.SerializerOptions); | ||
|
|
||
| ReferenceCodec.MarkValueField(writer.Session); | ||
| writer.WriteFieldHeaderExpected(1, WireType.LengthPrefixed); | ||
| writer.WriteVarUInt32((uint)bufferWriter.Value.Length); | ||
| bufferWriter.Value.CopyTo(ref writer); | ||
| } | ||
| finally | ||
| { | ||
| bufferWriter.Value.Dispose(); | ||
| } | ||
|
|
||
| writer.WriteEndObject(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| object IFieldCodec.ReadValue<TInput>(ref Reader<TInput> reader, Field field) | ||
| { | ||
| if (field.IsReference) | ||
| { | ||
| return ReferenceCodec.ReadReference(ref reader, field.FieldType); | ||
| } | ||
|
|
||
| field.EnsureWireTypeTagDelimited(); | ||
|
|
||
| var placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session); | ||
| object result = null; | ||
| Type type = null; | ||
| uint fieldId = 0; | ||
| while (true) | ||
| { | ||
| var header = reader.ReadFieldHeader(); | ||
| if (header.IsEndBaseOrEndObject) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| fieldId += header.FieldIdDelta; | ||
| switch (fieldId) | ||
| { | ||
| case 0: | ||
| ReferenceCodec.MarkValueField(reader.Session); | ||
| type = reader.Session.TypeCodec.ReadLengthPrefixed(ref reader); | ||
| break; | ||
| case 1: | ||
| if (type is null) | ||
| { | ||
| ThrowTypeFieldMissing(); | ||
| } | ||
|
|
||
| ReferenceCodec.MarkValueField(reader.Session); | ||
| var length = reader.ReadVarUInt32(); | ||
|
|
||
| var bufferWriter = new BufferWriterBox<PooledBuffer>(new()); | ||
| try | ||
| { | ||
| reader.ReadBytes(ref bufferWriter, (int)length); | ||
| result = MemoryPackSerializer.Deserialize(type, bufferWriter.Value.AsReadOnlySequence(), _options.SerializerOptions); | ||
| } | ||
| finally | ||
| { | ||
| bufferWriter.Value.Dispose(); | ||
| } | ||
|
|
||
| break; | ||
| default: | ||
| reader.ConsumeUnknownField(header); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId); | ||
| return result; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| bool IGeneralizedCodec.IsSupportedType(Type type) | ||
| { | ||
| if (type == SelfType) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| foreach (var selector in _serializableTypeSelectors) | ||
| { | ||
| if (selector.IsSupportedType(type)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| if (_options.IsSerializableType?.Invoke(type) is bool value) | ||
| { | ||
| return value; | ||
| } | ||
|
|
||
| return IsMemoryPackContract(type); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| object IDeepCopier.DeepCopy(object input, CopyContext context) | ||
| { | ||
| if (context.TryGetCopy(input, out object result)) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| var bufferWriter = new BufferWriterBox<PooledBuffer>(new()); | ||
| try | ||
| { | ||
| MemoryPackSerializer.Serialize(input.GetType(), bufferWriter, input, _options.SerializerOptions); | ||
|
|
||
| var sequence = bufferWriter.Value.AsReadOnlySequence(); | ||
| result = MemoryPackSerializer.Deserialize(input.GetType(), sequence, _options.SerializerOptions); | ||
| } | ||
| finally | ||
| { | ||
| bufferWriter.Value.Dispose(); | ||
| } | ||
|
|
||
| context.RecordCopy(input, result); | ||
| return result; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| bool IGeneralizedCopier.IsSupportedType(Type type) | ||
| { | ||
| if (CommonCodecTypeFilter.IsAbstractOrFrameworkType(type)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| foreach (var selector in _copyableTypeSelectors) | ||
| { | ||
| if (selector.IsSupportedType(type)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| if (_options.IsCopyableType?.Invoke(type) is bool value) | ||
| { | ||
| return value; | ||
| } | ||
|
|
||
| return IsMemoryPackContract(type); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| bool? ITypeFilter.IsTypeAllowed(Type type) => (((IGeneralizedCopier)this).IsSupportedType(type) || ((IGeneralizedCodec)this).IsSupportedType(type)) ? true : null; | ||
|
|
||
| private static bool IsMemoryPackContract(Type type) | ||
| { | ||
| if (SupportedTypes.TryGetValue(type, out bool isMemoryPackContract)) | ||
| { | ||
| return isMemoryPackContract; | ||
| } | ||
|
|
||
| isMemoryPackContract = type.GetCustomAttribute<MemoryPackableAttribute>() is not null; | ||
|
|
||
| SupportedTypes.TryAdd(type, isMemoryPackContract); | ||
| return isMemoryPackContract; | ||
| } | ||
|
|
||
| private static void ThrowTypeFieldMissing() => throw new RequiredFieldMissingException("Serialized value is missing its type field."); | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
src/Orleans.Serialization.MemoryPack/MemoryPackCodecOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System; | ||
| using MemoryPack; | ||
|
|
||
| namespace Orleans.Serialization; | ||
|
|
||
| /// <summary> | ||
| /// Options for <see cref="MemoryPackCodec"/>. | ||
| /// </summary> | ||
| public class MemoryPackCodecOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the <see cref="MemoryPackSerializerOptions"/>. | ||
| /// </summary> | ||
| public MemoryPackSerializerOptions SerializerOptions { get; set; } = MemoryPackSerializerOptions.Default; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a delegate used to determine if a type is supported by the MemoryPack serializer for serialization and deserialization. | ||
| /// </summary> | ||
| public Func<Type, bool?> IsSerializableType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a delegate used to determine if a type is supported by the MemoryPack serializer for copying. | ||
| /// </summary> | ||
| public Func<Type, bool?> IsCopyableType { get; set; } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/Orleans.Serialization.MemoryPack/Orleans.Serialization.MemoryPack.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <PackageReadmeFile>README.md</PackageReadmeFile> | ||
| <PackageId>Microsoft.Orleans.Serialization.MemoryPack</PackageId> | ||
| <TargetFrameworks>$(DefaultTargetFrameworks);netstandard2.1</TargetFrameworks> | ||
| <PackageDescription>MemoryPack integration for Orleans.Serialization</PackageDescription> | ||
| <OrleansBuildTimeCodeGen>true</OrleansBuildTimeCodeGen> | ||
| <IsOrleansFrameworkPart>false</IsOrleansFrameworkPart> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="MemoryPack" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Orleans.Serialization\Orleans.Serialization.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="README.md" Pack="true" PackagePath="\" /> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Microsoft Orleans Serialization for MemoryPack | ||
|
|
||
| ## Introduction | ||
| Microsoft Orleans Serialization for MemoryPack provides MemoryPack serialization support for Microsoft Orleans using the MemoryPack format. This high-performance binary serialization format is ideal for scenarios requiring efficient serialization and deserialization. | ||
|
|
||
| ## Getting Started | ||
| To use this package, install it via NuGet: | ||
|
|
||
| ```shell | ||
| dotnet add package Microsoft.Orleans.Serialization.MemoryPack | ||
| ``` | ||
|
|
||
| ## Example - Configuring MemoryPack Serialization | ||
| ```csharp | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Orleans.Hosting; | ||
| using Orleans.Serialization; | ||
|
|
||
| var builder = Host.CreateApplicationBuilder(args) | ||
| .UseOrleans(siloBuilder => | ||
| { | ||
| siloBuilder | ||
| .UseLocalhostClustering() | ||
| // Configure MemoryPack as a serializer | ||
| .AddSerializer(serializerBuilder => serializerBuilder.AddMemoryPackSerializer()); | ||
| }); | ||
|
|
||
| // Run the host | ||
| await builder.RunAsync(); | ||
| ``` | ||
|
|
||
| ## Example - Using MemoryPack with a Custom Type | ||
| ```csharp | ||
| using Orleans; | ||
| using Orleans.Serialization.Cloning; | ||
| using Orleans.Serialization.Codecs; | ||
| using Orleans.Serialization.Configuration; | ||
| using Orleans.Serialization.Serializers; | ||
| using MemoryPack; | ||
| namespace ExampleGrains; | ||
|
|
||
| // Define a class with MemoryPack attributes | ||
| [MemoryPackable] | ||
| public partial class MyMemoryPackClass | ||
| { | ||
| public string Name { get; set; } | ||
| public int Age { get; set; } | ||
| public List<string> Tags { get; set; } | ||
| } | ||
|
|
||
| // You can use it directly in your grain interfaces and implementation | ||
| public interface IMyGrain : IGrainWithStringKey | ||
| { | ||
| Task<MyMemoryPackClass> GetData(); | ||
| Task SetData(MyMemoryPackClass data); | ||
| } | ||
|
|
||
| public class MyGrain : Grain, IMyGrain | ||
| { | ||
| private MyMemoryPackClass _data; | ||
|
|
||
| public Task<MyMemoryPackClass> GetData() | ||
| { | ||
| return Task.FromResult(_data); | ||
| } | ||
|
|
||
| public Task SetData(MyMemoryPackClass data) | ||
| { | ||
| _data = data; | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Documentation | ||
| For more comprehensive documentation, please refer to: | ||
| - [Microsoft Orleans Documentation](https://learn.microsoft.com/dotnet/orleans/) | ||
| - [Orleans Serialization](https://learn.microsoft.com/en-us/dotnet/orleans/host/configuration-guide/serialization) | ||
| - [MemoryPack for C#](https://github.com/Cysharp/MemoryPack) | ||
|
|
||
| ## Feedback & Contributing | ||
| - If you have any issues or would like to provide feedback, please [open an issue on GitHub](https://github.com/dotnet/orleans/issues) | ||
| - Join our community on [Discord](https://aka.ms/orleans-discord) | ||
| - Follow the [@msftorleans](https://twitter.com/msftorleans) Twitter account for Orleans announcements | ||
| - Contributions are welcome! Please review our [contribution guidelines](https://github.com/dotnet/orleans/blob/main/CONTRIBUTING.md) | ||
| - This project is licensed under the [MIT license](https://github.com/dotnet/orleans/blob/main/LICENSE) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MemoryPack serializer options configured via AddMemoryPackSerializer()/MemoryPackCodecOptions are currently ignored during field serialization because the Serialize call uses the default options overload. Pass _options.SerializerOptions to MemoryPackSerializer.Serialize so that user configuration is honored (consistent with DeepCopy and MessagePackCodec).
This issue also appears on line 124 of the same file.