Class MemoryPackMessageSerializer<T>
- Namespace
- DotCompute.Abstractions.Messaging
- Assembly
- DotCompute.Abstractions.dll
High-performance MemoryPack serializer for IRingKernelMessage types.
public sealed class MemoryPackMessageSerializer<T> : IMessageSerializer<T> where T : IRingKernelMessage
Type Parameters
TThe message type implementing IRingKernelMessage.
- Inheritance
-
MemoryPackMessageSerializer<T>
- Implements
- Inherited Members
Remarks
MemoryPack provides 2-5x faster serialization compared to MessagePack with: - Source generator-based (zero reflection, Native AOT compatible) - Minimal allocations - Support for nullable types, Guid, DateTime - Optimized binary format
Requirements:
Message types must be decorated with [MemoryPackable] attribute:
[MemoryPackable]
public partial class VectorAddRequestMessage : IRingKernelMessage
{
public Guid MessageId { get; set; } = Guid.NewGuid();
public string MessageType => "VectorAddRequest";
public byte Priority { get; set; } = 128;
public Guid? CorrelationId { get; set; }
// Application data
public float A { get; set; }
public float B { get; set; }
// MemoryPack will auto-generate Serialize/Deserialize
}
Performance: - Serialization: ~20-50ns for small messages (vs ~100-200ns for JSON) - Minimal allocations (uses ArrayPool for temporary buffers) - Supports structs and classes
Properties
MaxSerializedSize
Gets the maximum serialized size for messages of type T.
public int MaxSerializedSize { get; }
Property Value
Remarks
This is used to allocate staging buffers. For variable-size messages, return a conservative upper bound. For fixed-size messages, return the exact size.
Methods
Deserialize(ReadOnlySpan<byte>)
Deserializes a message from the provided source span.
[UnconditionalSuppressMessage("Trimming", "IL2091", Justification = "T is constrained to IRingKernelMessage which must be [MemoryPackable] for MemoryPack to work. User types are source-generated.")]
public T Deserialize(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The source buffer containing serialized message data.
Returns
- T
The deserialized message.
Remarks
This method must be thread-safe. It may allocate managed memory for the message instance.
Exceptions
- ArgumentException
Thrown if
sourcecontains invalid or corrupted data.
GetSerializedSize(T)
Gets the serialized size for a specific message instance.
public int GetSerializedSize(T message)
Parameters
messageTThe message to measure.
Returns
- int
The number of bytes required to serialize this message.
Remarks
For fixed-size messages, this always returns MaxSerializedSize. For variable-size messages, this returns the exact size for this instance.
Serialize(T, Span<byte>)
Serializes a message into the provided destination span.
public int Serialize(T message, Span<byte> destination)
Parameters
messageTThe message to serialize.
destinationSpan<byte>The destination buffer (must be at least MaxSerializedSize bytes).
Returns
- int
The actual number of bytes written to
destination.
Remarks
This method must be thread-safe. It should not allocate managed memory.
Exceptions
- ArgumentException
Thrown if
destinationis too small.