-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NameValueCollectionExtensions Performance Optimisation #4951
Comments
Thanks for opening @stevejgordon. In general, performance optimizations are most definitely PRs that we welcome and encourage! If I understand correctly, to be able to benefit from this optimization for those folks running on a platform that supports .NET Standard 2.1, we would need to use a I am open to accepting such a PR, and publishing a .NET Standard 2.1 package in addition. @Mpdreamz, what are your thoughts? |
You're welcome, @russcam. Forgive me, I'd assumed there was a 2.1 package as I saw it listed in the TargetFrameworks for the project and some use of the FYI, my original benchmark was a little skewed as I'd accidentally targeted .NET 5 so was getting some perf improvements from there too. I've updated the original issue with the app targeting 2.1 so that the benchmarks are more accurate. NS2.1 what the first to include I'd advocate for a 2.1 package which includes the latest 2.1 optimisations for consumers on .NET Core 3.x and higher. |
A ~40% speed and allocation improvement is always welcomed with open arms 👍
Agreed, I would even open it up to
@adamsitnik squashed any qualms I had about
Both of these libraries have explicit The Long story short, nothing prevents us from adding this reference AFAICT. |
Would you be open to taking PR(s) for performance optimisations, specifically for cases where the NEST library is consumed within NetStandard 2.1 and NetCoreApp 2.1 apps?
I was exploring
NameValueCollectionExtensions
and one simple example is theToQueryString
method. By applyingSpan<T>
optimisation, it's possible to reduce the allocations fairly significantly. Given this is called every time aRequestData
instance is constructed it may have worthwhile affect for consumers.I've created a benchmark and an initial optimised implementation with the following results:
This reduced allocated bytes by 39.3% and operates 42% quicker with my test data of 5 random keys and values.
A note regarding the execution time. My prototype takes a shortcut and uses a rented char[] from the
ArrayPool
with a minimum length of 512 chars. If there is a realistic maximum length expected for the qs, then hard-coding it rather than calculating the length each time is potentially worthwhile. With the length calculation the execution time is 4.227 us, so only slightly faster than current performance.With my test data (where one key has a char that requires escaping), by applying custom
Uri
escaping, it may be possible to reduce a further 48 bytes. If it is common for keys/values to require escaping then the allocation reduction could be more significant.If you feel such allocation reductions would have value to consumers and you'd be open to such a PR, I'd happily submit this?
Example of prototype code...
The text was updated successfully, but these errors were encountered: