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

223
Vistas
Is there a reason why Roslyn does not optimize multiple increments?

I was trying to see how Roslyn optimizes the following snippet:

code

public int F(int n) {
    ++n;       
    ++n;       
    ++n;       
    ++n;       

    return n;
}              

asm

C.F(Int32)
    L0000: inc edx
    L0002: inc edx
    L0004: inc edx
    L0006: inc edx
    L0008: mov eax, edx
    L000a: ret

Question

why doesn't Roslyn optimize it like an ahead-of-time C compiler like MSVC? 4 x INC is slower (4 cycle latency vs. 1 even assuming mov-elimination, and 4 more uops than necessary for throughput; https://agner.org/optimize/).

C "equivalent" of it:

int
f(void *dummy_this, int n) {
        ++n;        
        ++n;        
        ++n;        
        ++n;        

        return n;
}

asm from MSVC, or GCC with __attribute__((ms_abi)) to use the same Windows x64 calling convention as the C# asm: https://godbolt.org/z/sK6h7KKcn

f:
        lea     eax, [rdx+4]
        ret
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The compiler does optimize. n is a parameter though, so it can't be modified. The JIT compiler must modify a copy of the parameter's value.

If the value is assigned to a variable before incrementing, the Roslyn compiler will eliminate the increments. From this Sharplab.io snippet, this C# code :

public int F(int i) {
    var n=i;
    ++n;       
    ++n;       
    ++n;       
    ++n;       

    return n;
} 

Will be translated to

public int F(int i)
{
    return i + 1 + 1 + 1 + 1;
}

And eventually compiled to this assembly code:

C.F(Int32)
    L0000: lea eax, [edx+4]
    L0003: ret
over 4 years ago · Santiago Trujillo Denunciar
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