feat: Replaces Zlib with LibDeflate (#1774)

> [!Warning]
> Users on Linux/OSX will need to follow the Readme
> and make sure `libdeflate` is properly installed

> [!Note]
> **Developer Note**
> The API for compression has changed. Use `Deflate.Standard` for the same functionality.

### Summary
* Replaces Zlib with LibDeflate for a 50% performance improvement!
* Adds MacOS 14 to properly test Arm64
This commit is contained in:
Kamron Batman 2024-05-16 22:53:01 -07:00 committed by GitHub
parent 35a292d2c7
commit 1f701e7b55
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 279 additions and 282 deletions

View file

@ -16,10 +16,9 @@
using System;
using System.Buffers;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using Server.Buffers;
using Server.Collections;
using Server.Compression;
using Server.Gumps;
using Server.Logging;
@ -49,7 +48,6 @@ public static class OutgoingGumpPackets
private static readonly byte[] _layoutBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly byte[] _stringsBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly byte[] _packBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly OrderedSet<string> _stringsList = new();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -63,36 +61,21 @@ public static class OutgoingGumpPackets
return;
}
var wantLength = Zlib.MaxPackSize(length);
var packBuffer = _packBuffer;
byte[] rentedBuffer = null;
var dest = writer.RawBuffer[(writer.Position + 8)..];
if (wantLength > packBuffer.Length)
var bytesPacked = Deflate.Standard.Pack(dest, span);
if (bytesPacked == 0)
{
packBuffer = rentedBuffer = STArrayPool<byte>.Shared.Rent(wantLength);
}
var packLength = wantLength;
var error = Zlib.Pack(packBuffer, ref packLength, span, length, ZlibQuality.Default);
if (error != ZlibError.Okay)
{
logger.Warning("Gump compression failed: {Error}", error);
logger.Warning("Gump compression failed");
writer.Write(4);
writer.Write(0);
return;
}
writer.Write(4 + packLength);
writer.Write(4 + bytesPacked);
writer.Write(length);
writer.Write(packBuffer.AsSpan(0, packLength));
if (rentedBuffer != null)
{
STArrayPool<byte>.Shared.Return(rentedBuffer);
}
writer.Seek(bytesPacked, SeekOrigin.Current);
}
public static void SendDisplayGump(this NetState ns, Gump gump, out int switches, out int entries)
@ -141,11 +124,8 @@ public static class OutgoingGumpPackets
stringsWriter.WriteBigUni(s);
}
var worstLayoutLength = Zlib.MaxPackSize(layoutWriter.BytesWritten);
var worstStringsLength = Zlib.MaxPackSize(stringsWriter.BytesWritten);
var maxLength = 40 + worstLayoutLength + worstStringsLength;
var writer = new SpanWriter(maxLength);
var writer = new SpanWriter(0x10000);
writer.Write((byte)0xDD); // Packet ID
writer.Seek(2, SeekOrigin.Current);
@ -171,6 +151,8 @@ public static class OutgoingGumpPackets
{
_stringsList.Clear();
}
writer.Dispose();
}
public static void SendDisplaySignGump(this NetState ns, Serial serial, int gumpId, string unknown, string caption)