fix: Eliminates string allocations while writing gump packets (#1017)

* Introduces `RawInterpolatedStringHandler` which is exactly the same as `DefaultInterpolatedStringHandler` except it _unsafely exposes_ it's `ReadOnlySpan<char>` buffer. This is useful for writing the string's data without actually building the string.
* Uses this new string interpolation handler in `SpanWriter` to eliminate intermediate strings built. This is immensely useful in eliminating string allocations in writing Gump packets.
This commit is contained in:
Kamron Batman 2022-05-10 18:50:23 -07:00 • committed by GitHub
parent f7cbeacf48
commit 87b63b38a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 674 additions and 43 deletions

View file

@ -166,7 +166,7 @@ namespace System.Buffers
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(Encoding encoding, bool safeString = false, int fixedLength = -1)
{
int sizeT = TextEncoding.GetByteLengthForEncoding(encoding);
int byteLength = encoding.GetByteLengthForEncoding();
bool isFixedLength = fixedLength > -1;
@ -174,7 +174,7 @@ namespace System.Buffers
int size;
if (isFixedLength)
{
size = fixedLength * sizeT;
size = fixedLength * byteLength;
if (size > Remaining)
{
throw new OutOfMemoryException();
@ -183,8 +183,8 @@ namespace System.Buffers
else
{
// In case the remaining is not evenly divisible
size = remaining - (remaining & (sizeT - 1));
int index = _buffer.Slice(Position, size).IndexOfTerminator(sizeT);
size = remaining - (remaining & (byteLength - 1));
int index = _buffer.Slice(Position, size).IndexOfTerminator(byteLength);
size = index < 0 ? size : index;
}