Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
Why does multiple class member assignments produce redundant assembly?

While playing with C# I came across to this "strange behavior".

The function

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;
}

When you use the class keyword for Vec3 then the asm output produces redundant code:

public class Vec3
{
    public int x;
    public int y;
    public int z;
}

asm

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

But whit struct keyword we get the following output:

public struct Vec3
{
    public int x;
    public int y;
    public int z;
}

asm

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

Question

Are those assembly lines not redundant? If no, can you please explain me why?

Note

I know that class and struct are NOT the same. The question is about multiple MOV instructions.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda