Where are .NET local variables stored?

前端 未结 3 1593
独厮守ぢ
独厮守ぢ 2020-12-06 11:12

In IL, you can define local variables using the .locals directive. Where are these variables stored, stack or heap?

3条回答
  •  一个人的身影
    2020-12-06 11:49

    It is very much an implementation detail of the JIT compiler. It will try very hard to store local variables in a CPU register, very efficient. The stack is the usual backing store, in case there aren't enough registers available to store all the local variables.

    Big difference between the x86 and x64 jitters for example. x64 has a lot more registers available. This also applies to the arguments passed to a method. x86 allows 2 passed in a CPU register, x64 allows 4. Plus whatever can be stored in the FPU stack or XMM registers. So, there are really four distinct places a local variable can be stored.

提交回复
热议问题