perf: Zero-alloc interpolation for SendMessage/Overhead APIs (#2434)
## Summary Phase 1 of a multi-phase optimization to eliminate intermediate string allocations between `$"..."` interpolation and the packet text region for ModernUO's player-facing message APIs. - Adds `[InterpolatedStringHandler]` overloads to every `Send*`/`Public/Local/Private/NonlocalOverheadMessage`/`Say`/`Emote`/`Whisper`/`Yell`/`SendLocalizedMessageTo` API in `OutgoingMessagePackets`, `Mobile`, and `Item`. Each overload is a 3-line shim that forwards `handler.Text` to the existing span-based path then calls `handler.Clear()` to return the rented `STArrayPool<char>` buffer (matches the established `SpanWriter.WriteAscii(ref RawInterpolatedStringHandler)` precedent). - Converts `string text/args/affix/name` parameters to `ReadOnlySpan<char>` for consistency with the handler path. `lang` intentionally stays `string` (it's never interpolated and the `??= "ENU"` fallback stays cleaner). - Adds `int charCount` overloads of the three `GetMaxMessage*Length` helpers so stackalloc sizing can avoid the redundant `ROS<char>` round-trip. - Moves `Mobile` (17 methods) and `Item` (4 methods) message methods into new partial-class files (`Mobile.Messages.cs`, `Item.Messages.cs`) for organization. No UOContent call sites change in this PR — existing `string`/`ROS<char>` calls compile unchanged via implicit conversion. Phase 2 (intermediate-string audit) and Phase 3 (cleanup PRs) follow. ## Files - `Projects/Server/Network/Packets/OutgoingMessagePackets.cs` — `string` → `ROS<char>` for text params, `int charCount` length helpers added, class made `partial` - `Projects/Server/Network/Packets/OutgoingMessagePackets.Interpolated.cs` (new) — 3 `ref RawInterpolatedStringHandler` extension overloads - `Projects/Server/Mobiles/Mobile.cs` — message methods extracted (-262 lines) - `Projects/Server/Mobiles/Mobile.Messages.cs` (new, 463 lines) — moved + ROS-converted methods + 25 handler overloads - `Projects/Server/Items/Item.cs` — message methods extracted (-93 lines) - `Projects/Server/Items/Item.Messages.cs` (new, 142 lines) — moved + ROS-converted methods + 4 handler overloads - `Projects/Server.Tests/Tests/Network/Packets/Outgoing/MessagePacketTests.cs` — 3 new regression tests verifying byte-equivalence for the handler overloads
This commit is contained in:
parent
b150c48328
commit
5f9fa88220
13 changed files with 904 additions and 409 deletions
|
|
@ -5611,9 +5611,9 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
|
|||
|
||||
ProcessDelta();
|
||||
|
||||
var regBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
|
||||
var regBuffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text.Length)].InitializePacket();
|
||||
var mutBuffer =
|
||||
stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(mutatedText)].InitializePacket();
|
||||
stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(mutatedText.Length)].InitializePacket();
|
||||
|
||||
// TODO: Should this be sorted like onSpeech is below?
|
||||
for (var i = 0; i < hears.Count; ++i)
|
||||
|
|
@ -8089,29 +8089,6 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
|
|||
public void SayTo(Mobile to, int number, string args = "") =>
|
||||
to.NetState.SendMessageLocalized(Serial, Body, MessageType.Regular, SpeechHue, 3, number, Name, args);
|
||||
|
||||
public void Say(bool ascii, string text) => PublicOverheadMessage(MessageType.Regular, SpeechHue, ascii, text);
|
||||
|
||||
public void Say(string text) => PublicOverheadMessage(MessageType.Regular, SpeechHue, false, text);
|
||||
|
||||
public void Say(int number, AffixType type, string affix, string args) =>
|
||||
PublicOverheadMessage(MessageType.Regular, SpeechHue, number, type, affix, args);
|
||||
|
||||
public void Say(int number, string args = "") => PublicOverheadMessage(MessageType.Regular, SpeechHue, number, args);
|
||||
|
||||
public void Emote(string text) => PublicOverheadMessage(MessageType.Emote, EmoteHue, false, text);
|
||||
|
||||
public void Emote(int number, string args = "") => PublicOverheadMessage(MessageType.Emote, EmoteHue, number, args);
|
||||
|
||||
public void Whisper(string text) => PublicOverheadMessage(MessageType.Whisper, WhisperHue, false, text);
|
||||
|
||||
public void Whisper(int number, string args = "") =>
|
||||
PublicOverheadMessage(MessageType.Whisper, WhisperHue, number, args);
|
||||
|
||||
public void Yell(string text) => PublicOverheadMessage(MessageType.Yell, YellHue, false, text);
|
||||
|
||||
public void Yell(int number, string args = "") =>
|
||||
PublicOverheadMessage(MessageType.Yell, YellHue, number, args);
|
||||
|
||||
public bool SendHuePicker(HuePicker p)
|
||||
{
|
||||
if (m_NetState != null)
|
||||
|
|
@ -8844,245 +8821,6 @@ public partial class Mobile : IHued, IComparable<Mobile>, ISpawnable, IObjectPro
|
|||
public Direction GetDirectionTo(IPoint2D p, bool run = false) =>
|
||||
p == null ? Direction.North | (run ? Direction.Running : 0) : GetDirectionTo(p.X, p.Y, run);
|
||||
|
||||
public void PublicOverheadMessage(
|
||||
MessageType type, int hue, bool ascii, string text, bool noLineOfSight = true,
|
||||
AccessLevel accessLevel = AccessLevel.Player
|
||||
)
|
||||
{
|
||||
if (m_Map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
|
||||
|
||||
foreach (var state in m_Map.GetClientsInRange(m_Location))
|
||||
{
|
||||
if (
|
||||
state.Mobile.AccessLevel >= accessLevel &&
|
||||
state.Mobile.CanSee(this) &&
|
||||
(noLineOfSight || state.Mobile.InLOS(this))
|
||||
)
|
||||
{
|
||||
var length = OutgoingMessagePackets.CreateMessage(
|
||||
buffer,
|
||||
Serial,
|
||||
Body,
|
||||
type,
|
||||
hue,
|
||||
3,
|
||||
ascii,
|
||||
Language,
|
||||
Name,
|
||||
text
|
||||
);
|
||||
|
||||
if (length != buffer.Length)
|
||||
{
|
||||
buffer = buffer[..length]; // Adjust to the actual size
|
||||
}
|
||||
|
||||
state.Send(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void PublicOverheadMessage(MessageType type, int hue, int number, string args = "", bool noLineOfSight = true)
|
||||
{
|
||||
if (m_Map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)].InitializePacket();
|
||||
|
||||
foreach (var state in m_Map.GetClientsInRange(m_Location))
|
||||
{
|
||||
if (state.Mobile.CanSee(this) && (noLineOfSight || state.Mobile.InLOS(this)))
|
||||
{
|
||||
var length = OutgoingMessagePackets.CreateMessageLocalized(
|
||||
buffer,
|
||||
Serial,
|
||||
Body,
|
||||
type,
|
||||
hue,
|
||||
3,
|
||||
number,
|
||||
Name,
|
||||
args
|
||||
);
|
||||
|
||||
if (length != buffer.Length)
|
||||
{
|
||||
buffer = buffer[..length]; // Adjust to the actual size
|
||||
}
|
||||
|
||||
state.Send(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void PublicOverheadMessage(
|
||||
MessageType type, int hue, int number, AffixType affixType, string affix,
|
||||
string args = "", bool noLineOfSight = false,
|
||||
AccessLevel accessLevel = AccessLevel.Player
|
||||
)
|
||||
{
|
||||
if (m_Map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedAffixLength(affix, args)]
|
||||
.InitializePacket();
|
||||
|
||||
foreach (var state in m_Map.GetClientsInRange(m_Location))
|
||||
{
|
||||
if (
|
||||
state.Mobile.AccessLevel >= accessLevel &&
|
||||
state.Mobile.CanSee(this) &&
|
||||
(noLineOfSight || state.Mobile.InLOS(this))
|
||||
)
|
||||
{
|
||||
var length = OutgoingMessagePackets.CreateMessageLocalizedAffix(
|
||||
buffer,
|
||||
Serial,
|
||||
Body,
|
||||
type,
|
||||
hue,
|
||||
3,
|
||||
number,
|
||||
Name,
|
||||
affixType,
|
||||
affix,
|
||||
args
|
||||
);
|
||||
|
||||
if (length != buffer.Length)
|
||||
{
|
||||
buffer = buffer[..length]; // Adjust to the actual size
|
||||
}
|
||||
|
||||
state.Send(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void PrivateOverheadMessage(MessageType type, int hue, bool ascii, string text, NetState state)
|
||||
{
|
||||
state.SendMessage(Serial, Body, type, hue, 3, ascii, m_Language, Name, text);
|
||||
}
|
||||
|
||||
public void PrivateOverheadMessage(MessageType type, int hue, int number, NetState state) =>
|
||||
PrivateOverheadMessage(type, hue, number, "", state);
|
||||
|
||||
public void PrivateOverheadMessage(MessageType type, int hue, int number, string args, NetState state) =>
|
||||
state.SendMessageLocalized(Serial, Body, type, hue, 3, number, Name, args);
|
||||
|
||||
public void LocalOverheadMessage(MessageType type, int hue, bool ascii, string text) =>
|
||||
m_NetState.SendMessage(Serial, Body, type, hue, 3, ascii, m_Language, Name, text);
|
||||
|
||||
public void LocalOverheadMessage(MessageType type, int hue, int number, string args = "") =>
|
||||
m_NetState.SendMessageLocalized(Serial, Body, type, hue, 3, number, Name, args);
|
||||
|
||||
public void NonlocalOverheadMessage(MessageType type, int hue, int number, string args = "")
|
||||
{
|
||||
if (m_Map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)].InitializePacket();
|
||||
|
||||
foreach (var state in m_Map.GetClientsInRange(m_Location))
|
||||
{
|
||||
if (state != m_NetState && state.Mobile.CanSee(this))
|
||||
{
|
||||
var length = OutgoingMessagePackets.CreateMessageLocalized(
|
||||
buffer,
|
||||
Serial,
|
||||
Body,
|
||||
type,
|
||||
hue,
|
||||
3,
|
||||
number,
|
||||
Name,
|
||||
args
|
||||
);
|
||||
|
||||
if (length != buffer.Length)
|
||||
{
|
||||
buffer = buffer[..length]; // Adjust to the actual size
|
||||
}
|
||||
|
||||
state.Send(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void NonlocalOverheadMessage(MessageType type, int hue, bool ascii, string text)
|
||||
{
|
||||
if (m_Map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
|
||||
|
||||
foreach (var state in m_Map.GetClientsInRange(m_Location))
|
||||
{
|
||||
if (state != m_NetState && state.Mobile.CanSee(this))
|
||||
{
|
||||
var length = OutgoingMessagePackets.CreateMessage(
|
||||
buffer,
|
||||
Serial,
|
||||
Body,
|
||||
type,
|
||||
hue,
|
||||
3,
|
||||
ascii,
|
||||
Language,
|
||||
Name,
|
||||
text
|
||||
);
|
||||
|
||||
if (length != buffer.Length)
|
||||
{
|
||||
buffer = buffer[..length]; // Adjust to the actual size
|
||||
}
|
||||
|
||||
state.Send(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SendLocalizedMessage(int number, string args = "", int hue = 0x3B2) =>
|
||||
m_NetState.SendMessageLocalized(Serial.MinusOne, -1, MessageType.Regular, hue, 3, number, "System", args);
|
||||
|
||||
public void SendLocalizedMessage(int number, bool append, string affix, string args = "", int hue = 0x3B2) =>
|
||||
m_NetState.SendMessageLocalizedAffix(
|
||||
Serial.MinusOne,
|
||||
-1,
|
||||
MessageType.Regular,
|
||||
hue,
|
||||
3,
|
||||
number,
|
||||
"System",
|
||||
(append ? AffixType.Append : AffixType.Prepend) | AffixType.System,
|
||||
affix,
|
||||
args
|
||||
);
|
||||
|
||||
public void SendMessage(string text) => SendMessage(0x3B2, text);
|
||||
|
||||
public void SendMessage(int hue, string text) =>
|
||||
m_NetState.SendMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, false, "ENU", "System", text);
|
||||
|
||||
public void SendAsciiMessage(string text) => SendAsciiMessage(0x3B2, text);
|
||||
|
||||
public void SendAsciiMessage(int hue, string text) =>
|
||||
m_NetState.SendMessage(Serial.MinusOne, -1, MessageType.Regular, hue, 3, true, null, "System", text);
|
||||
|
||||
/// <summary>
|
||||
/// Overridable. Event invoked when the Mobile is double clicked. By default, this method can either dismount or open the
|
||||
/// paperdoll.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue