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 | - | - | ```
This commit is contained in:
parent
a94814a7ef
commit
872de8d095
2 changed files with 41 additions and 18 deletions
|
|
@ -309,7 +309,7 @@ public ref struct ValueStringBuilder
|
|||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Append(ReadOnlySpan<char> value)
|
||||
public void Append(scoped ReadOnlySpan<char> value)
|
||||
{
|
||||
int pos = _length;
|
||||
if (pos > _chars.Length - value.Length)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue