fix: Optimize OrderedHashSet and introduce more high perf collections (#679)

* Optimizes OrderedHashSet using the learnings here: https://github.com/dotnet/runtime/issues/10050
* Removes ValueCollection since it was a relic from the conversion of the OrderedDictionary
* Centralizes throw strings.
* Adds pooled ordered hash set (but doesn't use it)

Closes #448
This commit is contained in:
Kamron Batman 2021-08-08 21:37:25 -07:00 • committed by GitHub
parent d10e782371
commit b0f8fe0350
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 2495 additions and 481 deletions

View file

@ -9,51 +9,37 @@ namespace Benchmarks
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class BenchmarkOrderedHashSet
{
private List<string> _list;
private OrderedHashSet<string> _ordered;
private HashSet<string> _hashSet;
private const int iterations = 16;
private readonly string[] _iterations = new string[16];
[IterationSetup]
public void IterationSetup()
{
_list = new List<string>();
_ordered = new OrderedHashSet<string>();
_hashSet = new HashSet<string>();
}
[IterationCleanup]
public void IterationCleanup()
{
_list = null;
_ordered = null;
_hashSet = null;
for (var i = 0; i < _iterations.Length; i++)
{
_iterations[i] = i.ToString();
}
}
[Benchmark]
public int UsingList()
{
for (int i = 0; i < iterations / 2; i++)
var list = new List<string>();
for (int i = 0; i < _iterations.Length / 2; i++)
{
AddIfNotPresent(_list, i.ToString());
AddIfNotPresent(list, _iterations[i]);
}
for (int i = 0; i < iterations; i++)
for (int i = 0; i < _iterations.Length; i++)
{
AddIfNotPresent(_list, i.ToString());
AddIfNotPresent(list, _iterations[i]);
}
for (int i = 0; i < iterations * 2; i++)
for (int i = 0; i < list.Count; i++)
{
AddIfNotPresent(_list, i.ToString());
list[i].ToString();
}
for (int i = 0; i < _list.Count; i++)
{
_list[i].ToString();
}
return _list.Count;
return list.Count;
}
private static int AddIfNotPresent<T>(List<T> list, T item)
@ -71,53 +57,74 @@ namespace Benchmarks
[Benchmark]
public int UsingOrderedHashSet()
{
for (int i = 0; i < iterations / 2; i++)
var ordered = new OrderedHashSet<string>();
for (int i = 0; i < _iterations.Length / 2; i++)
{
_ordered.GetOrAdd(i.ToString());
ordered.GetOrAdd(_iterations[i]).ToString();
}
for (int i = 0; i < iterations; i++)
for (int i = 0; i < _iterations.Length; i++)
{
_ordered.GetOrAdd(i.ToString());
ordered.GetOrAdd(_iterations[i]).ToString();
}
for (int i = 0; i < iterations * 2; i++)
{
_ordered.GetOrAdd(i.ToString());
}
foreach (var str in _ordered)
foreach (var str in ordered)
{
str.ToString();
}
return _ordered.Count;
return ordered.Count;
}
[Benchmark]
public int UsingPooledOrderedHashSet()
{
var ordered = new PooledOrderedHashSet<string>();
for (int i = 0; i < _iterations.Length / 2; i++)
{
ordered.GetOrAdd(_iterations[i]).ToString();
}
for (int i = 0; i < _iterations.Length; i++)
{
ordered.GetOrAdd(_iterations[i]).ToString();
}
foreach (var str in ordered)
{
str.ToString();
}
return ordered.Count;
}
[Benchmark]
public int UsingHashSet()
{
for (int i = 0; i < iterations / 2; i++)
var hashSet = new HashSet<(string, int)>(new OrderedStringComparer());
for (int i = 0; i < _iterations.Length / 2; i++)
{
_hashSet.Add(i.ToString());
hashSet.Add((_iterations[i], i));
}
for (int i = 0; i < iterations; i++)
for (int i = 0; i < _iterations.Length; i++)
{
_hashSet.Add(i.ToString());
hashSet.Add((_iterations[i], i));
}
for (int i = 0; i < iterations * 2; i++)
{
_hashSet.Add(i.ToString());
}
foreach (var str in _hashSet)
foreach (var str in hashSet)
{
str.ToString();
}
return _hashSet.Count;
return hashSet.Count;
}
private class OrderedStringComparer : EqualityComparer<(string, int)>
{
public override bool Equals((string, int) x, (string, int) y) => x.Item1.Equals(y.Item1, System.StringComparison.Ordinal);
public override int GetHashCode((string, int) obj) => obj.Item1.GetHashCode();
}
}
}

View file

@ -0,0 +1,173 @@
using System;
using System.Buffers;
using System.IO;
using System.IO.Compression;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Server.Collections;
using Server.Gumps;
using Server.Network;
using Server.Tests.Network;
namespace Benchmarks
{
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class OutgoingGumpPacketBenchmarks
{
private static readonly byte[] _layoutBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
private static readonly byte[] _stringsBuffer = GC.AllocateUninitializedArray<byte>(0x20000);
public static void CreateDisplayGump(Gump gump, out int switches, out int entries)
{
switches = 0;
entries = 0;
const bool packed = false;
var layoutWriter = new SpanWriter(_layoutBuffer);
if (!gump.Draggable)
{
layoutWriter.Write(Gump.NoMove);
}
if (!gump.Closable)
{
layoutWriter.Write(Gump.NoClose);
}
if (!gump.Disposable)
{
layoutWriter.Write(Gump.NoDispose);
}
if (!gump.Resizable)
{
layoutWriter.Write(Gump.NoResize);
}
var stringsList = new OrderedHashSet<string>(32);
foreach (var entry in gump.Entries)
{
entry.AppendTo(ref layoutWriter, stringsList, ref entries, ref switches);
}
var stringsWriter = new SpanWriter(_stringsBuffer);
foreach (var str in stringsList)
{
var s = str ?? "";
stringsWriter.Write((ushort)s.Length);
stringsWriter.WriteBigUni(s);
}
int maxLength;
if (packed)
{
var worstLayoutLength = Zlib.MaxPackSize(layoutWriter.BytesWritten);
var worstStringsLength = Zlib.MaxPackSize(stringsWriter.BytesWritten);
maxLength = 40 + worstLayoutLength + worstStringsLength;
}
else
{
maxLength = 23 + layoutWriter.BytesWritten + stringsWriter.BytesWritten;
}
var writer = new SpanWriter(maxLength);
writer.Write((byte)(packed ? 0xDD : 0xB0)); // Packet ID
writer.Seek(2, SeekOrigin.Current);
writer.Write(gump.Serial);
writer.Write(gump.TypeID);
writer.Write(gump.X);
writer.Write(gump.Y);
if (packed)
{
layoutWriter.Write((byte)0); // Layout text terminator
OutgoingGumpPackets.WritePacked(layoutWriter.Span, ref writer);
writer.Write(stringsList.Count);
OutgoingGumpPackets.WritePacked(stringsWriter.Span, ref writer);
}
else
{
writer.Write((ushort)layoutWriter.BytesWritten);
writer.Write(layoutWriter.Span);
writer.Write((ushort)stringsList.Count);
writer.Write(stringsWriter.Span);
}
writer.WritePacketLength();
layoutWriter.Dispose(); // Just in case
stringsWriter.Dispose(); // Just in case
}
public class NameChangeDeedGump : Gump
{
public NameChangeDeedGump() : base(50, 50)
{
Closable = false;
Draggable = false;
Resizable = false;
AddPage(0);
AddBlackAlpha(10, 120, 250, 85);
AddHtml(10, 125, 250, 20, Color(Center("Name Change Deed"), 0xFFFFFF));
AddLabel(73, 15, 1152, "");
AddLabel(20, 150, 0x480, "New Name:");
AddTextField(100, 150, 150, 20, 0);
AddButtonLabeled(75, 180, 1, "Submit");
}
public void AddBlackAlpha(int x, int y, int width, int height)
{
AddImageTiled(x, y, width, height, 2624);
AddAlphaRegion(x, y, width, height);
}
public void AddTextField(int x, int y, int width, int height, int index)
{
AddBackground(x - 2, y - 2, width + 4, height + 4, 0x2486);
AddTextEntry(x + 2, y + 2, width - 4, height - 4, 0, index, "");
}
public static string Center(string text) => $"<CENTER>{text}</CENTER>";
public static string Color(string text, int color) => $"<BASEFONT COLOR=#{color:X6}>{text}</BASEFONT>";
public void AddButtonLabeled(int x, int y, int buttonID, string text)
{
AddButton(x, y - 1, 4005, 4007, buttonID);
AddHtml(x + 35, y, 240, 20, Color(text, 0xFFFFFF));
}
}
private static Gump _gump;
[GlobalSetup]
public void Setup()
{
_gump = new NameChangeDeedGump();
}
[Benchmark]
public void TestNewStack()
{
CreateDisplayGump(_gump, out var _, out var _);
}
[Benchmark]
public void TestOldStack()
{
_gump.Compile().Compile(false, out var _);
}
}
}

View file

@ -0,0 +1,310 @@
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using Server.Gumps;
using Server.Network;
namespace Server.Tests
{
public interface IGumpWriter
{
int TextEntries { get; set; }
int Switches { get; set; }
void AppendLayout(bool val);
void AppendLayout(int val);
void AppendLayout(uint val);
void AppendLayoutNS(int val);
void AppendLayout(string text);
void AppendLayoutNS(string text);
void AppendLayout(byte[] buffer);
void WriteStrings(List<string> strings);
void Flush();
}
public sealed class CloseGump : Packet
{
public CloseGump(int typeID, int buttonID) : base(0xBF)
{
EnsureCapacity(13);
Stream.Write((short)0x04);
Stream.Write(typeID);
Stream.Write(buttonID);
}
}
public sealed class DisplayGumpPacked : Packet, IGumpWriter
{
private static readonly byte[] m_True = Gump.StringToBuffer(" 1");
private static readonly byte[] m_False = Gump.StringToBuffer(" 0");
private static readonly byte[] m_BeginTextSeparator = Gump.StringToBuffer(" @");
private static readonly byte[] m_EndTextSeparator = Gump.StringToBuffer("@");
private static readonly byte[] m_Buffer = new byte[48];
private readonly Gump m_Gump;
private readonly PacketWriter m_Layout;
private readonly PacketWriter m_Strings;
private int m_StringCount;
static DisplayGumpPacked() => m_Buffer[0] = (byte)' ';
public DisplayGumpPacked(Gump gump)
: base(0xDD)
{
m_Gump = gump;
m_Layout = PacketWriter.CreateInstance(8192);
m_Strings = PacketWriter.CreateInstance(8192);
}
public int TextEntries { get; set; }
public int Switches { get; set; }
public void AppendLayout(bool val)
{
AppendLayout(val ? m_True : m_False);
}
public void AppendLayout(int val)
{
var toString = val.ToString();
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
m_Layout.Write(m_Buffer, 0, bytes);
}
public void AppendLayout(uint val)
{
var toString = val.ToString();
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
m_Layout.Write(m_Buffer, 0, bytes);
}
public void AppendLayoutNS(int val)
{
var toString = val.ToString();
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1);
m_Layout.Write(m_Buffer, 1, bytes);
}
public void AppendLayoutNS(string text)
{
m_Layout.WriteAsciiFixed(text, text.Length);
}
public void AppendLayout(string text)
{
AppendLayout(m_BeginTextSeparator);
m_Layout.WriteAsciiFixed(text, text.Length);
AppendLayout(m_EndTextSeparator);
}
public void AppendLayout(byte[] buffer)
{
m_Layout.Write(buffer, 0, buffer.Length);
}
public void WriteStrings(List<string> strings)
{
m_StringCount = strings.Count;
for (var i = 0; i < strings.Count; ++i)
{
var v = strings[i] ?? "";
m_Strings.Write((ushort)v.Length);
m_Strings.WriteBigUniFixed(v, v.Length);
}
}
public void Flush()
{
EnsureCapacity(28 + (int)m_Layout.Length + (int)m_Strings.Length);
Stream.Write(m_Gump.Serial);
Stream.Write(m_Gump.TypeID);
Stream.Write(m_Gump.X);
Stream.Write(m_Gump.Y);
// Note: layout MUST be null terminated (don't listen to krrios)
m_Layout.Write((byte)0);
WritePacked(m_Layout);
Stream.Write(m_StringCount);
WritePacked(m_Strings);
PacketWriter.ReleaseInstance(m_Layout);
PacketWriter.ReleaseInstance(m_Strings);
}
private void WritePacked(PacketWriter src)
{
var buffer = src.UnderlyingStream.GetBuffer();
var length = (int)src.Length;
if (length == 0)
{
Stream.Write(0);
return;
}
var wantLength = 1 + length * 1024 / 1000;
wantLength += 4095;
wantLength &= ~4095;
var packBuffer = ArrayPool<byte>.Shared.Rent(wantLength);
var packLength = wantLength;
Zlib.Pack(packBuffer, ref packLength, buffer, length, ZlibQuality.Default);
Stream.Write(4 + packLength);
Stream.Write(length);
Stream.Write(packBuffer, 0, packLength);
ArrayPool<byte>.Shared.Return(packBuffer);
}
}
public sealed class DisplayGumpFast : Packet, IGumpWriter
{
private static readonly byte[] m_True = Gump.StringToBuffer(" 1");
private static readonly byte[] m_False = Gump.StringToBuffer(" 0");
private static readonly byte[] m_BeginTextSeparator = Gump.StringToBuffer(" @");
private static readonly byte[] m_EndTextSeparator = Gump.StringToBuffer("@");
private readonly byte[] m_Buffer = new byte[48];
private int m_LayoutLength;
public DisplayGumpFast(Gump g) : base(0xB0)
{
m_Buffer[0] = (byte)' ';
EnsureCapacity(4096);
Stream.Write(g.Serial);
Stream.Write(g.TypeID);
Stream.Write(g.X);
Stream.Write(g.Y);
Stream.Write((ushort)0xFFFF);
}
public int TextEntries { get; set; }
public int Switches { get; set; }
public void AppendLayout(bool val)
{
AppendLayout(val ? m_True : m_False);
}
public void AppendLayout(int val)
{
var toString = val.ToString();
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
Stream.Write(m_Buffer, 0, bytes);
m_LayoutLength += bytes;
}
public void AppendLayout(uint val)
{
var toString = val.ToString();
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1) + 1;
Stream.Write(m_Buffer, 0, bytes);
m_LayoutLength += bytes;
}
public void AppendLayoutNS(int val)
{
var toString = val.ToString();
var bytes = Encoding.ASCII.GetBytes(toString, 0, toString.Length, m_Buffer, 1);
Stream.Write(m_Buffer, 1, bytes);
m_LayoutLength += bytes;
}
public void AppendLayoutNS(string text)
{
var length = text.Length;
Stream.WriteAsciiFixed(text, length);
m_LayoutLength += length;
}
public void AppendLayout(string text)
{
AppendLayout(m_BeginTextSeparator);
var length = text.Length;
Stream.WriteAsciiFixed(text, length);
m_LayoutLength += length;
AppendLayout(m_EndTextSeparator);
}
public void AppendLayout(byte[] buffer)
{
var length = buffer.Length;
Stream.Write(buffer, 0, length);
m_LayoutLength += length;
}
public void WriteStrings(List<string> text)
{
Stream.Seek(19, SeekOrigin.Begin);
Stream.Write((ushort)m_LayoutLength);
Stream.Seek(0, SeekOrigin.End);
Stream.Write((ushort)text.Count);
for (var i = 0; i < text.Count; ++i)
{
var v = text[i] ?? "";
int length = (ushort)v.Length;
Stream.Write((ushort)length);
Stream.WriteBigUniFixed(v, length);
}
}
public void Flush()
{
}
}
public sealed class DisplaySignGump : Packet
{
public DisplaySignGump(Serial serial, int gumpID, string unknown, string caption) : base(0x8B)
{
unknown ??= "";
caption ??= "";
EnsureCapacity(15 + unknown.Length + caption.Length);
Stream.Write(serial);
Stream.Write((short)gumpID);
Stream.Write((short)(unknown.Length + 1));
Stream.WriteAsciiNull(unknown);
Stream.Write((short)(caption.Length + 1));
Stream.WriteAsciiNull(caption);
}
}
}

View file

@ -0,0 +1,476 @@
using System.Collections.Generic;
using Server.Gumps;
using Server.Network;
namespace Server.Tests.Network
{
public static class GumpUtilities
{
private static readonly byte[] m_BeginLayout = Gump.StringToBuffer("{ ");
private static readonly byte[] m_EndLayout = Gump.StringToBuffer(" }");
public static Packet Compile(this Gump g, NetState ns = null)
{
IGumpWriter disp = new DisplayGumpFast(g);
// IGumpWriter disp = new DisplayGumpPacked(g);
if (!g.Draggable)
{
disp.AppendLayout(Gump.NoMove);
}
if (!g.Closable)
{
disp.AppendLayout(Gump.NoClose);
}
if (!g.Disposable)
{
disp.AppendLayout(Gump.NoDispose);
}
if (!g.Resizable)
{
disp.AppendLayout(Gump.NoResize);
}
var count = g.Entries.Count;
var strings = new List<string>();
for (var i = 0; i < count; ++i)
{
var e = g.Entries[i];
disp.AppendLayout(m_BeginLayout);
e.AppendToByType(disp, strings);
disp.AppendLayout(m_EndLayout);
}
disp.WriteStrings(strings);
disp.Flush();
return (Packet)disp;
}
public static int Intern(this List<string> strings, string value)
{
var indexOf = strings.IndexOf(value);
if (indexOf >= 0)
{
return indexOf;
}
strings.Add(value);
return strings.Count - 1;
}
public static void AppendToByType(this GumpEntry e, IGumpWriter disp, List<string> strings)
{
switch (e)
{
case GumpAlphaRegion g:
{
g.AppendTo(disp, strings);
break;
}
case GumpBackground g:
{
g.AppendTo(disp, strings);
break;
}
case GumpButton g:
{
g.AppendTo(disp, strings);
break;
}
case GumpCheck g:
{
g.AppendTo(disp, strings);
break;
}
case GumpGroup g:
{
g.AppendTo(disp, strings);
break;
}
case GumpECHandleInput g:
{
g.AppendTo(disp, strings);
break;
}
case GumpHtml g:
{
g.AppendTo(disp, strings);
break;
}
case GumpHtmlLocalized g:
{
g.AppendTo(disp, strings);
break;
}
case GumpImage g:
{
g.AppendTo(disp, strings);
break;
}
case GumpImageTileButton g:
{
g.AppendTo(disp, strings);
break;
}
case GumpImageTiled g:
{
g.AppendTo(disp, strings);
break;
}
case GumpItem g:
{
g.AppendTo(disp, strings);
break;
}
case GumpItemProperty g:
{
g.AppendTo(disp, strings);
break;
}
case GumpLabel g:
{
g.AppendTo(disp, strings);
break;
}
case GumpLabelCropped g:
{
g.AppendTo(disp, strings);
break;
}
case GumpMasterGump g:
{
g.AppendTo(disp, strings);
break;
}
case GumpPage g:
{
g.AppendTo(disp, strings);
break;
}
case GumpRadio g:
{
g.AppendTo(disp, strings);
break;
}
case GumpSpriteImage g:
{
g.AppendTo(disp, strings);
break;
}
case GumpTextEntry g:
{
g.AppendTo(disp, strings);
break;
}
case GumpTextEntryLimited g:
{
g.AppendTo(disp, strings);
break;
}
case GumpTooltip g:
{
g.AppendTo(disp, strings);
break;
}
}
}
public static void AppendTo(this GumpAlphaRegion g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpAlphaRegion.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
}
public static void AppendTo(this GumpBackground g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpBackground.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.GumpID);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
}
public static void AppendTo(this GumpButton g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpButton.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.NormalID);
disp.AppendLayout(g.PressedID);
disp.AppendLayout((int)g.Type);
disp.AppendLayout(g.Param);
disp.AppendLayout(g.ButtonID);
}
public static void AppendTo(this GumpCheck g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpButton.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.InactiveID);
disp.AppendLayout(g.ActiveID);
disp.AppendLayout(g.InitialState);
disp.AppendLayout(g.SwitchID);
disp.Switches++;
}
public static void AppendTo(this GumpGroup g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpGroup.LayoutName);
disp.AppendLayout(g.Group);
}
public static void AppendTo(this GumpECHandleInput g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpECHandleInput.LayoutName);
}
public static void AppendTo(this GumpHtml g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpHtml.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(strings.Intern(g.Text));
disp.AppendLayout(g.Background);
disp.AppendLayout(g.Scrollbar);
}
public static void AppendTo(this GumpHtmlLocalized g, IGumpWriter disp, List<string> strings)
{
switch (g.Type)
{
case GumpHtmlLocalizedType.Plain:
{
disp.AppendLayout(GumpHtmlLocalized.LayoutNamePlain);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.Number);
disp.AppendLayout(g.Background);
disp.AppendLayout(g.Scrollbar);
break;
}
case GumpHtmlLocalizedType.Color:
{
disp.AppendLayout(GumpHtmlLocalized.LayoutNameColor);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.Number);
disp.AppendLayout(g.Background);
disp.AppendLayout(g.Scrollbar);
disp.AppendLayout(g.Color);
break;
}
case GumpHtmlLocalizedType.Args:
{
disp.AppendLayout(GumpHtmlLocalized.LayoutNameArgs);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.Background);
disp.AppendLayout(g.Scrollbar);
disp.AppendLayout(g.Color);
disp.AppendLayout(g.Number);
disp.AppendLayout(g.Args);
break;
}
}
}
public static void AppendTo(this GumpImage g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpImage.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.GumpID);
if (g.Hue != 0)
{
disp.AppendLayout(GumpImage.HueEquals);
disp.AppendLayoutNS(g.Hue);
}
if (!string.IsNullOrEmpty(g.Class))
{
disp.AppendLayout(GumpImage.ClassEquals);
disp.AppendLayoutNS(g.Class);
}
}
public static void AppendTo(this GumpImageTileButton g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpImageTileButton.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.NormalID);
disp.AppendLayout(g.PressedID);
disp.AppendLayout((int)g.Type);
disp.AppendLayout(g.Param);
disp.AppendLayout(g.ButtonID);
disp.AppendLayout(g.ItemID);
disp.AppendLayout(g.Hue);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
if (g.LocalizedTooltip > 0)
{
disp.AppendLayout(GumpImageTileButton.LayoutTooltip);
disp.AppendLayout(g.LocalizedTooltip);
}
}
public static void AppendTo(this GumpImageTiled g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpImageTiled.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.GumpID);
}
public static void AppendTo(this GumpItem g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(g.Hue == 0 ? GumpItem.LayoutName : GumpItem.LayoutNameHue);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.ItemID);
if (g.Hue != 0)
{
disp.AppendLayout(g.Hue);
}
}
public static void AppendTo(this GumpItemProperty g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpItemProperty.LayoutName);
disp.AppendLayout(g.Serial);
}
public static void AppendTo(this GumpLabel g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpLabel.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Hue);
disp.AppendLayout(strings.Intern(g.Text));
}
public static void AppendTo(this GumpLabelCropped g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpLabelCropped.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.Hue);
disp.AppendLayout(strings.Intern(g.Text));
}
public static void AppendTo(this GumpMasterGump g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpMasterGump.LayoutName);
disp.AppendLayout(g.GumpID);
}
public static void AppendTo(this GumpPage g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpPage.LayoutName);
disp.AppendLayout(g.Page);
}
public static void AppendTo(this GumpRadio g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpRadio.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.InactiveID);
disp.AppendLayout(g.ActiveID);
disp.AppendLayout(g.InitialState);
disp.AppendLayout(g.SwitchID);
disp.Switches++;
}
public static void AppendTo(this GumpSpriteImage g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpSpriteImage.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.GumpID);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.SX);
disp.AppendLayout(g.SY);
}
public static void AppendTo(this GumpTextEntry g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpTextEntry.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.Hue);
disp.AppendLayout(g.EntryID);
disp.AppendLayout(strings.Intern(g.InitialText));
disp.TextEntries++;
}
public static void AppendTo(this GumpTextEntryLimited g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpTextEntryLimited.LayoutName);
disp.AppendLayout(g.X);
disp.AppendLayout(g.Y);
disp.AppendLayout(g.Width);
disp.AppendLayout(g.Height);
disp.AppendLayout(g.Hue);
disp.AppendLayout(g.EntryID);
disp.AppendLayout(strings.Intern(g.InitialText));
disp.AppendLayout(g.Size);
disp.TextEntries++;
}
public static void AppendTo(this GumpTooltip g, IGumpWriter disp, List<string> strings)
{
disp.AppendLayout(GumpTooltip.LayoutName);
disp.AppendLayout(g.Number);
if (!string.IsNullOrEmpty(g.Args))
{
disp.AppendLayout(g.Args);
}
}
}
}

View file

@ -0,0 +1,265 @@
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using Server.Diagnostics;
namespace Server.Network
{
public abstract class Packet
{
private const int CompressorBufferSize = 0x10000;
private readonly int m_Length;
private byte[] m_CompiledBuffer;
private int m_CompiledLength;
private State m_State;
protected Packet(int packetID)
{
PacketID = packetID;
if (Core.Profiling)
{
var prof = PacketSendProfile.Acquire(PacketID);
prof.Increment();
}
}
protected Packet(int packetID, int length)
{
PacketID = packetID;
m_Length = length;
Stream = PacketWriter.CreateInstance(length); // new PacketWriter( length );
Stream.Write((byte)packetID);
if (Core.Profiling)
{
var prof = PacketSendProfile.Acquire(PacketID);
prof.Increment();
}
}
public int PacketID { get; }
public PacketWriter Stream { get; protected set; }
public void EnsureCapacity(int length)
{
Stream = PacketWriter.CreateInstance(length); // new PacketWriter( length );
Stream.Write((byte)PacketID);
Stream.Write((short)0);
}
public static Packet SetStatic(Packet p)
{
p.SetStatic();
return p;
}
public static Packet Acquire(Packet p)
{
p.Acquire();
return p;
}
public static void Release(ref Packet p)
{
p?.Release();
p = null;
}
public static void Release(Packet p)
{
p?.Release();
}
public void SetStatic()
{
m_State |= State.Static | State.Acquired;
}
public void Acquire()
{
m_State |= State.Acquired;
}
public void OnSend()
{
if ((m_State & (State.Acquired | State.Static)) == 0)
{
Free();
}
}
private void Free()
{
if (m_CompiledBuffer == null)
{
return;
}
if ((m_State & State.Buffered) != 0)
{
ArrayPool<byte>.Shared.Return(m_CompiledBuffer);
}
m_State &= ~(State.Static | State.Acquired | State.Buffered);
m_CompiledBuffer = null;
}
public void Release()
{
if ((m_State & State.Acquired) != 0)
{
Free();
}
}
private readonly object _object = new();
public byte[] Compile(bool compress, out int length)
{
lock (_object)
{
if (m_CompiledBuffer == null)
{
if ((m_State & State.Accessed) == 0)
{
m_State |= State.Accessed;
}
else
{
if ((m_State & State.Warned) == 0)
{
m_State |= State.Warned;
try
{
using var op = new StreamWriter("net_opt.log", true);
op.WriteLine("Redundant compile for packet {0}, use Acquire() and Release()", GetType());
op.WriteLine(new StackTrace());
}
catch
{
// ignored
}
}
m_CompiledBuffer = Array.Empty<byte>();
m_CompiledLength = 0;
length = m_CompiledLength;
return m_CompiledBuffer;
}
InternalCompile(compress);
}
length = m_CompiledLength;
return m_CompiledBuffer;
}
}
private void InternalCompile(bool compress)
{
if (m_Length == 0)
{
var streamLen = Stream.Length;
Stream.Seek(1, SeekOrigin.Begin);
Stream.Write((ushort)streamLen);
}
else if (Stream.Length != m_Length)
{
var diff = (int)Stream.Length - m_Length;
Console.WriteLine(
"Packet: 0x{0:X2}: Bad packet length! ({1}{2} bytes)",
PacketID,
diff >= 0 ? "+" : "",
diff
);
}
var ms = Stream.UnderlyingStream;
m_CompiledBuffer = ms.GetBuffer();
var length = (int)ms.Length;
if (compress)
{
var compressorBuffer = new byte[CompressorBufferSize];
var compressedLength = NetworkCompression.Compress(m_CompiledBuffer.AsSpan(0, length), compressorBuffer);
if (length <= 0)
{
Console.WriteLine(
"Warning: Compression buffer overflowed on packet 0x{0:X2} ('{1}') (length={2})",
PacketID,
GetType().Name,
length
);
using var op = new StreamWriter("compression_overflow.log", true);
op.WriteLine(
"{0} Warning: Compression buffer overflowed on packet 0x{1:X2} ('{2}') (length={3})",
Core.Now,
PacketID,
GetType().Name,
length
);
op.WriteLine(new StackTrace());
}
else
{
m_CompiledBuffer = compressorBuffer;
m_CompiledLength = compressedLength;
}
}
else
{
m_CompiledLength = length;
}
if (m_CompiledLength > 0)
{
var old = m_CompiledBuffer;
if ((m_State & State.Static) != 0)
{
m_CompiledBuffer = new byte[m_CompiledLength];
}
else
{
// Release it later using Release()
m_CompiledBuffer = ArrayPool<byte>.Shared.Rent(m_CompiledLength);
m_State |= State.Buffered;
}
Buffer.BlockCopy(old, 0, m_CompiledBuffer, 0, m_CompiledLength);
if (compress)
{
ArrayPool<byte>.Shared.Return(old);
}
}
PacketWriter.ReleaseInstance(Stream);
Stream = null;
}
[Flags]
private enum State
{
Inactive = 0x00,
Static = 0x01,
Acquired = 0x02,
Accessed = 0x04,
Buffered = 0x08,
Warned = 0x10
}
}
}

View file

@ -2,11 +2,15 @@ using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using Server;
using Server.Network;
namespace Benchmarks
{
public static class PacketTestUtilities
{
public static Span<byte> Compile(this Packet p) =>
p.Compile(false, out var length).AsSpan(0, length);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Write(this Span<byte> data, ref int pos, Serial serial)
{

View file

@ -0,0 +1,352 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text;
namespace Server.Network
{
/// <summary>
/// Provides functionality for writing primitive binary data.
/// </summary>
public class PacketWriter
{
private static readonly ConcurrentQueue<PacketWriter> m_Pool = new();
/// <summary>
/// Internal format buffer.
/// </summary>
private readonly byte[] m_Buffer = new byte[4];
private int m_Capacity;
/// <summary>
/// Instantiates a new PacketWriter instance with a given capacity.
/// </summary>
/// <param name="capacity">Initial capacity for the internal stream.</param>
public PacketWriter(int capacity = 32)
{
UnderlyingStream = new MemoryStream(capacity);
m_Capacity = capacity;
}
/// <summary>
/// Gets the total stream length.
/// </summary>
public long Length => UnderlyingStream.Length;
/// <summary>
/// Gets or sets the current stream position.
/// </summary>
public long Position
{
get => UnderlyingStream.Position;
set => UnderlyingStream.Position = value;
}
/// <summary>
/// The internal stream used by this PacketWriter instance.
/// </summary>
public MemoryStream UnderlyingStream { get; }
public static PacketWriter CreateInstance(int capacity = 32)
{
if (m_Pool.TryDequeue(out var pw))
{
pw.m_Capacity = capacity;
pw.UnderlyingStream.SetLength(0);
return pw;
}
return new PacketWriter(capacity);
}
public static void ReleaseInstance(PacketWriter pw)
{
m_Pool.Enqueue(pw);
}
/// <summary>
/// Writes a 1-byte boolean value to the underlying stream. False is represented by 0, true by 1.
/// </summary>
public void Write(bool value)
{
UnderlyingStream.WriteByte((byte)(value ? 1 : 0));
}
/// <summary>
/// Writes a 1-byte unsigned integer value to the underlying stream.
/// </summary>
public void Write(byte value)
{
UnderlyingStream.WriteByte(value);
}
/// <summary>
/// Writes a 1-byte signed integer value to the underlying stream.
/// </summary>
public void Write(sbyte value)
{
UnderlyingStream.WriteByte((byte)value);
}
/// <summary>
/// Writes a 2-byte signed integer value to the underlying stream.
/// </summary>
public void Write(short value)
{
m_Buffer[0] = (byte)(value >> 8);
m_Buffer[1] = (byte)value;
UnderlyingStream.Write(m_Buffer, 0, 2);
}
/// <summary>
/// Writes a 2-byte unsigned integer value to the underlying stream.
/// </summary>
public void Write(ushort value)
{
m_Buffer[0] = (byte)(value >> 8);
m_Buffer[1] = (byte)value;
UnderlyingStream.Write(m_Buffer, 0, 2);
}
/// <summary>
/// Writes a 4-byte signed integer value to the underlying stream.
/// </summary>
public void Write(int value)
{
m_Buffer[0] = (byte)(value >> 24);
m_Buffer[1] = (byte)(value >> 16);
m_Buffer[2] = (byte)(value >> 8);
m_Buffer[3] = (byte)value;
UnderlyingStream.Write(m_Buffer, 0, 4);
}
/// <summary>
/// Writes a 4-byte unsigned integer value to the underlying stream.
/// </summary>
public void Write(uint value)
{
m_Buffer[0] = (byte)(value >> 24);
m_Buffer[1] = (byte)(value >> 16);
m_Buffer[2] = (byte)(value >> 8);
m_Buffer[3] = (byte)value;
UnderlyingStream.Write(m_Buffer, 0, 4);
}
/// <summary>
/// Writes a sequence of bytes to the underlying stream
/// </summary>
public void Write(byte[] buffer, int offset, int size)
{
UnderlyingStream.Write(buffer, offset, size);
}
/// <summary>
/// Writes a fixed-length ASCII-encoded string value to the underlying stream. To fit (size), the string content is either
/// truncated or padded with null characters.
/// </summary>
public void WriteAsciiFixed(string value, int size)
{
if (value == null)
{
Console.WriteLine("Network: Attempted to WriteAsciiFixed() with null value");
value = string.Empty;
}
var length = value.Length;
UnderlyingStream.SetLength(UnderlyingStream.Length + size);
if (length >= size)
{
UnderlyingStream.Position +=
Encoding.ASCII.GetBytes(value, 0, size, UnderlyingStream.GetBuffer(), (int)UnderlyingStream.Position);
}
else
{
Encoding.ASCII.GetBytes(value, 0, length, UnderlyingStream.GetBuffer(), (int)UnderlyingStream.Position);
UnderlyingStream.Position += size;
}
}
/// <summary>
/// Writes a dynamic-length ASCII-encoded string value to the underlying stream, followed by a 1-byte null character.
/// </summary>
public void WriteAsciiNull(string value)
{
if (value == null)
{
Console.WriteLine("Network: Attempted to WriteAsciiNull() with null value");
value = string.Empty;
}
var length = value.Length;
UnderlyingStream.SetLength(UnderlyingStream.Length + length + 1);
Encoding.ASCII.GetBytes(value, 0, length, UnderlyingStream.GetBuffer(), (int)UnderlyingStream.Position);
UnderlyingStream.Position += length + 1;
}
/// <summary>
/// Writes a dynamic-length little-endian unicode string value to the underlying stream, followed by a 2-byte null
/// character.
/// </summary>
public void WriteLittleUniNull(string value)
{
if (value == null)
{
Console.WriteLine("Network: Attempted to WriteLittleUniNull() with null value");
value = string.Empty;
}
var length = value.Length;
UnderlyingStream.SetLength(UnderlyingStream.Length + (length + 1) * 2);
UnderlyingStream.Position +=
Encoding.Unicode.GetBytes(value, 0, length, UnderlyingStream.GetBuffer(), (int)UnderlyingStream.Position);
UnderlyingStream.Position += 2;
}
/// <summary>
/// Writes a fixed-length little-endian unicode string value to the underlying stream. To fit (size), the string content is
/// either truncated or padded with null characters.
/// </summary>
public void WriteLittleUniFixed(string value, int size)
{
if (value == null)
{
Console.WriteLine("Network: Attempted to WriteLittleUniFixed() with null value");
value = string.Empty;
}
var length = value.Length;
size *= 2;
UnderlyingStream.SetLength(UnderlyingStream.Length + size);
if (length * 2 >= size)
{
UnderlyingStream.Position +=
Encoding.Unicode.GetBytes(
value,
0,
size / 2,
UnderlyingStream.GetBuffer(),
(int)UnderlyingStream.Position
);
}
else
{
Encoding.Unicode.GetBytes(value, 0, length, UnderlyingStream.GetBuffer(), (int)UnderlyingStream.Position);
UnderlyingStream.Position += size;
}
}
/// <summary>
/// Writes a dynamic-length big-endian unicode string value to the underlying stream, followed by a 2-byte null character.
/// </summary>
public void WriteBigUniNull(string value)
{
if (value == null)
{
Console.WriteLine("Network: Attempted to WriteBigUniNull() with null value");
value = string.Empty;
}
var length = value.Length;
UnderlyingStream.SetLength(UnderlyingStream.Length + (length + 1) * 2);
UnderlyingStream.Position +=
Encoding.BigEndianUnicode.GetBytes(
value,
0,
length,
UnderlyingStream.GetBuffer(),
(int)UnderlyingStream.Position
);
UnderlyingStream.Position += 2;
}
/// <summary>
/// Writes a fixed-length big-endian unicode string value to the underlying stream. To fit (size), the string content is
/// either truncated or padded with null characters.
/// </summary>
public void WriteBigUniFixed(string value, int size)
{
if (value == null)
{
Console.WriteLine("Network: Attempted to WriteBigUniFixed() with null value");
value = string.Empty;
}
var length = value.Length;
size *= 2;
UnderlyingStream.SetLength(UnderlyingStream.Length + size);
if (length * 2 >= size)
{
UnderlyingStream.Position +=
Encoding.BigEndianUnicode.GetBytes(
value,
0,
size / 2,
UnderlyingStream.GetBuffer(),
(int)UnderlyingStream.Position
);
}
else
{
Encoding.BigEndianUnicode.GetBytes(
value,
0,
length,
UnderlyingStream.GetBuffer(),
(int)UnderlyingStream.Position
);
UnderlyingStream.Position += size;
}
}
/// <summary>
/// Fills the stream from the current position up to (capacity) with 0x00's
/// </summary>
public void Fill()
{
Fill(m_Capacity - UnderlyingStream.Length);
}
/// <summary>
/// Writes a number of 0x00 byte values to the underlying stream.
/// </summary>
public void Fill(long length)
{
if (UnderlyingStream.Position == UnderlyingStream.Length)
{
UnderlyingStream.SetLength(UnderlyingStream.Length + length);
UnderlyingStream.Seek(0, SeekOrigin.End);
}
else
{
UnderlyingStream.Write(new byte[length], 0, (int)length);
}
}
/// <summary>
/// Offsets the current position from an origin.
/// </summary>
public long Seek(long offset, SeekOrigin origin) => UnderlyingStream.Seek(offset, origin);
/// <summary>
/// Gets the entire stream content as a byte array.
/// </summary>
public byte[] ToArray() => UnderlyingStream.ToArray();
}
}