简体   繁体   English

什么是/ optimize C#编译器密钥用于?

[英]What is /optimize C# compiler key intended for?

Is there a full list of optimizations done by the /optimize C# compiler key available anywhere? 是否有/ optimize C#编译器密钥在任何地方都可以完成的完整优化列表?

EDIT: Why is it disabled by default? 编辑: 为什么默认禁用? Is it worth using in a real-world app? 是否值得在真实世界的应用程序中使用? -- it is disabled by default only in Debug configuration and Enabled in Release. - 默认情况下仅在“调试”配置中禁用,在“释放”中启用。

Scott Hanselman has a blog post that shows a few examples of what /optimize (which is enabled in Release Builds) does. Scott Hanselman有一篇博客文章 ,其中展示了什么/优化(在发布版本中启用)的一些示例。

As a summary: /optimize does many things with no exact number or definition given, but one of the more visible are method inlining (If you have a Method A() which calls B() which calls C() which calls D(), the compiler may "skip" B and C and go from A to D directly), which may cause a "weird" callstack in the Release build. 作为总结:/ optimize做了许多没有给出确切数字或定义的东西,但其中一个更明显的是方法内联(如果你有一个方法A()调用B()调用C()调用D() ,编译器可以“跳过”B和C并直接从A转到D),这可能会在Release版本中导致“怪异”的callstack。

It is disabled by default for debug builds. 默认情况下,它对调试版本禁用。 For Release builds it is enabled. 对于Release版本,它已启用。

It is definitely worth enabling this switch as the compiler makes lots of tweaks and optimizations depending on the kind of code you have. 绝对值得启用此开关,因为编译器会根据您拥有的代码类型进行大量调整和优化。 For eg: Skipping redundant initializations, comparisons that never change etc. 例如:跳过冗余初始化,从不改变的比较等。

Note: You might have some difficulty debugging if your turn on optimization as the code you have and the IL code that is generated may not match. 注意:如果启用优化作为您拥有的代码和生成的IL代码可能不匹配,则可能会遇到一些调试困难。 This is the reason it is turned on only for Release builds. 这就是它仅为Release版本打开的原因。

Quoted from the MSDN page : 引自MSDN页面

The /optimize option enables or disables optimizations performed by the compiler to make your output file smaller, faster, and more efficient. / optimize选项启用或禁用编译器执行的优化,以使输出文件更小,更快,更高效。

In other words, it does exactly what you think it would - optimises the compiled CIL (Common Intermediate Language) code that gets executed by the .NET VM. 换句话说,它完全符合您的想法 - 优化由.NET VM执行的已编译CIL(通用中间语言)代码。 I wouldn't worry about what the specific optimisations are - suffice to say that they are many, and probably quite complex in some cases. 我不担心具体的优化是什么 - 足以说它们很多,在某些情况下可能相当复杂。 If you are really interested in what sort of things it does, you could probably investigate the Mono C# Compiler (I doubt the details about the MS C# one are public). 如果你真的对它做了什么样的事情感兴趣,你可能会调查Mono C#编译器 (我怀疑有关MS C#的详细信息是公开的)。

The reason optimisation is disabled by default for Debug configurations is that it makes certain debugging features impossible. 默认情况下, Debug配置禁用优化的原因是它使某些调试功能无法实现。 A few notable ones: 一些值得注意的:

  • Perhaps most crucially, the Edit and Continue feature is disabled - ie no modifying code during execution. 也许最重要的是, 编辑和继续功能被禁用 - 即在执行期间不修改代码。
  • Breaking execution often means the wrong line of code is highlighted (usually the one after the expected one). 打破执行通常意味着突出显示错误的代码行(通常是预期的代码之后)。
  • Unused local variables aren't actually assigned or even declared. 未实际分配或甚至声明未使用的局部变量。

Really, the default options for optimisation never ought to be changed. 实际上,优化的默认选项永远不应该改变。 Having the option off for debugging is highly useful, while having it on for Release mode is equally wise. 关闭调试选项是非常有用的,而将其置于Release模式同样明智。

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

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