akamsteeg / AtleX.HaveIBeenPwned

A fully async .NET Standard client library for the API of HaveIBeenPwned.com
https://www.nuget.org/packages/AtleX.HaveIBeenPwned/
MIT License
5 stars 0 forks source link

Use newer APIs to simplify and possibly speed up generation of the KAnonimity hash parts #96

Closed akamsteeg closed 5 months ago

akamsteeg commented 5 months ago

Currently, a StringBuilder with a format is used to build the ValueTuple<T, T> with both parts of the KAnonimity hash:

https://github.com/akamsteeg/AtleX.HaveIBeenPwned/blob/b06cb9551bcb364239c4c9bef23a304948817f80/src/AtleX.HaveIBeenPwned/Helpers/KAnonimityHelper.cs#L43C3-L61C4

Since .NET 5, a Convert.ToHexString(byte[]) is available that is possibly more optimized than the StringBuilder implementation. We should test it and implement it's usage when it's advantageous.

akamsteeg commented 5 months ago

Well, that's a nice optimization.

Before:

Method Toolchain Mean Ratio Allocated Alloc Ratio
GetKAnonimityPartsForPassword .NET 6.0 868.9 ns 1.38 840 B 1.00
GetKAnonimityPartsForPassword .NET 8.0 631.8 ns 1.00 840 B 1.00
GetKAnonimityPartsForPassword .NET Framework 4.8.1 2,979.5 ns 4.71 4710 B 5.61

After

Method Toolchain Mean Ratio Allocated Alloc Ratio
GetKAnonimityPartsForPassword .NET 6.0 238.5 ns 1.01 312 B 1.00
GetKAnonimityPartsForPassword .NET 8.0 235.3 ns 1.00 312 B 1.00
GetKAnonimityPartsForPassword .NET Framework 4.8.1 2,936.3 ns 12.48 4710 B 15.10

About 3 to 4 times faster on .NET 6 with less than half the memory usage. 🎉