ModernUO/Projects/Server/Text/TextEncoding.cs
Kamron Batman 872de8d095
fix: Optimizes GetString to eliminate allocations (#2154)
### Summary

* Optimized GetString by eliminating the intermediate string allocation.

** Note **: Encoding.GetChars() is still really inefficient, especially when strings are not aligned or have regular ascii/unicode characters. Thankfully we generally don't have to worry about these odd edge cases, but if they happen then GetChars can allocate hundreds of bytes.


This is a benchmark for just the related changes. NonSpecial are just ascii characters, while the other tests include control codes.
```cs
| Method                         | Mean     | Error   | StdDev  | Gen0   | Allocated |
|------------------------------- |---------:|--------:|--------:|-------:|----------:|
| GetString                      | 306.7 ns | 5.96 ns | 6.86 ns | 0.0124 |     200 B |
| GetStringNotSpecial            | 257.0 ns | 4.83 ns | 4.52 ns | 0.0114 |     184 B |
| GetStringSpanHelpers           | 166.4 ns | 3.26 ns | 3.48 ns |      - |         - |
| GetStringSpanHelpersNotSpecial | 137.3 ns | 1.57 ns | 1.22 ns |      - |         - |
```
2025-04-12 12:42:15 -07:00

169 lines
6.5 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2025 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: TextEncoding.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 System.Text;
using Server.Buffers;
namespace Server.Text;
public static class TextEncoding
{
private static Encoding m_UTF8, m_Unicode, m_UnicodeLE;
public static Encoding UTF8 => m_UTF8 ??= new UTF8Encoding(false, false);
public static Encoding Unicode => m_Unicode ??= new UnicodeEncoding(true, false, false);
public static Encoding UnicodeLE => m_UnicodeLE ??= new UnicodeEncoding(false, false, false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesAscii(this string str) => GetBytes(str, Encoding.ASCII);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesBigUni(this string str) => GetBytes(str, Unicode);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesLittleUni(this string str) => GetBytes(str, UnicodeLE);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesUtf8(this string str) => GetBytes(str, UTF8);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesAscii(this ReadOnlySpan<char> str) => GetBytes(str, Encoding.ASCII);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesBigUni(this ReadOnlySpan<char> str) => GetBytes(str, Unicode);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesLittleUni(this ReadOnlySpan<char> str) => GetBytes(str, UnicodeLE);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] GetBytesUtf8(this ReadOnlySpan<char> str) => GetBytes(str, UTF8);
// Unlike the one built into the encoder, this avoids local init
public static byte[] GetBytes(ReadOnlySpan<char> str, Encoding encoding)
{
if (str.Length == 0)
{
return Array.Empty<byte>();
}
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
encoding.GetBytes(str, bytes);
return bytes;
}
// Unlike the one built into the encoder, this avoids local init
public static byte[] GetBytes(string str, Encoding encoding)
{
if (str.Length == 0)
{
return Array.Empty<byte>();
}
var bytes = GC.AllocateUninitializedArray<byte>(encoding.GetByteCount(str));
encoding.GetBytes(str, 0, str.Length, bytes, 0);
return bytes;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesAscii(this string str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesAscii(this ReadOnlySpan<char> str, Span<byte> buffer) => Encoding.ASCII.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesBigUni(this string str, Span<byte> buffer) => Unicode.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesBigUni(this ReadOnlySpan<char> str, Span<byte> buffer) => Unicode.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesLittleUni(this string str, Span<byte> buffer) => UnicodeLE.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesLittleUni(this ReadOnlySpan<char> str, Span<byte> buffer) => UnicodeLE.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesUtf8(this string str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBytesUtf8(this ReadOnlySpan<char> str, Span<byte> buffer) => UTF8.GetBytes(str, buffer);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetByteLengthForEncoding(this Encoding encoding) =>
encoding.BodyName switch
{
"utf-16BE" => 2,
"utf-16" => 2,
"utf-32BE" => 3,
"utf-32" => 3,
_ => 1
};
public static string GetString(ReadOnlySpan<byte> span, Encoding encoding, bool safeString = false)
{
if (!safeString)
{
return encoding.GetString(span);
}
var charCount = encoding.GetMaxCharCount(span.Length);
char[] rentedChars = null;
Span<char> chars = charCount <= 256
? stackalloc char[charCount]
: rentedChars = STArrayPool<char>.Shared.Rent(charCount);
try
{
var length = encoding.GetChars(span, chars);
chars = chars[..length];
var index = chars.IndexOfAnyExceptInRange((char)0x20, (char)0xFFFD);
if (index == -1)
{
return new string(chars);
}
using var sb = charCount <= 256
? new ValueStringBuilder(stackalloc char[charCount])
: ValueStringBuilder.Create(charCount);
while (index != -1)
{
sb.Append(chars[..index]);
chars = chars[(index + 1)..];
index = chars.IndexOfAnyExceptInRange((char)0x20, (char)0xFFFD);
}
if (chars.Length > 0)
{
sb.Append(chars);
}
return sb.ToString();
}
finally
{
if (rentedChars != null)
{
STArrayPool<char>.Shared.Return(rentedChars);
}
}
}
}