[CSHARP-1972] Performance of method ToHexString() Created: 29/Apr/17  Updated: 06/Feb/24

Status: Backlog
Project: C# Driver
Component/s: Performance
Affects Version/s: 2.4.3
Fix Version/s: 2.4.4

Type: Improvement Priority: Major - P3
Reporter: Viktor Svyatokha Assignee: Damien Guard
Resolution: Unresolved Votes: 0
Labels: devexp-product, performance
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

From several profiling sessions of my application I've noticed that method ToHexString() is used very often. DotTrace has also marked it as one of hot methods during profiling. I've found a solution which has about 40x speedup in compare with current implementation.
Here's attached benchmarking info and code:

BenchmarkDotNet=v0.10.5, OS=Windows 10.0.15063
Processor=Intel Core i5-2400 CPU 3.10GHz (Sandy Bridge), ProcessorCount=4
Frequency=3037251 Hz, Resolution=329.2451 ns, Timer=TSC
  [Host]     : Clr 4.0.30319.42000, 32bit LegacyJIT-v4.7.2046.0
  DefaultJob : Clr 4.0.30319.42000, 32bit LegacyJIT-v4.7.2046.0
 
 
                Method |      Mean |     Error |    StdDev |
---------------------- |----------:|----------:|----------:|
 MongoDbImplementation | 97.146 us | 1.1451 us | 1.0711 us |
             HandCoded |  1.932 us | 0.0216 us | 0.0202 us |

Benchmark code: https://gist.github.com/ahydrax/477fedfe5a44bb49ff862b554694a4f1



 Comments   
Comment by Damien Guard [ 06/Feb/24 ]

Hi Viktor. We're going through some older PRs that were overlooked for various reasons and taking another look. I re-ran the benchmarks on .NET 7 and also included using the new `Convert.ToHex` function available in .NET 5.0 and later.
 
| Method                | Mean        | Error       | StdDev    |
|---------------------- |-----------:|----------:|---------:|
| MongoDbImplementation | 12,485.3 ns | 2,114.60 ns | 115.91 ns |
| HandCoded             |    718.0 ns |   266.98 ns |  14.63 ns |
| DotNetBuiltIn         |    130.3 ns |    73.88 ns |   4.05 ns |

The new Convert.ToHex uses SIMD instructions where appropriate and has very highly optimized code offering further performance gains. I'm going to recommend we switch to that as part of the C# Driver 3.0 work when we drop support for older targets.

Comment by Daniel Hegener [ 20/Feb/19 ]

Additional PR https://github.com/mongodb/mongo-csharp-driver/pull/285

Comment by Githook User [ 04/May/17 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1972: Optimize BsonUtils.ToHexString and ObjectId.ToString.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2894c3df1cf155a793aa8641cbc6cdb6cf308a7a

Comment by Viktor Svyatokha [ 29/Apr/17 ]

http://joxi.ru/gmvgWe9HLwRzV2 detailed profiling info for this method
http://joxi.ru/DmBDPE0fwvYQ3A stack trace

I can't provide full stack trace because of NDA but I'll try to provide as much info as possible

Comment by Craig Wilson [ 29/Apr/17 ]

Interesting. Thanks for sending. I'm not surprised that ours is slow. We didn't spend time optimizing it. I am surprised that it's showing up in the hot path, which is why we didn't spend any time optimizing it.

Do you know that stacktrace that was ultimately causing it to get hit so often?

Generated at Wed Feb 07 21:41:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.