这些操作码有什么用?
使用反射器,我得到以下输出:
.method private hidebysig static class myModelTestarea.Foo Method() cil managed
{
.maxstack 1
.locals init ([0] class myModelTestarea.Foo CS$1$0000)
L_0000: nop
L_0001: ldc.i4.0
L_0002: newarr object
L_0007: call object myModelTestarea.Program::Resolve(object[])
L_000c: castclass myModelTestarea.Foo
L_0011: stloc.0
L_0012: br.s L_0014
L_0014: ldloc.0
L_0015: ret
}
?
private static Foo Method()
{
return (Foo)Resolve();
}
private static object Resolve( params object[] args )
{
return new Foo();
}
第 11-14 行做什么 我调用一个函数并得到结果(第 7 行)。 我将结果转换为正确的返回类型(c 行) - 为什么不立即返回?
不知何故,转换结果存储为局部变量 - 然后无条件跳转到下一行,再次加载局部变量。 为什么?
在我看来,第 11-14 行和局部变量可以省略...?
Using reflector I get the following output:
.method private hidebysig static class myModelTestarea.Foo Method() cil managed
{
.maxstack 1
.locals init ([0] class myModelTestarea.Foo CS$1$0000)
L_0000: nop
L_0001: ldc.i4.0
L_0002: newarr object
L_0007: call object myModelTestarea.Program::Resolve(object[])
L_000c: castclass myModelTestarea.Foo
L_0011: stloc.0
L_0012: br.s L_0014
L_0014: ldloc.0
L_0015: ret
}
for
private static Foo Method()
{
return (Foo)Resolve();
}
private static object Resolve( params object[] args )
{
return new Foo();
}
What do the lines 11-14 do? I call a function and get a result (line 7). I cast the result to the right returntype (line c) - why not return right now?
Somehow, the casted result is stored as a local variable - then there is an uncoditional jump to the next line, where the local variable is loaded again. Why?
In my opinion line 11-14 and the local variable can be omitted ... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来像一个 DEBUG 构建,它留下了额外的 IL 来帮助调试器。 在 RELEASE 中再试一次,它应该看起来更干净,并进行了优化等。
That looks like a DEBUG build, which leaves in extra IL to help the debugger. Try it again in RELEASE and it should look cleaner, with optimization etc.
这是调试版本吗? 它可能是为了调试器而存在的。
我在其他地方也见过类似的事情——但它几乎总是无害的。 不要忘记大部分优化是由 JIT 完成的,它可以很容易地注意到这样的事情。 唯一的缺点是更多的 IL 提示 JIT 不应内联该方法。
Is this a debug build? It's possible that it's there for the sake of the debugger.
I've seen similar things in other places - it's almost always harmless though. Don't forget that most of the optimisation is done by the JIT, which can notice things like this easily enough. The only downside is that more IL hints to the JIT that the method shouldn't be inlined.