如何解决为什么多个类成员分配会产生冗余程序集? 注意
在使用 C# 时,我遇到了这种“奇怪的行为”。
功能
public static Vec3 f()
{
var v = new Vec3();
v.x = 0;
v.y = 0;
v.z = 0;
v.x = 0;
v.y = 0;
v.z = 0;
v.x = 0;
v.y = 0;
v.z = 0;
return v;
}
当您对 class
使用 Vec3
关键字时,asm 输出会产生冗余代码:
public class Vec3
{
public int x;
public int y;
public int z;
}
汇编
C.f()
L0000: sub rsp,0x28
L0004: mov rcx,0x7ff7aaa5d040
L000e: call 0x00007ff801dba370
L0013: xor edx,edx
L0015: mov [rax+8],edx
L0018: mov [rax+0xc],edx
L001b: mov [rax+0x10],edx
L001e: mov [rax+8],edx ; isn't this redundant?
L0021: mov [rax+0xc],edx ; isn't this redundant?
L0024: mov [rax+0x10],edx ; isn't this redundant?
L0027: mov [rax+8],edx ; isn't this redundant?
L002a: mov [rax+0xc],edx ; isn't this redundant?
L002d: mov [rax+0x10],edx ; isn't this redundant?
L0030: add rsp,0x28
L0034: ret
但是使用 struct
关键字我们得到以下输出:
public struct Vec3
{
public int x;
public int y;
public int z;
}
汇编
C.f()
L0000: xor eax,eax
L0002: mov [rcx],eax
L0004: mov [rcx+4],eax
L0007: mov [rcx+8],eax
L000a: mov rax,rcx
L000d: ret
问题
那些流水线不是多余的吗?如果不是,你能解释一下为什么吗?
注意
我知道 class
和 struct
不一样。问题是关于多个 MOV
指令。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。