本文介绍了MS C#编译器和非优化code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我注意到在我贴出例如一些错误 - 编辑解决它

该负责人C#编译器做一些有趣的事情,如果你没有启用优化。

The official C# compiler does some interesting things if you don't enable optimization.

例如,如果一个简单的语句:

For example, a simple if statement:

int x;
// ... //
if (x == 10)
   // do something

变得像下面这样,如果优化的:

becomes something like the following if optimized:

ldloc.0
ldc.i4.s 10
ceq
bne.un.s do_not_do_something
// do something
do_not_do_something:

但如果我们禁用的优化,就变成是这样的:

but if we disable optimization, it becomes something like this:

ldloc.0
ldc.i4.s 10
ceq
ldc.i4.0
ceq
stloc.1
ldloc.1
brtrue.s do_not_do_something
// do something
do_not_do_something:

我不能完全得到我的头,围绕这一点。为什么所有额外code,这貌似不是present源吗?在C#中,这将是等价的:

I can't quite get my head around this. Why all that extra code, which is seemingly not present in the source? In C#, this would be the equivalent of:

int x, y;
// ... //
y = x == 10;
if (y != 0)
   // do something

有谁知道为什么它这样做?

Does anyone know why it does this?

推荐答案

我不完全理解的问题点。这听起来像你问为什么编译器产生的未优化code当优化开关关闭?这有点儿答案本身。

I don't fully understand the point of the question. It sounds like you're asking "why does the compiler produce unoptimized code when the optimization switch is off?" which kinda answers itself.

不过,我会采取刺伤它。我觉得这个问题实际上是像什么设计决策会导致编译器发出的地方#1,它可以被优化掉的声明,存储和加载?

However, I'll take a stab at it. I think the question is actually something like "what design decision causes the compiler to emit a declaration, store and load of local #1, which can be optimized away?"

答案是因为未优化codeGEN的设计是明确的,毫不含糊的,易调试,并鼓励抖动产生code,它的没有的积极收集垃圾。其中我们实现所有这些目标的方法之一是生成的当地人的为进入堆叠,甚至临时的值最值。让我们来看看一个更复杂的例子。假设你有:

The answer is because the unoptimized codegen is designed to be clear, unambiguous, easy to debug, and to encourage the jitter to generate code that does not aggressively collect garbage. One of the ways we achieve all those goals is to generate locals for most values that go on the stack, even temporary values. Let's take a look at a more complicated example. Suppose you have:

Foo(Bar(123), 456)

我们可能会产生这样的:

We could generate this as:

push 123
call Bar - this pops the 123 and pushes the result of Bar
push 456
call Foo

这是很好的,高效的和小的,但它并不符合我们的目标。它是清晰和明确的,但它是不容易的调试,因为垃圾收集器可以得到侵略性。 如果富出于某种原因,实际上并没有做任何事情与它的第一个参数则GC允许回收律师的返回值美孚运行前。

That is nice and efficient and small, but it does not meet our goals. It is clear and unambiguous, but it is not easy to debug because the garbage collector could get aggressive. If Foo for some reason does not actually do anything with its first argument then the GC is allowed to reclaim the return value of Bar before Foo runs.

在未经优化的版本,我们会产生更多的东西像

In the unoptimized build we would generate something more like

push 123
call Bar - this pops the 123 and pushes the result of Bar
store the top of the stack in a temporary location - this pops the stack, and we need it back, so
push the value in the temporary location back onto the stack
push 456
call Foo

现在的抖动有一个很大的提示,上面写着:嘿抖动的保持这种活在当地一段时间,即使富不使用它

Now the jitter has a big hint that says "hey jitter, keep that alive in the local for a while even if Foo doesn't use it"

这里的一般规则是使局部变量的所有临时值在未优化版本。所以,你去;以评估如果声明,我们需要评估的条件,并将其转换为bool。 (当然,情况不一定是bool类型的,它可能是一个实现了operator true /操作员错误对一个类型隐式转换为bool,或类型。)未优化code发电机已被告知积极转所有临时值到当地人,所以这就是你得到的。

The general rule here is "make local variables out of all temporary values in the unoptimized build". And so there you go; in order to evaluate the "if" statement we need to evaluate a condition and convert it to bool. (Of course the condition need not be of type bool; it could be of a type implicitly convertible to bool, or a type that implements an operator true/operator false pair.) The unoptimized code generator has been told "aggressively turn all temporary values into locals", and so that's what you get.

我想在这种情况下,我们可以燮preSS上临时的都是如果语句的条件,但是这听起来像的让我的工作,没有客户的利益的。因为我有一堆的工作,只要你的手臂的,做的有切实的客户利益,我不会改变未优化code发生器,它产生未优化code,正好因为它被认为

I suppose in this case we could suppress that on temporaries that are conditions in "if" statements, but that sounds like making work for me that has no customer benefit. Since I have a stack of work as long as your arm that does have tangible customer benefit, I'm not going to change the unoptimized code generator, which generates unoptimized code, exactly as it is supposed to.

这篇关于MS C#编译器和非优化code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:18