Optimizing .NET: Day 4
BitConverter.ToString()#
byte[] bytes = [0xFF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x00];
string hex = BitConverter.ToString(bytes, 1, 9);
// "01-23-45-67-89-AB-CD-EF-00"
The stock formatter processes one byte at a time. The patch formats eight bytes at a time and uses AVX2 when available to combine two 128-bit UTF-16 stores into one 256-bit store. That cuts the total from three stores per block to two.
The first byte is formatted separately because it has no leading dash. Each block after it uses SSSE3 shuffles to place 16 uppercase hex digits and eight dashes.
When fewer than eight bytes remain, the loop uses the last eight bytes of the slice. The read and write stay within bounds, and bytes processed twice produce the same characters at the same output positions. Slices shorter than nine bytes and processors without SSSE3 use the scalar formatter.
Show the vector loop
int last = source.Length - 8;
for (int i = 0; ; i = Math.Min(i + 8, last))
{
Vector128<byte> bytes = Vector128.CreateScalar(Unsafe.ReadUnaligned<ulong>(ref Unsafe.Add(ref input, i))).AsByte();
(Vector128<byte> digits, _) = HexConverter.AsciiToHexVector128(bytes, hexMap);
Vector128<byte> head = Ssse3.Shuffle(digits, headIndices) | headSeparators;
Vector128<byte> tail = Ssse3.Shuffle(digits, tailIndices) | tailSeparators;
nuint offset = (nuint)i * 3;
if (Avx2.IsSupported)
{
Avx2.ConvertToVector256Int16(head).AsUInt16().StoreUnsafe(ref output, offset);
}
else
{
Vector128.WidenLower(head).StoreUnsafe(ref output, offset);
Vector128.WidenUpper(head).StoreUnsafe(ref output, offset + 8);
}
Vector128.WidenLower(tail).StoreUnsafe(ref output, offset + 16);
if (i == last)
{
return;
}
}
Stock and patched .NET 11 each passed all 31 BitConverter tests with AVX2 enabled, with the SSSE3 fallback, and with hardware intrinsics disabled.
A separate check matched stock output for 74,016 slices in each configuration.
Benchmark#
Hex1024Bytes formats a 1 KiB slice at offset five into a new 3,071-character string.
| Metric | Before | After | Change |
|---|---|---|---|
| Mean latency (ns/op) | 1,374.74 | 345.10 | 74.9% lower |
| Managed allocation (B/op) | 6,168 | 6,168 | 0% |
| Throughput (MB/s) | 745 | 2,967 | 298.4% higher |
Results
| Method | Job | Mean | Error | StdDev | Ratio | Allocated | Alloc Ratio |
|---|---|---|---|---|---|---|---|
| Hex9Bytes | stock | 14.324 ns | 0.5299 ns | 0.3831 ns | 1.00 | 80 B | 1.00 |
| Hex9Bytes | patched | 7.833 ns | 0.5097 ns | 0.3980 ns | 0.55 | 80 B | 1.00 |
| Hex16Bytes | stock | 23.170 ns | 1.2409 ns | 0.9688 ns | 1.00 | 120 B | 1.00 |
| Hex16Bytes | patched | 12.245 ns | 0.9012 ns | 0.7036 ns | 0.53 | 120 B | 1.00 |
| Hex17Bytes | stock | 24.623 ns | 1.1480 ns | 0.8963 ns | 1.00 | 128 B | 1.00 |
| Hex17Bytes | patched | 11.306 ns | 1.1416 ns | 0.8913 ns | 0.46 | 128 B | 1.00 |
| Hex32Bytes | stock | 47.426 ns | 2.5894 ns | 2.0216 ns | 1.00 | 216 B | 1.00 |
| Hex32Bytes | patched | 15.729 ns | 1.1969 ns | 0.9344 ns | 0.33 | 216 B | 1.00 |
| Hex65Bytes | stock | 86.361 ns | 4.0941 ns | 3.1964 ns | 1.00 | 416 B | 1.00 |
| Hex65Bytes | patched | 25.476 ns | 2.4318 ns | 1.7583 ns | 0.30 | 416 B | 1.00 |
| Hex1024Bytes | stock | 1,374.737 ns | 65.9150 ns | 47.6609 ns | 1.00 | 6168 B | 1.00 |
| Hex1024Bytes | patched | 345.102 ns | 41.1063 ns | 32.0931 ns | 0.25 | 6168 B | 1.00 |
| Hex4096Bytes | stock | 5,641.837 ns | 416.3978 ns | 325.0960 ns | 1.00 | 24600 B | 1.00 |
| Hex4096Bytes | patched | 1,140.104 ns | 66.7515 ns | 52.1152 ns | 0.20 | 24600 B | 1.00 |
| Control1Byte | stock | 4.890 ns | 0.2820 ns | 0.2039 ns | 1.00 | 32 B | 1.00 |
| Control1Byte | patched | 5.460 ns | 0.2800 ns | 0.2186 ns | 1.12 | 32 B | 1.00 |
| Control6Bytes | stock | 11.141 ns | 0.7066 ns | 0.5517 ns | 1.00 | 56 B | 1.00 |
| Control6Bytes | patched | 10.671 ns | 0.5274 ns | 0.4117 ns | 0.96 | 56 B | 1.00 |
Control1Byte and Control6Bytes exercise the scalar path. Both changed direction in a patched-first repeat.
Reproduce#
With the .NET 11 SDK on x64 Linux or x64 Windows, run this from the extracted benchmark directory.
dotnet run -c Release
The benchmark loads bundled stock and patched libraries built from dotnet/runtime commit deae2e32bf0e018e09d64597b7c59b62b73bb3ce. The jobs differ only in System.Private.CoreLib.dll.
Download the patch with tests | Download the Linux benchmark | Download the Windows benchmark