DynamicMethod is much slower than compiled IL function

后端 未结 2 1172
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 23:56

I wrote a simple object copier that copies public properties. I can\'t figure out why the Dynamic method is a lot slower than the c# version.

Durations

C# me

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 00:26

    This problem was introduced by changes made in .NET Framework 4.0. I found a solution posted by user "Alan-N" on CodeProject.

    The big slowdown in execution time is caused when the DynamicMethod gets associated with a "system-provided, fully trusted, security-transparent assembly," which happens if you use the DynamicMethod(string, Type, Type[], bool) constructor. It appears that .NET 4 is doing more security checks than the previous versions although I have no insight into, or explanation for, what is actually going on.

    Associating the DynamicMethod with a Type (by using the DynamicMethod(string, Type, Type[], Type, bool) constructor instead; notice the additional Type-valued parameter, 'owner') completely removes the speed penalty.

    There are some notes on MSDN which may be relevant (if only I could understand them!):

    • DynamicMethod Constructor (String, Type, Type[], Boolean)
    • Security Issues in Reflection Emit

提交回复
热议问题