Skip to content

Commit 28cf4a3

Browse files
committed
Use BlockCopy
1 parent b9d948d commit 28cf4a3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9+
### Changed
10+
11+
- Optimized `Append(scoped ReadOnlySpan<char>)` to be roughly 5% faster.
12+
913
## [1.18.3] - 2023-09-22
1014

1115
### Changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.Append.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
23

34
namespace LinkDotNet.StringBuilder;
45

@@ -51,7 +52,13 @@ public void Append(scoped ReadOnlySpan<char> str)
5152
Grow(newSize);
5253
}
5354

54-
str.CopyTo(buffer[bufferPosition..]);
55+
ref var strRef = ref MemoryMarshal.GetReference(str);
56+
ref var bufferRef = ref MemoryMarshal.GetReference(buffer[bufferPosition..]);
57+
Unsafe.CopyBlock(
58+
ref Unsafe.As<char, byte>(ref bufferRef),
59+
ref Unsafe.As<char, byte>(ref strRef),
60+
(uint)(str.Length * sizeof(char)));
61+
5562
bufferPosition += str.Length;
5663
}
5764

0 commit comments

Comments
 (0)