Closed
Description
Strings in .NET are immutable, and we want to experiment with runtime features (deduplication, codegen optimizations, etc.) which rely on this fact. To accomplish this we'll need to start moving away from existing code which mutates string instances in-place. Here's a first-pass at all the places in this repo which mutate strings and recommended workarounds for them.
aspnetcore/src/Shared/ServerInfrastructure/StringUtilities.cs
Lines 73 to 94 in 8f56489
Suggested workaround (pseudocode):
fixed (byte* pBytes = bytes)
{
return string.Create(bytes.Length, (IntPtr)pBytes, (span, ptr) =>
{
fixed (char* pChars = span)
{
WidenAndCopy((byte*)ptr, pChars, span.Length);
}
});
}
Suggested workaround: Write to a stackalloced buffer, or rent a char array and write to that, then turn it into a string. Or use string.Create
and avoid the intermediate rentals.