File tree 2 files changed +27
-8
lines changed
src/LinkDotNet.StringBuilder
2 files changed +27
-8
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
6
6
7
7
## [ Unreleased]
8
8
9
+ ### Changed
10
+ - ` Append(bool) ` is now 33% faster
11
+
9
12
## [ 1.21.0] - 2024-09-20
10
13
11
14
### Added
Original file line number Diff line number Diff line change @@ -10,22 +10,38 @@ public ref partial struct ValueStringBuilder
10
10
/// </summary>
11
11
/// <param name="value">Bool value to add.</param>
12
12
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
13
- public void Append ( bool value )
13
+ public unsafe void Append ( bool value )
14
14
{
15
- // 5 is the length of the string "False"
16
- // So we can check if we have enough space in the buffer
17
- var newSize = bufferPosition + 5 ;
15
+ const int trueLength = 4 ;
16
+ const int falseLength = 5 ;
17
+
18
+ var newSize = bufferPosition + falseLength ;
19
+
18
20
if ( newSize > buffer . Length )
19
21
{
20
22
Grow ( newSize ) ;
21
23
}
22
24
23
- if ( ! value . TryFormat ( buffer [ bufferPosition .. ] , out var charsWritten ) )
25
+ fixed ( char * dest = & buffer [ bufferPosition ] )
24
26
{
25
- throw new InvalidOperationException ( $ "Could not add { value } to the builder.") ;
27
+ if ( value )
28
+ {
29
+ * ( dest + 0 ) = 'T';
30
+ * ( dest + 1 ) = 'r';
31
+ * ( dest + 2 ) = 'u';
32
+ * ( dest + 3 ) = 'e';
33
+ bufferPosition += trueLength ;
34
+ }
35
+ else
36
+ {
37
+ * ( dest + 0 ) = 'F';
38
+ * ( dest + 1 ) = 'a';
39
+ * ( dest + 2 ) = 'l';
40
+ * ( dest + 3 ) = 's';
41
+ * ( dest + 4 ) = 'e';
42
+ bufferPosition += falseLength ;
43
+ }
26
44
}
27
-
28
- bufferPosition += charsWritten ;
29
45
}
30
46
31
47
/// <summary>
You can’t perform that action at this time.
0 commit comments