fix: Make RelayInfo zero allocation (#1699)

This commit is contained in:
Stefano Merotta 2024-03-09 07:58:32 +01:00 committed by GitHub
parent cfe9e04c78
commit 76f132fd49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 64 deletions

View file

@ -13,61 +13,43 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using Server.Text;
using System;
namespace Server.Gumps;
public readonly struct TextRelay
public readonly ref struct RelayInfo
{
public TextRelay(int entryId, string text)
private readonly ReadOnlySpan<byte> _textBlock;
private readonly ReadOnlySpan<ushort> _textIds;
private readonly ReadOnlySpan<Range> _textRanges;
public RelayInfo(
int buttonId,
ReadOnlySpan<int> switches,
ReadOnlySpan<ushort> textIds,
ReadOnlySpan<Range> textRanges,
ReadOnlySpan<byte> textBlock
)
{
EntryId = entryId;
Text = text;
}
public int EntryId { get; }
public string Text { get; }
}
public ref struct RelayInfo
{
public RelayInfo(int buttonID, int[] switches, TextRelay[] textEntries)
{
ButtonID = buttonID;
ButtonID = buttonId;
Switches = switches;
TextEntries = textEntries;
_textIds = textIds;
_textRanges = textRanges;
_textBlock = textBlock;
}
public int ButtonID { get; }
public ReadOnlySpan<int> Switches { get; }
public ReadOnlySpan<TextRelay> TextEntries { get; }
public bool IsSwitched(int switchID)
{
for (var i = 0; i < Switches.Length; ++i)
{
if (Switches[i] == switchID)
{
return true;
}
}
return false;
}
public bool IsSwitched(int switchId) => Switches.Contains(switchId);
public string GetTextEntry(int entryId)
{
for (var i = 0; i < TextEntries.Length; ++i)
{
if (TextEntries[i].EntryId == entryId)
{
return TextEntries[i].Text;
}
}
return default;
int index = _textIds.IndexOf((ushort)entryId);
return index == -1
? default
: TextEncoding.GetString(_textBlock[_textRanges[index]], TextEncoding.Unicode, true);
}
}