fix: Fixes sending packets and sidesteps a major issue with stackalloc and PGO in .NET 8 (#1607)

### Summary
- Works around a sneaky edge case bug in the JIT with stackalloc where sometimes the buffer is not zero'd.
- Fixes SendDisplayBoatHS
- Fixes sending health bars in the `SendEverything()` logic.
- Fixes a bug in sizing for some string helper functions.

### Developer Note
We are enabled `SkipLocalsInit` - do not rely on `stackalloc` to be zero'd. To zero the buffer, use `span.Clear();`

Closes #1606
This commit is contained in:
Kamron Batman 2023-11-21 12:18:20 -08:00 • committed by GitHub
parent 79ba1ea917
commit 03f850fe03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 67 additions and 76 deletions

View file

@ -140,15 +140,14 @@ namespace Server.Engines.PartySystem
return;
}
Span<byte> buffer = stackalloc byte[10];
var writer = new SpanWriter(buffer);
var writer = new SpanWriter(stackalloc byte[10]);
writer.Write((byte)0xBF); // Packet ID
writer.Write((ushort)10);
writer.Write((ushort)0x06); // Sub-packet
writer.Write((byte)0x07); // command
writer.Write(leader);
ns.Send(buffer);
ns.Send(writer.Span);
}
}
}

View file

@ -86,6 +86,8 @@ public class ImageTypeInfo
var length = m_Table.Length;
Span<bool> list = stackalloc bool[length];
list.Clear();
var imageTypes = new ImageType[count];
var i = 0;
@ -100,4 +102,4 @@ public class ImageTypeInfo
return imageTypes;
}
}
}

View file

@ -233,7 +233,7 @@ namespace Server.Network
Span<byte> textBuffer = stackalloc byte[TextEncoding.UTF8.GetMaxByteCount(longestTextLine)];
var writer = maxLength > 81920 ? new SpanWriter(maxLength) : new SpanWriter(stackalloc byte[maxLength]);
var writer = maxLength > 1024 ? new SpanWriter(maxLength) : new SpanWriter(stackalloc byte[maxLength]);
writer.Write((byte)0x71); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write((byte)(content ? 0x02 : 0x01)); // Command

View file

@ -0,0 +1,4 @@
using System.Runtime.CompilerServices;
// Skips initializing stackalloc with zeros.
[module: SkipLocalsInit]

View file

@ -106,8 +106,6 @@ public static class BoatPackets
using var builder = new PacketContainerBuilder(stackalloc byte[minLength]);
Span<byte> buffer = builder.GetSpan(OutgoingEntityPackets.MaxWorldEntityPacketLength);
foreach (var entity in boat.GetMovingEntities(true))
{
if (!beholder.CanSee(entity))
@ -115,7 +113,7 @@ public static class BoatPackets
continue;
}
buffer.InitializePacket();
Span<byte> buffer = builder.GetSpan(OutgoingEntityPackets.MaxWorldEntityPacketLength).InitializePacket();
var bytesWritten = OutgoingEntityPackets.CreateWorldEntity(buffer, entity, true);
builder.Advance(bytesWritten);
}

View file

@ -106,6 +106,8 @@ namespace Server.Multis
using var planesWriter = new SpanWriter(planeLength * planeCount);
Span<bool> planesUsed = stackalloc bool[9];
planesUsed.Clear();
int index;
var totalPlaneOffsets = 0;
var totalPlanes = 0;