我想知道 .maxstack 是如何工作的。 我知道这与您声明的类型的实际大小无关,而是与它们的数量有关。 我的问题是:
- 这是否仅适用于
函数,或所有函数
我们所呼吁的?
- 即使只是为了功能
正在声明 .maxstack,
你怎么知道 maxstack 是多少
你有分支吗? 你去看看
所有“路径”并返回
可能的最大值?
- 如果我将其设置为 16 并且会发生什么
实际上有17个变量?
- 如果我这样做的话,惩罚是否太大了?
设置为256?
I'd like to know how does .maxstack really work. I know it doesn't have to do with the actual size of the types you are declaring but with the number of them. My questions are:
- does this apply just for the
function, or to all the functions
that we are calling for?
- even if it's just for the function
were .maxstack is being declared,
how do you know what maxstack is if
you have branching? You go and see
all the "paths" and return the
maximum value possible?
- What happens if I set it to 16 and
actually there are 17 variables?
- Is there a too big of a penalty if I
set it to 256?
发布评论
评论(3)
您可以参考以下内容和 ECMA 标准来获得更好的理解:
当我运行 ildasm.exe 时,我得到了:
从上面。 我发现最大
stakc
值不是由推送和推送决定的。 弹出指令。我不知道真正的堆栈数值是多少。 因此,我引用 ildasm 反汇编代码来确定真正的最大堆栈值。
You can refer to the following and the ECMA STANDARD to get a better understanding:
When I run
ildasm.exe
I got this:from the above. I found the max
stakc
value which isn't determined by the push & pop instructions.I didn't know what the real stack number values are. So, I reference the
ildasm
disassembly code to determine the real max stack value.它与声明的变量的数量无关,而是与在任何给定时间需要将多少值压入堆栈以计算某个表达式有关。
例如,在下面的表达式中,我假设需要将 2 个值压入堆栈:
这与至少存在 3 个变量(x、y 和 z,可能还有其他变量)的事实无关。
不幸的是,我不知道你其他问题的答案,我想实验是找到一些答案的一种方法。
It has nothing to do with the number of variables declared, but instead everything to do with how many values you need to push on a stack at any given time in order to compute some expression.
For instance, in the following expression, I would assume 2 values needs to be pushed onto the stack:
This is unrelated to the fact that there are at least 3 variables present, x, y, and z, and possibly others as well.
Unfortunately I don't know the answer to your other questions, and I would guess experimentation would be one way to find some answers.
.maxstack
是 IL 验证的一部分。 基本上,.maxstack
告诉 JIT 需要为该方法保留的最大堆栈大小。 例如,x = y + (a - b)
转换为(Pseudo IL:)
如您所见,每次堆栈中最多有 3 个项目。
如果您将此方法的
.maxstack
设置为 2(或更少),则代码将不会运行。另外,您不能拥有这样的东西,因为它需要无限的堆栈大小:
要回答您的问题:
只是为了功能
你去查看所有路径并返回可能的最大值
与变量的数量无关,参见Lasse V. Karlsen的回答
似乎不是一个好主意,但我不知道。
您真的需要自己计算
.maxstack
吗?System.Reflection.Emit
为您计算 IIRC。.maxstack
is part of the IL verification. Basically.maxstack
tells the JIT the max stack size it needs to reserve for the method. For example,x = y + (a - b)
translates to(Pseudo IL:)
As you can see, there are at most 3 items on the stack at each time.
If you'd set
.maxstack
to 2 (or less) for this method, the code wouldn't run.Also, you cannot have something like this as it would require an infinite stack size:
To answer your questions:
Just for the function
You go and see all the paths and return the maximum value possible
It's unrelated to the number of variables, see Lasse V. Karlsen's answer
Doesn't seem like a good idea, but I don't know.
Do you really have to calculate the
.maxstack
yourself?System.Reflection.Emit
calculates it for you IIRC.