perf(net): borrow inbound packet storage

Decode headers, optional fields, fragments, and single-fragment messages directly over pooled datagrams. Copy only fragment state that crosses a datagram lifetime, preserve synchronous dispatch and ACK ordering, and lock the path to the owned decoder with differential and zero-allocation tests.
This commit is contained in:
Erik 2026-07-25 05:58:55 +02:00
parent 7211bb1bf7
commit e928c5dd02
18 changed files with 1329 additions and 66 deletions

View file

@ -42,16 +42,28 @@ public readonly record struct GameEventEnvelope(
/// </summary>
public static GameEventEnvelope? TryParse(byte[] body)
{
if (body is null || body.Length < HeaderSize) return null;
ArgumentNullException.ThrowIfNull(body);
return TryParseBorrowed(body);
}
uint outer = BinaryPrimitives.ReadUInt32LittleEndian(body);
/// <summary>
/// Internal receive-path parser. The returned payload borrows
/// <paramref name="body"/> and must not escape synchronous dispatch.
/// </summary>
internal static GameEventEnvelope? TryParseBorrowed(
ReadOnlyMemory<byte> body)
{
if (body.Length < HeaderSize) return null;
ReadOnlySpan<byte> span = body.Span;
uint outer = BinaryPrimitives.ReadUInt32LittleEndian(span);
if (outer != Opcode) return null;
uint guid = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(4));
uint sequence = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(8));
uint eventType = BinaryPrimitives.ReadUInt32LittleEndian(body.AsSpan(12));
uint guid = BinaryPrimitives.ReadUInt32LittleEndian(span.Slice(4));
uint sequence = BinaryPrimitives.ReadUInt32LittleEndian(span.Slice(8));
uint eventType = BinaryPrimitives.ReadUInt32LittleEndian(span.Slice(12));
var payload = body.AsMemory(HeaderSize);
ReadOnlyMemory<byte> payload = body.Slice(HeaderSize);
return new GameEventEnvelope(guid, sequence, (GameEventType)eventType, payload);
}
}