3

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?

Rob
  • 4,327
  • 6
  • 29
  • 55
  • It is a jitter helper function, it automatically generates a call to it to verify sandboxing restrictions. Exact details are pretty murky, you can dig through the coreclr source code to find details. The most important detail of optimizing code is to know when you're done. If this function dominates then you've probably overshot done. – Hans Passant Apr 01 '15 at 18:00
  • @Hans Is it the "thing" that can be fixed by `[assembly: AllowPartiallyTrustedCallers] [assembly: SecurityTransparent] [assembly: SecurityRules(SecurityRuleSet.Level2, SkipVerificationInFullTrust = true)]`? – xanatos Apr 01 '15 at 18:40
  • @xanatos - the comments after [this question](http://stackoverflow.com/q/24802222/738851) include these attributes, so it might be worth a try. – Rob Apr 02 '15 at 08:43
  • 1
    I also found [this answer](http://stackoverflow.com/a/5642774/738851) which, although it specifies a different clr method, seems very similar to my issue. – Rob Apr 02 '15 at 08:46
  • @Hans The code in question is performance critical in a soft real time system. Profiling suggests this new call costs 10ms, or 6%. Not huge, but not trivial. The concern is that it was introduced with the Expression Trees - I doubt we would have looked at it had it always been there. – Rob Apr 02 '15 at 08:52
  • Hmm, that's not possible. Maybe total accumulated time is 10 msec from many thousands calls. Not a problem for a soft realtime system. – Hans Passant Apr 02 '15 at 10:33
  • @Hans - yes - we compile expressions and call them thousands of times. – Rob Apr 02 '15 at 10:51
  • Could you include example code that demonstrates the issue? – svick May 10 '15 at 23:17

2 Answers2

0

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.

kerem
  • 2,699
  • 1
  • 32
  • 37
0

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?

lvilnis
  • 518
  • 3
  • 13