Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
chawolbaka committed Feb 23, 2024
1 parent 339ca6e commit 6dc5dd1
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 126 deletions.
34 changes: 4 additions & 30 deletions Protocol.Core/Compression/VarInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

namespace MinecraftProtocol.Compression
{
Expand All @@ -16,10 +17,7 @@ public static class VarInt

public static int Convert(ReadOnlySpan<byte> bytes) => Read(bytes,out _);
public static int Convert(ReadOnlySpan<byte> bytes, out int length) => Read(bytes, out length);
public static int Convert(byte[] bytes) => Read(bytes as IList<byte>, 0, out _);
public static int Convert(byte[] bytes, int offset) => Read(bytes as IList<byte>, offset, out _);
public static int Convert(byte[] bytes, int offset, out int end) => Read(bytes as IList<byte>, offset, out end);
public static int Convert(IList<byte> bytes) => Read(bytes, 0, out _);

public static int Convert(IList<byte> bytes, int offset) => Read(bytes, offset, out _);
public static int Convert(IList<byte> bytes, int offset, out int end) => Read(bytes, offset, out end);
public static byte[] Convert(int value) => GetBytes(value);
Expand Down Expand Up @@ -49,12 +47,9 @@ public static int Read(Func<byte> readByte, out int readCount)
throw new OverflowException("VarInt too big");
}

//不这样子会导致和ReadOnlySpan的重载冲突
public static int Read(byte[] bytes) => Read(bytes as IList<byte>, 0, out _);
public static int Read(byte[] bytes, int offset) => Read(bytes as IList<byte>, offset, out _);
public static int Read(byte[] bytes, int offset, out int length) => Read(bytes as IList<byte>, offset, out length);

public static int Read(IList<byte> bytes) => Read(bytes, 0, out _);
public static int Read(IList<byte> bytes, int offset) => Read(bytes, offset, out _);
public static int Read(IList<byte> bytes, int offset, out int length)
{
Expand Down Expand Up @@ -107,19 +102,8 @@ public static byte[] GetBytes(int value)
{
return GetSpan(value).ToArray();
}


public static int WriteTo(int value, byte[] dest)
{
int offset = 0;
while ((value & -128) != 0)
{
dest[offset++] = ((byte)(value & 127 | 128));
value = (int)(((uint)value) >> 7);
}
dest[offset++] = (byte)value;
return offset;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteTo(int value, Span<byte> dest)
{
int offset = 0;
Expand All @@ -131,18 +115,8 @@ public static int WriteTo(int value, Span<byte> dest)
dest[offset++] = (byte)value;
return offset;
}
public static int WriteTo(int value, IList<byte> dest)
{
int offset = 0;
while ((value & -128) != 0)
{
dest[offset++] = ((byte)(value & 127 | 128));
value = (int)(((uint)value) >> 7);
}
dest[offset++] = (byte)value;
return offset;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetLength(int value)
{
uint temp = (uint)value;
Expand Down
47 changes: 7 additions & 40 deletions Protocol.Core/Compression/VarLong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

namespace MinecraftProtocol.Compression
{
Expand All @@ -14,16 +15,11 @@ public static class VarLong

public static long Convert(ReadOnlySpan<byte> bytes) => Read(bytes, out _);
public static long Convert(ReadOnlySpan<byte> bytes, out int length) => Read(bytes, out length);
public static long Convert(byte[] bytes) => Read(bytes as IList<byte>, 0, out _);
public static long Convert(byte[] bytes, int offset) => Read(bytes as IList<byte>, offset, out _);
public static long Convert(byte[] bytes, int offset, out int end) => Read(bytes as IList<byte>, offset, out end);
public static long Convert(IList<byte> bytes) => Read(bytes, 0, out _);

public static long Convert(IList<byte> bytes, int offset) => Read(bytes, offset, out _);
public static long Convert(IList<byte> bytes, int offset, out int end) => Read(bytes, offset, out end);
public static byte[] Convert(long value) => GetBytes(value);



public static long Read(Stream stream) => Read(() => (byte)stream.ReadByte(), out _);
public static long Read(Stream stream, out int readCount) => Read(() => { int read = stream.ReadByte(); return read >= 0 ? (byte)read : throw new InvalidDataException("negative"); }, out readCount);
public static long Read(Socket socket) => Read(socket, out _);
Expand All @@ -48,10 +44,6 @@ public static long Read(Func<byte> readByte, out int readCount)
throw new OverflowException("VarLong too big");
}

public static long Read(byte[] bytes) => Read(bytes as IList<byte>, 0, out _);
public static long Read(byte[] bytes, int offset) => Read(bytes as IList<byte>, offset, out _);
public static long Read(byte[] bytes, int offset, out int length) => Read(bytes as IList<byte>, offset, out length);
public static long Read(IList<byte> bytes) => Read(bytes, 0, out _);
public static long Read(IList<byte> bytes, int offset) => Read(bytes, offset, out _);
public static long Read(IList<byte> bytes, int offset, out int length)
{
Expand All @@ -67,6 +59,7 @@ public static long Read(IList<byte> bytes, int offset, out int length)
}
throw new OverflowException("VarLong too big");
}

public static long Read(ReadOnlySpan<byte> bytes) => Read(bytes, out _);
public static long Read(ReadOnlySpan<byte> bytes, out int length)
{
Expand All @@ -82,6 +75,7 @@ public static long Read(ReadOnlySpan<byte> bytes, out int length)
}
throw new OverflowException("VarLong too big");
}

public static Span<byte> GetSpan(long value)
{
int offset = 0;
Expand All @@ -99,26 +93,13 @@ public static Span<byte> GetSpan(long value)
} while (Value!=0);
return bytes.AsSpan().Slice(0, offset);
}

public static byte[] GetBytes(long value)
{
return GetSpan(value).ToArray();
}


public static int WriteTo(long value, byte[] dest)
{
ulong Value = (ulong)value;
int offset = 0;
do
{
byte temp = (byte)(Value & MaskValue);
Value >>= 7;
if (Value != 0) temp |= MaskByteSigned;
dest[offset++] = temp;

} while (Value != 0);
return offset;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteTo(long value, Span<byte> dest)
{
ulong Value = (ulong)value;
Expand All @@ -133,22 +114,8 @@ public static int WriteTo(long value, Span<byte> dest)
} while (Value != 0);
return offset;
}
public static int WriteTo(long value, List<byte> dest)
{
ulong Value = (ulong)value;
int offset = 0;
do
{
byte temp = (byte)(Value & MaskValue);
Value >>= 7;
if (Value != 0) temp |= MaskByteSigned;
dest[offset++] = temp;

} while (Value != 0);
return offset;
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetLength(long value)
{
ulong temp = (ulong)value;
Expand Down
27 changes: 4 additions & 23 deletions Protocol.Core/Compression/VarShort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

namespace MinecraftProtocol.Compression
{
Expand All @@ -16,10 +17,7 @@ public static class VarShort

public static int Convert(ReadOnlySpan<byte> bytes) => Read(bytes,out _);
public static int Convert(ReadOnlySpan<byte> bytes, out int length) => Read(bytes,out length);
public static int Convert(byte[] bytes) => Read(bytes as IList<byte>, 0, out _);
public static int Convert(byte[] bytes, int offset) => Read(bytes as IList<byte>, offset, out _);
public static int Convert(byte[] bytes, int offset, out int end) => Read(bytes as IList<byte>, offset, out end);
public static int Convert(IList<byte> bytes) => Read(bytes, 0, out _);

public static int Convert(IList<byte> bytes, int offset) => Read(bytes, offset, out _);
public static int Convert(IList<byte> bytes, int offset, out int end) => Read(bytes, offset, out end);
public static byte[] Convert(int value) => GetBytes(value);
Expand Down Expand Up @@ -55,10 +53,6 @@ public static int Read(Func<int,byte[]> readBytes, out int readCount)
return ((high & 0xFF) << 15) | low;
}

public static int Read(byte[] bytes) => Read(bytes as IList<byte>, 0, out _);
public static int Read(byte[] bytes, int offset) => Read(bytes as IList<byte>, offset, out _);
public static int Read(byte[] bytes, int offset, out int length) => Read(bytes as IList<byte>, offset, out length);
public static int Read(IList<byte> bytes) => Read(bytes, 0, out _);
public static int Read(IList<byte> bytes, int offset) => Read(bytes, offset, out _);
public static int Read(IList<byte> bytes, int offset, out int length)
{
Expand All @@ -73,6 +67,7 @@ public static int Read(IList<byte> bytes, int offset, out int length)
}
return ((high & 0xFF) << 15) | low;
}

public static int Read(ReadOnlySpan<byte> bytes) => Read(bytes, out _);
public static int Read(ReadOnlySpan<byte> bytes, out int length)
{
Expand Down Expand Up @@ -114,21 +109,7 @@ public static byte[] GetBytes(int value)
}


public static int GetLength(byte[] bytes, int offset = 0) => GetLength(bytes as IList<byte>, offset);
public static int GetLength(IList<byte> bytes, int offset = 0)
{
if (bytes.Count >= offset + 3 && ((bytes[offset] << 8 | bytes[offset + 1]) & 0x8000) != 0)
return 3;
else
return bytes.Count >= offset + 2 ? 2 : throw new OverflowException("VarShort too small");
}
public static int GetLength(ReadOnlySpan<byte> bytes)
{
if (bytes.Length >= 3 && ((bytes[0] << 8 | bytes[1]) & 0x8000) != 0)
return 3;
else
return bytes.Length >= 2 ? 2 : throw new OverflowException("VarShort too short");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetLength(int value)
{
if ((value & 0x7F8000) >> 15 != 0)
Expand Down
2 changes: 1 addition & 1 deletion Protocol.Core/IO/ByteWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public virtual int Capacity
public virtual Memory<byte> AsMemory() { ThrowIfDisposed(); return _data.AsMemory(_start, _size); }

//mc大部分都是小包所以使用这种形状的数组池可能更适合?
internal static SawtoothArrayPool<byte> _dataPool = new SawtoothArrayPool<byte>(4096, 2048, 1024, 256, 256, 256, 256, 256, 256, 256, 128, 128, 128, 128, 128, 64, 64, 64, 64, 64, 64, 16);
internal static SawtoothArrayPool<byte> _dataPool = new SawtoothArrayPool<byte>(1024, 512, 256, 64, 32, 16);
protected const int DEFUALT_CAPACITY = 16;
internal protected bool _returnToPool;

Expand Down
8 changes: 5 additions & 3 deletions Protocol.Core/IO/PacketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using MinecraftProtocol.Compatible;
Expand All @@ -29,8 +30,8 @@ public partial class PacketListener : NetworkListener, IPacketListener, ICompati
public event CommonEventHandler<object, PacketReceivedEventArgs> PacketReceived;

internal static IPool<PacketReceivedEventArgs> PREAPool = new ObjectPool<PacketReceivedEventArgs>();
internal static ArrayPool<Memory<byte>> _dataBlockPool = new SawtoothArrayPool<Memory<byte>>(1024 * 8, 2048);
internal static ArrayPool<byte[]> _bufferBlockPool = new SawtoothArrayPool<byte[]>(1024 * 8, 512);
internal static ArrayPool<Memory<byte>> _dataBlockPool = new SawtoothArrayPool<Memory<byte>>(2048, 1024);
internal static ArrayPool<byte[]> _bufferBlockPool = new SawtoothArrayPool<byte[]>(512, 256);

private Memory<byte>[] _dataBlock;
private byte[][] _bufferBlock;
Expand Down Expand Up @@ -192,7 +193,7 @@ protected virtual void InvokeReceived()
try
{
PacketReceivedEventArgs prea = _usePool ? PREAPool.Rent() : new PacketReceivedEventArgs();
prea.Setup(_dataBlock, _dataBlockIndex, _bufferBlock, _bufferBlockIndex, _packetDataBlockIndex, _packetLengthOffset, _packetLength, this);
prea.Setup(ref _dataBlock, ref _dataBlockIndex, ref _bufferBlock, ref _bufferBlockIndex, ref _packetDataBlockIndex, ref _packetLengthOffset, ref _packetLength, this);
ResetToPacketLengthRead();
EventUtils.InvokeCancelEvent(PacketReceived, this, prea);
}
Expand All @@ -210,6 +211,7 @@ protected virtual void InvokeReceived()

}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ResetToPacketLengthRead()
{
_state = ReadState.PacketLength;
Expand Down
Loading

0 comments on commit 6dc5dd1

Please sign in to comment.