File tree 3 files changed +13
-25
lines changed
src/LinkDotNet.StringBuilder
tests/LinkDotNet.StringBuilder.Benchmarks
3 files changed +13
-25
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
8
8
9
9
### Changed
10
10
- ` 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
12
12
## [ 1.18.5] - 2023-10-19
13
13
14
14
### Changed
Original file line number Diff line number Diff line change @@ -313,7 +313,18 @@ private void Grow(int capacity = 0)
313
313
}
314
314
315
315
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
+
317
328
var oldBufferFromPool = arrayFromPool ;
318
329
buffer = arrayFromPool = rented ;
319
330
Original file line number Diff line number Diff line change @@ -5,17 +5,6 @@ namespace LinkDotNet.StringBuilder.Benchmarks;
5
5
[ MemoryDiagnoser ]
6
6
public class AppendBenchmarks
7
7
{
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
-
19
8
[ Benchmark ]
20
9
public string ValueStringBuilder ( )
21
10
{
@@ -27,16 +16,4 @@ public string ValueStringBuilder()
27
16
builder . AppendLine ( "We can also add other Append method if we want. But we keep it easy for now." ) ;
28
17
return builder . ToString ( ) ;
29
18
}
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
- }
42
19
}
You can’t perform that action at this time.
0 commit comments