Improve perf of hash one-shots on Win10#40510
Conversation
....Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Windows.cs
Outdated
Show resolved
Hide resolved
....Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Windows.cs
Show resolved
Hide resolved
|
@vcsjones I addressed your second feedback item by querying the hash size right after we create the algorithm, then shoving that information into the algorithm handle cache. It means we only take the lookup hit once. For the one-shot algos, I think it's still appropriate to hard-code the expected digest length since we need to maintain the friendly-name -> pseudo-alg handle mapping ourselves anyway. |
|
|
||
| if (!s_useCompatOneShot) | ||
| NTSTATUS ntStatus; | ||
| fixed (byte* pSrc = &MemoryMarshal.GetReference(source)) |
There was a problem hiding this comment.
A question: why the change to use MemoryMarshal.GetReference?
There was a problem hiding this comment.
Habit, muscle memory, whatever you want to call it. :)
There was a problem hiding this comment.
The way via GetPinnableReference has an extra length check, that can be avoided here, as we know the span won't be empty. So the codegen is better (maybe the reason for Levi's muscle memory to kick in 😉).
bartonjs
left a comment
There was a problem hiding this comment.
Seems like a lot of work and complexity for a relatively trivial gain, but since it's written... OK.
This same test (or a very similar looking one) has failed on at least 3 reruns for the last commit of the PR. Looks like it only happens on Win7, and I haven't seen it on other PRs (but maybe I'm unobservant). I don't see how it could necessarily be related, but the consistency bothers me. |
|
Odd, I thought that every test rerun tried stacking the PR diff on top of master freshly... but I agree with your assessment that this is a known/fixed issue. 🤷 |
Uses bcrypt pseudo-handles (see https://docs.microsoft.com/windows/win32/seccng/cng-algorithm-pseudo-handles) with the
BCryptHashroutine to get a modest perf improvement in the one-shot hash algortihms when running on Windows 10. For downlevel Windows platforms, continues to use the existing "catchEntryPointNotFoundand fall back toBCryptHashData" logic.The Win10-specific routine also has a safety net such that if any of the invariants are violated (unknown algorithm or destination too short), it falls back to the downlevel code paths for additional error handling.
No change on other OSes.