ModernUO/Projects/Server/Buffers/PooledArraySpanFormattable.cs
Kamron Batman 87b63b38a5
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.
2022-05-10 18:50:23 -07:00

70 lines
2.4 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: PooledArraySpanFormattable.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/>. *
*************************************************************************/
#nullable enable
using System;
namespace Server.Buffers;
public struct PooledArraySpanFormattable : ISpanFormattable, IDisposable
{
private char[] _arrayToReturnToPool;
private int _pos;
public PooledArraySpanFormattable(char[] arrayToReturnToPool, int length)
{
_arrayToReturnToPool = arrayToReturnToPool;
_pos = length;
}
public ReadOnlySpan<char> Chars => _arrayToReturnToPool.AsSpan(.._pos);
public static implicit operator string(PooledArraySpanFormattable f) => f.ToString();
public string ToString(string? format = null, IFormatProvider formatProvider = null)
{
var result = new string(_arrayToReturnToPool.AsSpan(0, _pos));
Dispose();
return result;
}
public bool TryFormat(
Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default,
IFormatProvider provider = null
)
{
if (destination.Length < _pos)
{
charsWritten = 0;
return false;
}
_arrayToReturnToPool.AsSpan(0, _pos).CopyTo(destination);
Dispose();
charsWritten = _pos;
return true;
}
public void Dispose()
{
if (_arrayToReturnToPool != null)
{
STArrayPool<char>.Shared.Return(_arrayToReturnToPool);
_arrayToReturnToPool = null;
}
}
}