简体   繁体   English

在Compact Framework 3.5中使用lambda表达式加载类在首次加载时很慢

[英]Loading class with lambda expressions in Compact Framework 3.5 is slow on first load

While optimizing one of the larger forms in a compact framework 3.5 applications we noticed a significant performance hit while loading forms that contain lambda expressions. 在紧凑框架3.5应用程序中优化其中一个较大的表单时,我们注意到在加载包含lambda表达式的表单时会出现显着的性能损失。 This is only noticeable when running the application on the device in release mode. 只有在发布模式下在设备上运行应用程序时才会注意到这一点。

When the form contains code such as: 当表单包含如下代码时:

foreach (MyObject in objects.OrderBy(x => x.id))

The base constructor of the class takes severable seconds longer to execute (depending on the hardware), than the equivalent: 该类的基本构造函数执行(取决于硬件)的时间比可能的等效时间长得多:

foreach (MyObject in objects.OrderBy(FunctionPointerInsteadOfLambda)) 

...

private string FunctionPointerInsteadOfLambda(MyObject obJ) {
    return obj.Id;
}

My understanding is that the lambda compiles to an anonymous method, and this will add an entry to the method slot table for the class. 我的理解是lambda编译成一个匿名方法,这将为该类的方法槽表添加一个条目。 However, there is such a big difference in load times when including the lambda. 然而,当包括λ时,加载时间存在很大差异。 Simply having it there influences the load time, even if it is not called. 只要有它就会影响加载时间,即使它没有被调用。 And this only occurs on when creating an instance of the class the first time. 这只发生在第一次创建类的实例时。

In the second example, the slowness is deferred until the method is actually called. 在第二个示例中,延迟缓慢直到实际调用该方法。

I am struggling to find specific details on the compact framework's CLR that would shed any light on this issue. 我正在努力寻找关于紧凑框架的CLR的具体细节,这将有助于解决这个问题。

If the performance delay is consistent (ie happens at the same point on the same hardware) then I would attribute the one-time performance delay to JIT compilation. 如果性能延迟是一致的(即在同一硬件上的同一点发生),那么我会将一次性能延迟归因于JIT编译。 Though not necessarily convenient, a one-time delay is typical of JIT compilation. 虽然不一定方便,但一次性延迟是JIT编译的典型特征。

Since you mention that it only happens in release mode, another consideration may be that the compiler is performing some optimization, such as inlining, that is triggering the delay behavior. 由于您提到它仅在发布模式下发生,因此另一个考虑因素可能是编译器正在执行一些优化,例如内联,这会触发延迟行为。 One way to test this theory would be to create a release build with optimizations disabled, then see if the problem is reproducible. 测试该理论的一种方法是创建一个禁用优化的发布版本,然后查看问题是否可重现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM