Skip to content

Commit

Permalink
Revert "Use array pool input message buffer (#41)"
Browse files Browse the repository at this point in the history
This reverts commit 7a95240.
  • Loading branch information
lucasteles committed May 8, 2024
1 parent aae32f7 commit 545c3fa
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 336 deletions.
135 changes: 0 additions & 135 deletions benchmarks/Backdash.Benchmarks/Cases/SessionBenchmark.cs

This file was deleted.

4 changes: 2 additions & 2 deletions benchmarks/Backdash.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
// BenchmarkRunner.Run<GetBitStringBenchmark>();
// BenchmarkRunner.Run<UdpClientBenchmark>();
// BenchmarkRunner.Run<UdpClientBenchmark>();
// await new UdpClientBenchmark().Start(10, false).ConfigureAwait(false);
2 changes: 1 addition & 1 deletion src/Backdash/Network/Client/PeerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async Task ReceiveLoop(CancellationToken ct)
{
var buffer = Mem.CreatePinnedMemory(maxPacketSize);
SocketAddress address = new(socket.AddressFamily);
T msg = default;
while (!ct.IsCancellationRequested)
{
int receivedSize;
Expand Down Expand Up @@ -113,7 +114,6 @@ async Task ReceiveLoop(CancellationToken ct)

try
{
T msg = new();
serializer.Deserialize(buffer[..receivedSize].Span, ref msg);
await observer.OnPeerMessage(msg, address, receivedSize, ct).ConfigureAwait(false);
}
Expand Down
64 changes: 13 additions & 51 deletions src/Backdash/Network/Messages/InputMessage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Backdash.Core;
using Backdash.Data;
Expand All @@ -10,37 +8,19 @@
namespace Backdash.Network.Messages;

[Serializable]
struct InputMessage : IDisposable, IBinarySerializable, IUtf8SpanFormattable, IEquatable<InputMessage>
record struct InputMessage : IBinarySerializable, IUtf8SpanFormattable
{
public PeerStatusBuffer PeerConnectStatus;
public Frame StartFrame;
public bool DisconnectRequested;
public Frame AckFrame;
public ushort NumBits;
public byte InputSize;
public Memory<byte> Bits = Memory<byte>.Empty;

public InputMessage() => InitBits();

public InputMessage(ReadOnlySpan<byte> bits) : this()
{
InitBits();
bits.CopyTo(Bits.Span);
}

public void InitBits()
{
var buffer = ArrayPool<byte>.Shared.Rent(Max.CompressedBytes);
buffer.AsSpan().Clear();
Bits = buffer;
}

public readonly int InputByteSize() => (int)Math.Ceiling(NumBits / (float)ByteSize.ByteToBits);
public readonly Memory<byte> InputBytes() => Bits[..InputByteSize()];
public InputMessageBuffer Bits;

public void Clear()
{
Mem.Clear(Bits.Span);
Mem.Clear(Bits);
PeerConnectStatus[..].Clear();
StartFrame = Frame.Zero;
DisconnectRequested = false;
Expand All @@ -62,7 +42,7 @@ public readonly void Serialize(BinarySpanWriter writer)
writer.Write(in InputSize);
writer.Write(in NumBits);
var bitCount = (int)Math.Ceiling(NumBits / (float)ByteSize.ByteToBits);
writer.Write(Bits.Span[..bitCount]);
writer.Write(Bits[..bitCount]);
}

public void Deserialize(BinarySpanReader reader)
Expand All @@ -76,11 +56,7 @@ public void Deserialize(BinarySpanReader reader)
InputSize = reader.ReadByte();
NumBits = reader.ReadUShort();
var bitCount = (int)Math.Ceiling(NumBits / (float)ByteSize.ByteToBits);

if (Bits.Length is 0)
InitBits();

reader.ReadByte(Bits.Span[..bitCount]);
reader.ReadByte(Bits[..bitCount]);
}

public readonly bool TryFormat(
Expand All @@ -94,28 +70,6 @@ public readonly bool TryFormat(
if (!writer.Write(NumBits)) return false;
return true;
}

public readonly bool Equals(InputMessage other) =>
PeerConnectStatus[..].SequenceEqual(other.PeerConnectStatus) &&
StartFrame.Equals(other.StartFrame) &&
DisconnectRequested == other.DisconnectRequested &&
AckFrame.Equals(other.AckFrame) && NumBits == other.NumBits &&
InputSize == other.InputSize &&
Mem.EqualBytes(InputBytes().Span, other.InputBytes().Span, truncate: true);

public override readonly bool Equals(object? obj) => obj is InputMessage other && Equals(other);

public override readonly int GetHashCode() => HashCode.Combine(
PeerConnectStatus, StartFrame, DisconnectRequested, AckFrame, NumBits, InputSize, Bits);

public readonly void Dispose()
{
if (Bits.Length > 0 && MemoryMarshal.ToEnumerable<byte>(Bits) is byte[] array)
ArrayPool<byte>.Shared.Return(array);
}

public static bool operator ==(InputMessage left, InputMessage right) => left.Equals(right);
public static bool operator !=(InputMessage left, InputMessage right) => !left.Equals(right);
}

[Serializable, InlineArray(Max.NumberOfPlayers)]
Expand Down Expand Up @@ -149,3 +103,11 @@ public override readonly string ToString()
return builder.ToString();
}
}

[Serializable, InlineArray(Max.CompressedBytes)]
struct InputMessageBuffer
{
byte element0;
public InputMessageBuffer(ReadOnlySpan<byte> bits) => bits.CopyTo(this);
public override readonly string ToString() => Mem.GetBitString(this);
}
11 changes: 2 additions & 9 deletions src/Backdash/Network/Messages/ProtocolMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
namespace Backdash.Network.Messages;

[StructLayout(LayoutKind.Explicit, Pack = 2)]
struct ProtocolMessage(MessageType type)
: IDisposable, IBinarySerializable, IEquatable<ProtocolMessage>, IUtf8SpanFormattable
struct ProtocolMessage(MessageType type) : IBinarySerializable, IEquatable<ProtocolMessage>, IUtf8SpanFormattable
{
[FieldOffset(0)]
public Header Header = new(type);
Expand All @@ -30,7 +29,7 @@ struct ProtocolMessage(MessageType type)
[FieldOffset(Header.Size)]
public KeepAlive KeepAlive;

[FieldOffset(Header.Size + 2)]
[FieldOffset(Header.Size)]
public InputMessage Input;

public readonly void Serialize(BinarySpanWriter writer)
Expand Down Expand Up @@ -115,12 +114,6 @@ public override readonly string ToString()
return $"Msg({Header.Type}){info}";
}

public readonly void Dispose()
{
if (Header.Type is MessageType.Input)
Input.Dispose();
}

public readonly bool TryFormat(
Span<byte> utf8Destination, out int bytesWritten,
ReadOnlySpan<char> format, IFormatProvider? provider
Expand Down
8 changes: 4 additions & 4 deletions src/Backdash/Network/Protocol/Comm/InputEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Backdash.Network.Protocol.Comm;

static class InputEncoder
{
public static DeltaXorRle.Encoder GetCompressor(in InputMessage inputMsg, Span<byte> lastBuffer) =>
new(inputMsg.Bits.Span, lastBuffer);
public static DeltaXorRle.Encoder GetCompressor(ref InputMessage inputMsg, Span<byte> lastBuffer) =>
new(inputMsg.Bits, lastBuffer);

public static DeltaXorRle.Decoder GetDecompressor(in InputMessage inputMsg) =>
new(inputMsg.Bits.Span, inputMsg.NumBits);
public static DeltaXorRle.Decoder GetDecompressor(ref InputMessage inputMsg) =>
new(inputMsg.Bits, inputMsg.NumBits);
}
Loading

0 comments on commit 545c3fa

Please sign in to comment.