2

Do someone know if there is even a small difference using ldloc var CIL instruction and ldloc.n ?

Considering this local var table in a method scope :

.locals init ([0] int32 a,
              [1] int32 b)

Are those instructions:

ldloc.0
ldloc.1

better, worste or equal than:

ldloc a
ldloc b
svick
  • 236,525
  • 50
  • 385
  • 514
G. Ghez
  • 3,429
  • 2
  • 21
  • 18
  • The `ldloc var` is just sugar syntax for `ldloc.n` in ilasm. – leppie Apr 01 '14 at 12:38
  • I'm wondering if there is a performance impact to create more than 4 local variables since ldloc.n is right for 0 <= n <= 3... – G. Ghez Apr 01 '14 at 12:52
  • `ldloc n` can take any number. The `.` version is just the short version of it. Apparently slightly more efficient... – leppie Apr 01 '14 at 12:56
  • 2
    Yeah they are just 1 byte version, versus the 3 byte version opcode+short. It use to be that functions with 16 bytes or less of cil would be inlined. – Michael B Apr 01 '14 at 13:30
  • could you please give more details about inlining in this context ? – G. Ghez Apr 01 '14 at 14:52
  • I was wrong it was 32 bytes that was the usual jit limit. This article mentions more details about it. http://dotnet.dzone.com/news/aggressive-inlining-clr-45-jit You can now use a `methodattribute` to try to force inlining. – Michael B Apr 01 '14 at 18:12

1 Answers1

2

There are many shortened forms of common opcodes with a simple goal - to shorten the length of the method. ldloc.n instructions take up one byte, ldloc.s n two bytes, and ldloc n three bytes. This is useful for inlining, as methods longer than 16 bytes are not eligible for it.

IS4
  • 11,945
  • 2
  • 47
  • 86