Skip to content

Commit d5c091c

Browse files
committed
Refactored Grow method
1 parent 9252a1d commit d5c091c

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
88

99
### Changed
1010
- `Dispose` resets the `ValueStringBuilder` to the initial state, so it doesn't lead to undefined behavior when used again
11-
11+
- Use different approach for `Grow` to be a bit more performant
1212
## [1.18.5] - 2023-10-19
1313

1414
### Changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,18 @@ private void Grow(int capacity = 0)
313313
}
314314

315315
var rented = ArrayPool<char>.Shared.Rent(size);
316-
buffer[..bufferPosition].CopyTo(rented);
316+
317+
if (bufferPosition > 0)
318+
{
319+
ref var sourceRef = ref MemoryMarshal.GetReference(buffer);
320+
ref var destinationRef = ref MemoryMarshal.GetReference(rented.AsSpan());
321+
322+
Unsafe.CopyBlock(
323+
ref Unsafe.As<char, byte>(ref destinationRef),
324+
ref Unsafe.As<char, byte>(ref sourceRef),
325+
(uint)(bufferPosition * sizeof(char)));
326+
}
327+
317328
var oldBufferFromPool = arrayFromPool;
318329
buffer = arrayFromPool = rented;
319330

tests/LinkDotNet.StringBuilder.Benchmarks/AppendBenchmark.cs

-23
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@ namespace LinkDotNet.StringBuilder.Benchmarks;
55
[MemoryDiagnoser]
66
public class AppendBenchmarks
77
{
8-
[Benchmark(Baseline = true)]
9-
public string DotNetStringBuilder()
10-
{
11-
var builder = new System.Text.StringBuilder();
12-
builder.AppendLine("That is the first line of our benchmark.");
13-
builder.AppendLine("We can multiple stuff in here if want.");
14-
builder.AppendLine("The idea is that we can resize the internal structure from time to time.");
15-
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
16-
return builder.ToString();
17-
}
18-
198
[Benchmark]
209
public string ValueStringBuilder()
2110
{
@@ -27,16 +16,4 @@ public string ValueStringBuilder()
2716
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
2817
return builder.ToString();
2918
}
30-
31-
[Benchmark]
32-
public string ValueStringBuilderPreAllocated()
33-
{
34-
using var builder = new ValueStringBuilder(stackalloc char[256]);
35-
builder.AppendLine("That is the first line of our benchmark.");
36-
builder.AppendLine("We can multiple stuff in here if want.");
37-
builder.AppendLine("We can multiple stuff in here if want.");
38-
builder.AppendLine("The idea is that we can resize the internal structure from time to time.");
39-
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
40-
return builder.ToString();
41-
}
4219
}

0 commit comments

Comments
 (0)