mongo-csharp-driver
mongo-csharp-driver copied to clipboard
CSHARP-1972: Further performance optimization of the Hex conversion methods in BsonUtils
May I suggest another performance optimization around the hex string to byte array (and back) conversion methods in BsonUtils
.
This change introduces some precalculated jump tables that cost a tiny little bit of memory but give an dramatic boost in performance.
I have put together a demo solution using Benchmark.NET that proves the correctness and shows the performance improvement here: https://github.com/dnickless/BsonUtilsHexConversionPerformance
While I was originally putting together that code it had not been optimized yet. So after comparing the optimized version that does exist right now to my suggestion here, the increase in performance is not quite as "dramatic" anymore as I wrote in the original comment. However, it's probably still big enough to justify a PR.
Here are the CLR results:
BenchmarkDotNet=v0.11.1, OS=Windows 7 SP1 (6.1.7601.0)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328369 Hz, Resolution=300.4475 ns, Timer=TSC
[Host] : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3133.0
Job-QANSFC : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit LegacyJIT/clrjit-v4.7.3133.0;compatjit-v4.7.3133.0
Job-EEIESK : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3133.0
Job-DZQHCN : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3133.0
Runtime=Clr
Method | Jit | Platform | Mean | Error | StdDev | Median | Rank |
---|---|---|---|---|---|---|---|
New | LegacyJit | X64 | 916.9 ns | 18.339 ns | 41.393 ns | 906.2 ns | 1 |
Old | LegacyJit | X64 | 1,456.7 ns | 58.015 ns | 171.060 ns | 1,401.5 ns | 4 |
New | LegacyJit | X86 | 1,419.3 ns | 43.761 ns | 122.710 ns | 1,390.9 ns | 4 |
Old | LegacyJit | X86 | 1,729.0 ns | 30.519 ns | 27.055 ns | 1,716.3 ns | 5 |
New | RyuJit | X64 | 1,037.5 ns | 5.153 ns | 4.820 ns | 1,036.8 ns | 2 |
Old | RyuJit | X64 | 1,291.5 ns | 4.688 ns | 4.156 ns | 1,291.1 ns | 3 |
And this is on .NET Core 2.1
BenchmarkDotNet=v0.11.1, OS=Windows 7 SP1 (6.1.7601.0)
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
Frequency=3328369 Hz, Resolution=300.4475 ns, Timer=TSC
.NET Core SDK=2.1.402
[Host] : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT
Job-BYAUMP : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT
Runtime=Core Toolchain=.NET Core 2.1
Method | Mean | Error | StdDev | Median | Rank |
---|---|---|---|---|---|
New | 1.157 us | 0.0230 us | 0.0384 us | 1.136 us | 1 |
Old | 1.318 us | 0.0110 us | 0.0103 us | 1.316 us | 2 |
Thanks for the PR and sorry it was not looked at in a timely manner.
We will be moving to .NET 5 in an upcoming release and will use the Convert.ToHex methods that offer further performance gains.