gpt4 book ai didi

c# - ldobj 和 ldind. 有什么区别,为什么 ldobj 更快?

转载 作者:太空狗 更新时间:2023-10-29 20:27:55 24 4
gpt4 key购买 nike

当使用 64 位大小的结构时,以下代码片段

[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 8)]
unsafe struct BUF
{
}

((BUF*)dst) = *((BUF*)src);

生产

    IL_0046: nop          
IL_0047: ldloc.s dst
IL_0049: ldloc.2
IL_004a: ldobj MyClass/BUF
IL_004f: stobj MyClass/BUF

但是,当只使用 long 时,下面的代码产生

*((long*)dst) = *((long*)src); 

产生:

IL_0046: nop
IL_0047: ldloc.s dst
IL_0049: ldloc.2
IL_004a: ldind.i8
IL_004b: stind.i8

有人知道 ldobj/stobj 和 ldind.i8/stind.i8 对这个例子有什么区别吗?

ldobj/stobj 似乎提高了 20% 的性能,但我不明白为什么会这样。这两行不是在做完全相同的事情吗?

谢谢!

编辑:[64 位 Release模式] 在 Release模式下编译时字节码看起来是一样的。性能测量是在 Release模式下完成的。

最佳答案

我已经复制了您正在使用的两种不同的方法,并看到生成了相同的 IL,但是在 Release模式下运行时,这两种方法的 jitted 代码完全相同:

这是我使用的测试方法:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Test
{
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 8)]
unsafe struct BUF
{
}

static class Program
{
static void Main()
{
BUF x, y, z;
unsafe
{
Do1(&x, &y);
Do2(&y, &z);
}
// Readline here to allow attaching debugger and dumping jitted code
Console.ReadLine();
}

// Disable inlining to permit easier identification of the code
[MethodImpl(MethodImplOptions.NoInlining)]
unsafe static void Do1(BUF* src, BUF* dst)
{
*((BUF*)dst) = *((BUF*)src);
}

// Disable inlining to permit easier identification of the code
[MethodImpl(MethodImplOptions.NoInlining)]
unsafe static void Do2(BUF* src, BUF* dst)
{
*((long*)dst) = *((long*)src);
}
}
}

这两种方法的 IL 与您的匹配:

Do1:

IL_0000: ldarg.1 IL_0001: ldarg.0 IL_0002: ldobj Test.BUFIL_0007: stobj Test.BUFIL_000c: ret 

Do2:

IL_0000: ldarg.1 IL_0001: ldarg.0 IL_0002: ldind.i8 IL_0003: stind.i8 IL_0004: ret 

并转储 jitted 代码:

Do1:

Test.Program.Do1(Test.BUF*, Test.BUF*)Begin 000007ff00170190, size 7000007ff`00170190 488b01          mov     rax,qword ptr [rcx]000007ff`00170193 488902          mov     qword ptr [rdx],rax000007ff`00170196 c3              ret

Do2:

Test.Program.Do2(Test.BUF*, Test.BUF*)Begin 000007ff001701b0, size 7000007ff`001701b0 488b01          mov     rax,qword ptr [rcx]000007ff`001701b3 488902          mov     qword ptr [rdx],rax000007ff`001701b6 c3              ret

他们看起来和我一模一样。

关于c# - ldobj 和 ldind.<type> 有什么区别,为什么 ldobj 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15162390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com