这些操作码有什么用?

发布于 2024-07-23 11:54:42 字数 787 浏览 4 评论 0原文

使用反射器,我得到以下输出:

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-07-30 11:54:42

这看起来像一个 DEBUG 构建,它留下了额外的 IL 来帮助调试器。 在 RELEASE 中再试一次,它应该看起来更干净,并进行了优化等。

.method private hidebysig static class program/Foo Method() cil managed
{
    .maxstack 8
    L_0000: ldc.i4.0 
    L_0001: newarr object
    L_0006: call object program::Resolve(object[])
    L_000b: castclass program/Foo
    L_0010: ret 
}

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.

.method private hidebysig static class program/Foo Method() cil managed
{
    .maxstack 8
    L_0000: ldc.i4.0 
    L_0001: newarr object
    L_0006: call object program::Resolve(object[])
    L_000b: castclass program/Foo
    L_0010: ret 
}
方觉久 2024-07-30 11:54:42

这是调试版本吗? 它可能是为了调试器而存在的。

我在其他地方也见过类似的事情——但它几乎总是无害的。 不要忘记大部分优化是由 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文