Table of Contents

Interface IMessageSerializer<T>

Namespace
DotCompute.Abstractions.Messaging
Assembly
DotCompute.Abstractions.dll

Provides serialization and deserialization for IRingKernelMessage types.

public interface IMessageSerializer<T> where T : IRingKernelMessage

Type Parameters

T

The message type implementing IRingKernelMessage.

Remarks

Serializers must produce deterministic, platform-independent byte layouts suitable for GPU consumption. The serialized format is:

┌─────────────────┬──────────────────┬─────────────┐
│ Size (4 bytes)  │ MessageId (8B)   │ Payload...  │
└─────────────────┴──────────────────┴─────────────┘

Implementations must be thread-safe for concurrent serialization calls.

Properties

MaxSerializedSize

Gets the maximum serialized size for messages of type T.

int MaxSerializedSize { get; }

Property Value

int

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.

T Deserialize(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<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 source contains invalid or corrupted data.

GetSerializedSize(T)

Gets the serialized size for a specific message instance.

int GetSerializedSize(T message)

Parameters

message T

The 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.

int Serialize(T message, Span<byte> destination)

Parameters

message T

The message to serialize.

destination Span<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 destination is too small.