Compiled Expression Tree slow due to JIT_MethodAcc

2019-04-10 19:54发布

问题:

We're using compiled Expression Trees to generate code dynamically; some information only available to us at runtime enables us to (in theory) write simpler, faster code. We do get a performance boost in many cases.

However, in some cases we get a performance hit. In such cases, the Visual Studio Profiler shows that the difference in performance is due to this method (which doesn't show up at all in statically compiled code)

JIT_MethodAccessCheck

What does this method do? (Google doesn't have much to say about it). Can I optimize it away somehow?

回答1:

JIT_MethodAccessCheck method performs security checks such as SecurityTransparent, APTCA and class access checks as mentioned by @xanatos.

Class access checks include SecurityCritical, SecuritySafeCritical, attached profiler bypass and LinkDemand. More details can be found at coreclr jithelpers.cpp.

Since coreclr is compatible with CLR, we can safely assume that the checks are in the same manner in both of them.



回答2:

Use the code snippet in this answer to compile expression trees instead, it gets rid of the checks across method boundaries:

Why is JIT_MethodAccessAllowedBySecurity taking so much time?