From b2ccc7e4f3e4d1edda1c146077776bdd57233772 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sun, 3 May 2026 18:26:49 -0700 Subject: [PATCH] perf(messages): mechanical interpolation cleanups (#2436) ## Summary Phase 3.1 of the message-interpolation optimization series. Fixes 9 of the 28 sites flagged in the Phase 2 audit (PR #2435): | File | Fix | |---|---| | `Commands/StaffAccess.cs:88,99` | Drop redundant `.ToString()` on enum holes | | `Commands/Handlers.cs:102` | `builder.ToString()` -> `builder.AsSpan()` | | `World Saves/SaveCommands.cs:71-75` | Merge 3 concatenated `$"..."` into one literal | | `Server/Items/Item.cs:4213` | Hoist nested ternary `$"..."` to if/else | | `Engines/Factions/Mobiles/Guards/BaseFactionGuard.cs:140-150` | Convert switch expression to switch statement | | `Mobiles/Monsters/LBR/Jukas/JukaLord.cs:85` | Restructure `string.Format(toSay.RandomElement(), ...)` into switch | | `Misc/AttackMessage.cs:30-41` | Inline `AggressorFormat`/`AggressedFormat` constants | No functional changes. Each site emits identical text; the only difference is that the message string is now built into a pooled char buffer instead of being allocated as a `string` first. --- Projects/Server/Items/Item.cs | 16 ++++++- Projects/UOContent/Commands/Handlers.cs | 2 +- Projects/UOContent/Commands/StaffAccess.cs | 4 +- .../Mobiles/Guards/BaseFactionGuard.cs | 42 ++++++++++++++----- Projects/UOContent/Misc/AttackMessage.cs | 6 +-- .../UOContent/World Saves/SaveCommands.cs | 4 +- 6 files changed, 53 insertions(+), 21 deletions(-) diff --git a/Projects/Server/Items/Item.cs b/Projects/Server/Items/Item.cs index 0e75c175b..bbf718c65 100644 --- a/Projects/Server/Items/Item.cs +++ b/Projects/Server/Items/Item.cs @@ -4106,6 +4106,20 @@ public partial class Item : IHued, IComparable, ISpawnable, IObjectPropert ); } } + else if (m_Amount > 1) + { + ns.SendMessage( + Serial, + m_ItemID, + MessageType.Label, + 0x3B2, + 3, + false, + "ENU", + "", + $"{Name} : {m_Amount}" + ); + } else { ns.SendMessage( @@ -4117,7 +4131,7 @@ public partial class Item : IHued, IComparable, ISpawnable, IObjectPropert false, "ENU", "", - $"{Name}{(m_Amount > 1 ? $" : {m_Amount}" : "")}" + $"{Name}" ); } } diff --git a/Projects/UOContent/Commands/Handlers.cs b/Projects/UOContent/Commands/Handlers.cs index 0918ac87d..d3a9d3350 100644 --- a/Projects/UOContent/Commands/Handlers.cs +++ b/Projects/UOContent/Commands/Handlers.cs @@ -99,7 +99,7 @@ namespace Server.Commands reg = reg.Parent; } - from.SendMessage($"Your region is {builder.ToString()}."); + from.SendMessage($"Your region is {builder.AsSpan()}."); } } } diff --git a/Projects/UOContent/Commands/StaffAccess.cs b/Projects/UOContent/Commands/StaffAccess.cs index 25eabf4a7..69387baee 100644 --- a/Projects/UOContent/Commands/StaffAccess.cs +++ b/Projects/UOContent/Commands/StaffAccess.cs @@ -85,7 +85,7 @@ public static class StaffAccess if ((originalAccessLevel ?? m.AccessLevel) < newAccessLevel) { - m.SendMessage($"You cannot set your staff access to {newAccessLevel.ToString()}."); + m.SendMessage($"You cannot set your staff access to {newAccessLevel}."); return; } @@ -96,6 +96,6 @@ public static class StaffAccess } m.AccessLevel = newAccessLevel; - m.SendMessage($"Staff access set to {newAccessLevel.ToString()}."); + m.SendMessage($"Staff access set to {newAccessLevel}."); } } diff --git a/Projects/UOContent/Engines/Factions/Mobiles/Guards/BaseFactionGuard.cs b/Projects/UOContent/Engines/Factions/Mobiles/Guards/BaseFactionGuard.cs index acb63a3c6..bed521a5c 100644 --- a/Projects/UOContent/Engines/Factions/Mobiles/Guards/BaseFactionGuard.cs +++ b/Projects/UOContent/Engines/Factions/Mobiles/Guards/BaseFactionGuard.cs @@ -137,17 +137,39 @@ namespace Server.Factions { Direction = GetDirectionTo(m); - var warning = Utility.Random(6) switch + switch (Utility.Random(6)) { - 0 => $"I warn you, {m.Name}, you would do well to leave this area before someone shows you the world of gray.", - 1 => $"It would be wise to leave this area, {m.Name}, lest your head become my commanders' trophy.", - 2 => $"You are bold, {m.Name}, for one of the meager {Faction.Find(m)?.Definition.FriendlyName ?? "civilians"}. Leave now, lest you be taught the taste of dirt.", - 3 => $"Your presence here is an insult, {m.Name}. Be gone now, knave.", - 4 => $"Dost thou wish to be hung by your toes, {m.Name}? Nay? Then come no closer.", - _ => $"Hey, {m.Name}. Yeah, you. Get out of here before I beat you with a stick." // 5 - }; - - Say(warning); + case 0: + { + Say($"I warn you, {m.Name}, you would do well to leave this area before someone shows you the world of gray."); + break; + } + case 1: + { + Say($"It would be wise to leave this area, {m.Name}, lest your head become my commanders' trophy."); + break; + } + case 2: + { + Say($"You are bold, {m.Name}, for one of the meager {Faction.Find(m)?.Definition.FriendlyName ?? "civilians"}. Leave now, lest you be taught the taste of dirt."); + break; + } + case 3: + { + Say($"Your presence here is an insult, {m.Name}. Be gone now, knave."); + break; + } + case 4: + { + Say($"Dost thou wish to be hung by your toes, {m.Name}? Nay? Then come no closer."); + break; + } + default: // 5 + { + Say($"Hey, {m.Name}. Yeah, you. Get out of here before I beat you with a stick."); + break; + } + } } } diff --git a/Projects/UOContent/Misc/AttackMessage.cs b/Projects/UOContent/Misc/AttackMessage.cs index 04d4697d1..ed4690ca7 100644 --- a/Projects/UOContent/Misc/AttackMessage.cs +++ b/Projects/UOContent/Misc/AttackMessage.cs @@ -4,8 +4,6 @@ namespace Server.Misc { public static class AttackMessage { - private const string AggressorFormat = "You are attacking {0}!"; - private const string AggressedFormat = "{0} is attacking you!"; private const int Hue = 0x22; private static readonly TimeSpan Delay = TimeSpan.FromMinutes(1.0); @@ -31,13 +29,13 @@ namespace Server.Misc MessageType.Regular, Hue, true, - string.Format(AggressorFormat, aggressed.Name) + $"You are attacking {aggressed.Name}!" ); aggressed.LocalOverheadMessage( MessageType.Regular, Hue, true, - string.Format(AggressedFormat, aggressor.Name) + $"{aggressor.Name} is attacking you!" ); } } diff --git a/Projects/UOContent/World Saves/SaveCommands.cs b/Projects/UOContent/World Saves/SaveCommands.cs index c0186b2b5..76f919556 100644 --- a/Projects/UOContent/World Saves/SaveCommands.cs +++ b/Projects/UOContent/World Saves/SaveCommands.cs @@ -69,9 +69,7 @@ public static class SaveCommands foreach (var dest in destinations) { mobile.SendMessage( - $" - {dest.Name} (retention: {dest.GetRetentionCount(ArchivePeriod.Hourly)}h/" + - $"{dest.GetRetentionCount(ArchivePeriod.Daily)}d/" + - $"{dest.GetRetentionCount(ArchivePeriod.Monthly)}m)" + $" - {dest.Name} (retention: {dest.GetRetentionCount(ArchivePeriod.Hourly)}h/{dest.GetRetentionCount(ArchivePeriod.Daily)}d/{dest.GetRetentionCount(ArchivePeriod.Monthly)}m)" ); }