Negotiate reflection performance improvements
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
Feedback from @mconnew:
I was looking over your reflected abstraction of the negotiate code ... and noticed a significant performance problem in the code. There is lots of code which looks similar to this:
public bool IsCompleted
{
get => (bool)_isCompleted.Invoke(_instance, Array.Empty());
}
The problem with calling the Invoke method on an MethodInfo is it does a whole bunch of runtime type checking to make sure the number and type of arguments is correct. There’s also a boxing and unboxing as well as a cast type check on the return value. You can use MethodInfo.CreateDelegate to create a delegate you can straight up call. Because it only does the type checking of the delegate type to the method info when you call CreateDelegate, it’s a lot cheaper to call. The generated delegate is strongly typed so the CLR just depends on the strong typing to ensure all the parameters etc are correct.
---
If we get a proper API for this in 6.0 then this code can be removed (https://github.com/dotnet/runtime/issues/29270). If not we may consider making some improvements.
Note it's not clear how these costs reflect on RPS. Windows Auth is already a very in-efficient process that often involves multiple request.
Contributor guide
Assessment
This issue has not been assessed yet.