简体   繁体   中英

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. 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. 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.

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. Though not necessarily convenient, a one-time delay is typical of JIT compilation.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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