Skip to content

Commit

Permalink
Refactored Grow method
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Nov 3, 2023
1 parent 9252a1d commit d5c091c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T

### Changed
- `Dispose` resets the `ValueStringBuilder` to the initial state, so it doesn't lead to undefined behavior when used again

- Use different approach for `Grow` to be a bit more performant
## [1.18.5] - 2023-10-19

### Changed
Expand Down
13 changes: 12 additions & 1 deletion src/LinkDotNet.StringBuilder/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,18 @@ private void Grow(int capacity = 0)
}

var rented = ArrayPool<char>.Shared.Rent(size);
buffer[..bufferPosition].CopyTo(rented);

if (bufferPosition > 0)
{
ref var sourceRef = ref MemoryMarshal.GetReference(buffer);
ref var destinationRef = ref MemoryMarshal.GetReference(rented.AsSpan());

Unsafe.CopyBlock(
ref Unsafe.As<char, byte>(ref destinationRef),
ref Unsafe.As<char, byte>(ref sourceRef),
(uint)(bufferPosition * sizeof(char)));
}

var oldBufferFromPool = arrayFromPool;
buffer = arrayFromPool = rented;

Expand Down
23 changes: 0 additions & 23 deletions tests/LinkDotNet.StringBuilder.Benchmarks/AppendBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ namespace LinkDotNet.StringBuilder.Benchmarks;
[MemoryDiagnoser]
public class AppendBenchmarks
{
[Benchmark(Baseline = true)]
public string DotNetStringBuilder()
{
var builder = new System.Text.StringBuilder();
builder.AppendLine("That is the first line of our benchmark.");
builder.AppendLine("We can multiple stuff in here if want.");
builder.AppendLine("The idea is that we can resize the internal structure from time to time.");
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
return builder.ToString();
}

[Benchmark]
public string ValueStringBuilder()
{
Expand All @@ -27,16 +16,4 @@ public string ValueStringBuilder()
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
return builder.ToString();
}

[Benchmark]
public string ValueStringBuilderPreAllocated()
{
using var builder = new ValueStringBuilder(stackalloc char[256]);
builder.AppendLine("That is the first line of our benchmark.");
builder.AppendLine("We can multiple stuff in here if want.");
builder.AppendLine("We can multiple stuff in here if want.");
builder.AppendLine("The idea is that we can resize the internal structure from time to time.");
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
return builder.ToString();
}
}

0 comments on commit d5c091c

Please sign in to comment.