Skip to content

Commit 46b3ac3

Browse files
committed
Updated some API's and benchmarks
1 parent fb11a3f commit 46b3ac3

File tree

8 files changed

+44
-17
lines changed

8 files changed

+44
-17
lines changed

.DS_Store

-8 KB
Binary file not shown.

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ robots.txt
2323
# Coverage files and reports
2424
/**/TestResults/*
2525
/**/coverage*.xml
26-
/CoverageReport/
26+
/CoverageReport/
27+
28+
# MacOS
29+
.DS_Store

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Added
10+
- Smaller performance improvements in internal API's
11+
912
## [1.4.1] - 2022-11-04
1013

1114
### Added

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@ A more detailed documentation can be found [here](https://linkdotnet.github.io/S
4040
The following table gives you a small comparison between the `StringBuilder` which is part of .NET, [`ZString`](https://github.com/Cysharp/ZString) and the `ValueStringBuilder`:
4141

4242
```no-class
43-
| Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen 0 | Gen 1 | Allocated |
44-
|-------------------- |-----------:|---------:|----------:|-----------:|------:|--------:|--------:|-------:|----------:|
45-
| DotNetStringBuilder | 401.7 ns | 29.15 ns | 84.56 ns | 373.4 ns | 1.00 | 0.00 | 0.3576 | - | 1,496 B |
46-
| ValueStringBuilder | 252.8 ns | 9.05 ns | 26.27 ns | 249.0 ns | 0.65 | 0.13 | 0.1583 | - | 664 B |
47-
| ZStringBuilderUtf8 | 1,239.0 ns | 44.93 ns | 131.06 ns | 1,192.0 ns | 3.18 | 0.56 | 15.6250 | - | 66,136 B |
48-
| ZStringBuilderUtf16 | 1,187.6 ns | 21.35 ns | 25.42 ns | 1,185.0 ns | 2.88 | 0.52 | 15.6250 | 0.0019 | 66,136 B |
43+
BenchmarkDotNet=v0.13.2, OS=macOS Monterey 12.6.1 (21G217) [Darwin 21.6.0]
44+
Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
45+
.NET SDK=7.0.100-rc.2.22477.23
46+
[Host] : .NET 6.0.10 (6.0.1022.47605), Arm64 RyuJIT AdvSIMD
47+
DefaultJob : .NET 6.0.10 (6.0.1022.47605), Arm64 RyuJIT AdvSIMD
48+
49+
50+
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio |
51+
|------------------------------- |-----------:|---------:|---------:|------:|--------:|--------:|----------:|------------:|
52+
| DotNetStringBuilder | 227.3 ns | 1.31 ns | 1.22 ns | 1.00 | 0.00 | 0.7114 | 1488 B | 1.00 |
53+
| ValueStringBuilder | 128.7 ns | 0.57 ns | 0.53 ns | 0.57 | 0.00 | 0.2677 | 560 B | 0.38 |
54+
| ValueStringBuilderPreAllocated | 113.9 ns | 0.67 ns | 0.60 ns | 0.50 | 0.00 | 0.2677 | 560 B | 0.38 |
55+
| ZStringBuilderUtf8 | 1,976.1 ns | 17.33 ns | 15.37 ns | 8.69 | 0.08 | 31.2500 | 66120 B | 44.44 |
56+
| ZStringBuilderUtf16 | 1,888.2 ns | 18.50 ns | 16.40 ns | 8.30 | 0.09 | 31.2500 | 66120 B | 44.44 |
57+
4958
```
5059

5160
For more comparison check the documentation.

src/LinkDotNet.StringBuilder/TypedSpanList.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Buffers;
21
using System.Runtime.CompilerServices;
32

43
namespace LinkDotNet.StringBuilder;
@@ -19,7 +18,7 @@ internal ref struct TypedSpanList<T>
1918
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2019
public TypedSpanList()
2120
{
22-
buffer = new T[8];
21+
buffer = GC.AllocateUninitializedArray<T>(8);
2322
count = 0;
2423
}
2524

@@ -43,9 +42,8 @@ private void Grow(int capacity = 0)
4342
{
4443
var currentSize = buffer.Length;
4544
var newSize = capacity > 0 ? capacity : currentSize * 2;
46-
var rented = ArrayPool<T>.Shared.Rent(newSize);
45+
var rented = GC.AllocateUninitializedArray<T>(newSize);
4746
buffer.CopyTo(rented);
4847
buffer = rented;
49-
ArrayPool<T>.Shared.Return(rented);
5048
}
5149
}

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ public int LastIndexOf(ReadOnlySpan<char> word, int startIndex)
244244
/// <summary>
245245
/// Disposes the instance and returns rented buffer from an array pool if needed.
246246
/// </summary>
247+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
247248
public void Dispose()
248249
{
249-
var toReturn = arrayFromPool;
250-
if (toReturn != null)
250+
if (arrayFromPool != null)
251251
{
252-
ArrayPool<char>.Shared.Return(toReturn);
252+
ArrayPool<char>.Shared.Return(arrayFromPool);
253253
}
254254
}
255255

tests/LinkDotNet.StringBuilder.Benchmarks/AppendBenchmark.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ public string ValueStringBuilder()
2929
return builder.ToString();
3030
}
3131

32+
[Benchmark]
33+
public string ValueStringBuilderPreAllocated()
34+
{
35+
using var builder = new ValueStringBuilder(stackalloc char[256]);
36+
builder.AppendLine("That is the first line of our benchmark.");
37+
builder.AppendLine("We can multiple stuff in here if want.");
38+
builder.AppendLine("We can multiple stuff in here if want.");
39+
builder.AppendLine("The idea is that we can resize the internal structure from time to time.");
40+
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
41+
return builder.ToString();
42+
}
43+
3244
[Benchmark]
3345
public string ZStringBuilderUtf8()
3446
{

tests/LinkDotNet.StringBuilder.Benchmarks/AppendValueTypesBenchmark.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ namespace LinkDotNet.StringBuilder.Benchmarks;
66
[MemoryDiagnoser]
77
public class AppendValueTypes
88
{
9+
private const int NumberOfIterations = 25;
10+
911
[Benchmark]
1012
public string DotNetStringBuilder()
1113
{
1214
var builder = new System.Text.StringBuilder();
1315

14-
for (var i = 0; i < 25; i++)
16+
for (var i = 0; i < NumberOfIterations; i++)
1517
{
1618
builder.Append(true);
1719
builder.Append(int.MaxValue);
@@ -29,7 +31,7 @@ public string ValueStringBuilder()
2931
{
3032
using var builder = new ValueStringBuilder();
3133

32-
for (var i = 0; i < 25; i++)
34+
for (var i = 0; i < NumberOfIterations; i++)
3335
{
3436
builder.Append(true);
3537
builder.Append(int.MaxValue);
@@ -47,7 +49,7 @@ public string ZStringBuilder()
4749
{
4850
var builder = ZString.CreateStringBuilder();
4951

50-
for (var i = 0; i < 25; i++)
52+
for (var i = 0; i < NumberOfIterations; i++)
5153
{
5254
builder.Append(true);
5355
builder.Append(int.MaxValue);

0 commit comments

Comments
 (0)