High performance StringPool base on ArrayPool.
$ dotnet add package StringPoolArrayPool design, optimized for reusing string instances.ArrayPool or manual caching lack dedicated support for string pooling.StringPool provides a standardized, efficient way to manage reusable strings without GC overhead.StringPoolpublic string Rent(int minimumLength)
public void Return(string array, bool clearArray = true)
clearArray is true (default), the string will be zeroed.public string Rent(ReadOnlySpan<char> buffer)
UnsafeStringpublic bool SetText(ReadOnlySpan<char> buffer)
Rents a new internal buffer if the current buffer is null;
or resizes the internal buffer if the current buffer's capacity is insufficient.
Then copies characters from the provided ReadOnlySpan<char> buffer into the internal buffer,
returns true if a new buffer was rented,
returns false if no new buffer was rented.
You can use public static implicit operator string?(UnsafeString? @string) to convert an UnsafeString to a string,
the string's Length will be equal to buffer.Length.
public void Dispose()
null.CustomizationYou can use:
public static unsafe void Custom(Func<double> getMemoryPressure)
public static unsafe void Custom(nint getMemoryPressure)
public static unsafe void Custom(delegate* managed<double> getMemoryPressure)
to customize how StringPool determines memory pressure for its automatic Trim() behavior.
< 0.7 -> low[0.7, 0.9) -> medium>= 0.9 -> highYou can use:
public static void Configure(int DOTNET_SYSTEM_BUFFERS_SHAREDSTRINGPOOL_MAXSTRINGSPERPARTITION = 256, int DOTNET_SYSTEM_BUFFERS_SHAREDSTRINGPOOL_MAXPARTITIONCOUNT = int.MaxValue)to pre-configure the StringPool.Shared parameters before initialization.
DOTNET_SYSTEM_BUFFERS_SHAREDSTRINGPOOL_MAXSTRINGSPERPARTITION: Maximum strings per partition (default: 256)DOTNET_SYSTEM_BUFFERS_SHAREDSTRINGPOOL_MAXPARTITIONCOUNT: Maximum partition count (default: int.MaxValue, clamped to Environment.ProcessorCount)⚠️ Has no effect after StringPool.Shared is initialized.
⚠️ The actual length of the string rented from StringPool's Rent may not match the requested length.
UnsafeString's public bool SetText(ReadOnlySpan<char> buffer),UnsafeString's public void Dispose().⚠️ The string converted from UnsafeString:
UnsafeString itself.strings converted from UnsafeString every time you update the UnsafeString.