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
139
Projects/Server/Items/Item.Messages.cs
Normal file
139
Projects/Server/Items/Item.Messages.cs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2026 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: Item.Messages.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Server.Buffers;
|
||||
using Server.Network;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public partial class Item
|
||||
{
|
||||
private readonly struct PublicVisibilityFilter : IBroadcastFilter
|
||||
{
|
||||
private readonly Item _source;
|
||||
private readonly Point3D _worldLoc;
|
||||
|
||||
public PublicVisibilityFilter(Item source, Point3D worldLoc)
|
||||
{
|
||||
_source = source;
|
||||
_worldLoc = worldLoc;
|
||||
}
|
||||
|
||||
public bool Allow(NetState state)
|
||||
{
|
||||
var m = state.Mobile;
|
||||
return m.CanSee(_source) && m.InRange(_worldLoc, _source.GetUpdateRange(m));
|
||||
}
|
||||
}
|
||||
|
||||
public void PublicOverheadMessage(MessageType type, int hue, bool ascii, ReadOnlySpan<char> text)
|
||||
{
|
||||
var worldLoc = GetWorldLocation();
|
||||
OutgoingMessagePackets.BroadcastMessage(
|
||||
m_Map, worldLoc, GetMaxUpdateRange(),
|
||||
Serial, m_ItemID, type, hue, 3, ascii, "ENU", Name, text,
|
||||
new PublicVisibilityFilter(this, worldLoc)
|
||||
);
|
||||
}
|
||||
|
||||
public void PublicOverheadMessage(MessageType type, int hue, int number, ReadOnlySpan<char> args = default)
|
||||
{
|
||||
var worldLoc = GetWorldLocation();
|
||||
OutgoingMessagePackets.BroadcastMessageLocalized(
|
||||
m_Map, worldLoc, GetMaxUpdateRange(),
|
||||
Serial, m_ItemID, type, hue, 3, number, Name, args,
|
||||
new PublicVisibilityFilter(this, worldLoc)
|
||||
);
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, ReadOnlySpan<char> args = default)
|
||||
{
|
||||
if (Deleted || !to.CanSee(this))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
to?.NetState?.SendMessageLocalized(Serial, ItemID, MessageType.Regular, 0x3B2, 3, number, "", args);
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, AffixType affixType, ReadOnlySpan<char> affix, ReadOnlySpan<char> args)
|
||||
{
|
||||
if (Deleted || !to.CanSee(this))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
to?.NetState?.SendMessageLocalizedAffix(
|
||||
Serial,
|
||||
ItemID,
|
||||
MessageType.Regular,
|
||||
0x3B2,
|
||||
3,
|
||||
number,
|
||||
"",
|
||||
affixType,
|
||||
affix,
|
||||
args
|
||||
);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, int hue, ReadOnlySpan<char> args = default)
|
||||
=> to?.NetState?.SendMessageLocalized(Serial, ItemID, MessageType.Regular, hue, 3, number, "", args);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SendMessageTo(Mobile to, ReadOnlySpan<char> text, int hue = 0x3B2)
|
||||
=> to?.NetState?.SendMessage(Serial, ItemID, MessageType.Regular, hue, 3, false, "ENU", "", text);
|
||||
|
||||
// ---------- Interpolated handler overloads ----------
|
||||
|
||||
public void PublicOverheadMessage(MessageType type, int hue, bool ascii, ref RawInterpolatedStringHandler text)
|
||||
{
|
||||
PublicOverheadMessage(type, hue, ascii, text.Text);
|
||||
text.Clear();
|
||||
}
|
||||
|
||||
public void PublicOverheadMessage(MessageType type, int hue, int number, ref RawInterpolatedStringHandler args)
|
||||
{
|
||||
PublicOverheadMessage(type, hue, number, args.Text);
|
||||
args.Clear();
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, ref RawInterpolatedStringHandler args)
|
||||
{
|
||||
SendLocalizedMessageTo(to, number, args.Text);
|
||||
args.Clear();
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, AffixType affixType, ReadOnlySpan<char> affix, ref RawInterpolatedStringHandler args)
|
||||
{
|
||||
SendLocalizedMessageTo(to, number, affixType, affix, args.Text);
|
||||
args.Clear();
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, int hue, ref RawInterpolatedStringHandler args)
|
||||
{
|
||||
SendLocalizedMessageTo(to, number, hue, args.Text);
|
||||
args.Clear();
|
||||
}
|
||||
|
||||
public void SendMessageTo(Mobile to, int hue, ref RawInterpolatedStringHandler text)
|
||||
{
|
||||
SendMessageTo(to, text.Text, hue);
|
||||
text.Clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -3312,68 +3312,6 @@ public partial class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropert
|
|||
Delete();
|
||||
}
|
||||
|
||||
public void PublicOverheadMessage(MessageType type, int hue, bool ascii, string text)
|
||||
{
|
||||
if (m_Map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var worldLoc = GetWorldLocation();
|
||||
|
||||
var buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLength(text)].InitializePacket();
|
||||
|
||||
foreach (var state in m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange()))
|
||||
{
|
||||
var m = state.Mobile;
|
||||
|
||||
if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m)))
|
||||
{
|
||||
var length = OutgoingMessagePackets.CreateMessage(
|
||||
buffer, Serial, m_ItemID, type, hue, 3, ascii, "ENU", 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 = "")
|
||||
{
|
||||
if (m_Map == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var worldLoc = GetWorldLocation();
|
||||
|
||||
var buffer = stackalloc byte[OutgoingMessagePackets.GetMaxMessageLocalizedLength(args)].InitializePacket();
|
||||
|
||||
foreach (var state in m_Map.GetClientsInRange(worldLoc, GetMaxUpdateRange()))
|
||||
{
|
||||
var m = state.Mobile;
|
||||
|
||||
if (m.CanSee(this) && m.InRange(worldLoc, GetUpdateRange(m)))
|
||||
{
|
||||
var length = OutgoingMessagePackets.CreateMessageLocalized(
|
||||
buffer, Serial, m_ItemID, type, hue, 3, number, Name, args
|
||||
);
|
||||
|
||||
if (length != buffer.Length)
|
||||
{
|
||||
buffer = buffer[..length]; // Adjust to the actual size
|
||||
}
|
||||
|
||||
state.Send(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnAfterDelete()
|
||||
{
|
||||
}
|
||||
|
|
@ -3898,37 +3836,6 @@ public partial class Item : IHued, IComparable<Item>, ISpawnable, IObjectPropert
|
|||
);
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, string args = "")
|
||||
{
|
||||
if (Deleted || !to.CanSee(this))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
to.NetState.SendMessageLocalized(Serial, ItemID, MessageType.Regular, 0x3B2, 3, number, "", args);
|
||||
}
|
||||
|
||||
public void SendLocalizedMessageTo(Mobile to, int number, AffixType affixType, string affix, string args)
|
||||
{
|
||||
if (Deleted || !to.CanSee(this))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
to.NetState.SendMessageLocalizedAffix(
|
||||
Serial,
|
||||
ItemID,
|
||||
MessageType.Regular,
|
||||
0x3B2,
|
||||
3,
|
||||
number,
|
||||
"",
|
||||
affixType,
|
||||
affix,
|
||||
args
|
||||
);
|
||||
}
|
||||
|
||||
public virtual void OnSnoop(Mobile from)
|
||||
{
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue