简体   繁体   English

C# 编译器或 JIT 在什么级别优化应用程序代码?

[英]At what level C# compiler or JIT optimize the application code?

I want to know this info to reduce my code size so I will not waste my time optimize things that will be done by compiler or JIT.我想知道这些信息以减少我的代码大小,这样我就不会浪费我的时间来优化将由编译器或 JIT 完成的事情。

for example:例如:

if we assume the compiler inline the call to the get function of a property so I do not have to save the return value in a local variable to avoid function call.如果我们假设编译器内联对 get function 属性的调用,那么我不必将返回值保存在局部变量中以避免 function 调用。

I want to recommend a good reference that describes what is going on?我想推荐一个很好的参考来描述发生了什么?

You may want to take a look at these articles:你可能想看看这些文章:

JIT Optimizations - (Sasha Goldshtein - CodeProject) JIT 优化 - (Sasha Goldshtein - CodeProject)
Jit Optimizations: Inlining I (David Notario) Jit 优化:内联 I (David Notario)
Jit Optimizations: Inlining II (David Notario) Jit 优化:内联 II (David Notario)

To be honest you shouldn't be worrying too much about this level of micro-detail.老实说,您不应该过多担心这种级别的微观细节。 Let the compiler/JIT'er worry about this for you, it's better at it than you are in almost all cases.让编译器/JIT'er 为您担心这个问题,在几乎所有情况下它都比您做得更好。 Don't get hung up on Premature Optimisation .不要挂断过早的优化 Focus on getting your code working, then worry about optimisations later on if (a) it doesn't run fast enough, (b) you have 'size' issues.专注于让您的代码正常工作,然后在 (a) 它运行速度不够快,(b) 您有“大小”问题时担心以后的优化。

If you are worried about performance, run a profiler.如果您担心性能,请运行分析器。 Then change code.然后改代码。 Chances are that you will never in a million years guess 100% correctly where the time is going.很有可能,在一百万年后,您永远不会 100% 正确地猜出时间的去向。 You could be changing the 0.02% timing, and leaving the method that contributes 62% of the burden.您可能会更改 0.02% 的时间,并留下造成 62% 负担的方法。 You could also be making it worse.你也可能使情况变得更糟。 Without a profiler and evidence, you are blind.没有分析器和证据,你就是盲人。


You can't assume that the JIT will inline a property getter.您不能假设JIT 将内联属性获取器。 There are many reasons it may or may not do so;它可能会或可能不会这样做的原因有很多; size of the method body, virtual, value vs reference type, architecture, debugger attached, etc.方法体的大小、虚拟、值与引用类型、架构、附加的调试器等。

"Hoisting" still has a place, and can still achieve savings if the code is called repeatedly in a tight loop; “吊装”还是有一席之地的,如果代码在一个紧密的循环中重复调用,仍然可以实现节省; for example:例如:

var count = list.Count;
for(int i = 0 ; i < count ; i++) {...}

(forget the for vs foreach debate fr the above - this is an orthogonal discussion). (忘记上面的for vs foreach辩论 - 这是一个正交讨论)。 In the above, the "hoist" will help performance.在上面,“提升”将有助于性能。 But just to be really confusing - with arrays, it is the opposite, and it is more efficient to not hoist it:但只是为了真正令人困惑 - 对于 arrays,情况正好相反,提升它更有效:

for(int i = 0 ; i < arr.Length ; i++) {...}

The JIT recognises this and removes the bounds check (as arrays are fixed size). JIT 认识到这一点并删除了边界检查(因为 arrays 是固定大小的)。

This looks like a kind of micro optimization which you shouldn't be looking at.这看起来像是一种你不应该关注的微优化。 If I'm not mistaken it depends on the architecture and version of the CLR which kind of optimization is applied.如果我没记错的话,这取决于应用哪种优化的 CLR 的体系结构和版本。

If your method is called that much, and you really want it to inline, you can inline it yourself at the cost of spaghetti code.如果你的方法被调用了这么多,并且你真的希望它内联,你可以自己内联它,代价是意大利面条式的代码。

I would recommend analyzing your algorithm, to inline a method will not save magnitudes of speed, while a better algorithm can make your running time decrease from hours to seconds.我建议分析您的算法,内联方法不会节省速度,而更好的算法可以使您的运行时间从几小时减少到几秒钟。

The most powerful optimization performed by a JIT is typically inlining. JIT 执行的最强大的优化通常是内联。 A JIT can even inline hundreds of functions deep (I heard this figure for JikesRVM). JIT 甚至可以内联数百个函数(我听说 JikesRVM 的这个数字)。 They will even inline things that aren't always possible to inline, and back it out later if they need to (called dynamic deoptimization).他们甚至会内联并不总是可以内联的东西,并在需要时将其退出(称为动态去优化)。

A nice overview is http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html .一个很好的概述是http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html

For your specific question, I would say probably, if the function call in question is hot .对于您的具体问题,我可能会说,如果有问题的 function 调用很热

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

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