Skip to content
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<PackageVersion Include="Grpc.Tools" Version="2.72.0" />
<PackageVersion Include="Hyperion" Version="0.12.2" />
<PackageVersion Include="KubernetesClient" Version="18.0.5" />
<PackageVersion Include="MemoryPack" Version="1.21.4" />
<PackageVersion Include="MessagePack" Version="3.1.4" />
<PackageVersion Include="Microsoft.AspNetCore.Connections.Abstractions" Version="8.0.11" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.54.1" />
Expand Down
1 change: 1 addition & 0 deletions Orleans.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<Project Path="src/Orleans.Sdk/Orleans.Sdk.csproj" />
<Project Path="src/Orleans.Serialization.Abstractions/Orleans.Serialization.Abstractions.csproj" />
<Project Path="src/Orleans.Serialization.FSharp/Orleans.Serialization.FSharp.csproj" />
<Project Path="src/Orleans.Serialization.MemoryPack/Orleans.Serialization.MemoryPack.csproj" />
<Project Path="src/Orleans.Serialization.MessagePack/Orleans.Serialization.MessagePack.csproj" />
<Project Path="src/Orleans.Serialization.NewtonsoftJson/Orleans.Serialization.NewtonsoftJson.csproj" />
<Project Path="src/Orleans.Serialization.SystemTextJson/Orleans.Serialization.SystemTextJson.csproj" />
Expand Down
241 changes: 241 additions & 0 deletions src/Orleans.Serialization.MemoryPack/MemoryPackCodec.cs
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);
Comment on lines +68 to +76

Copilot AI Feb 15, 2026

Copy link

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.

Copilot uses AI. Check for mistakes.
}
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 src/Orleans.Serialization.MemoryPack/MemoryPackCodecOptions.cs
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; }
}
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>
87 changes: 87 additions & 0 deletions src/Orleans.Serialization.MemoryPack/README.md
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)
Loading
Loading