对于大型 .maxstack,性能损失是什么(如果有)?
我正在编写一个编译器,该编译器的一个功能是它自动为值类型生成 GetHashCode()、Equals(object) 和 Equals({this value type}) 方法。现在,Equals({this value type}) 实现基本上生成与此 c# 生成的 il 等效的 il:
public bool Equals(ThisType o)
{
return Field1 == o.Field1 && Field2 == o.Field2 && Field3 == o.Field3;//etc
}
我的编译器将所有这些对象推入堆栈,然后开始比较并将它们“与”在一起。这会导致该方法的 .maxstack 很快变大。对此有处罚吗?如果是这样,我应该在什么时候开始向当地人灌输价值观?
谢谢。
I'm working on writing a compiler and one feature of this compiler is that it automatically generates GetHashCode(), Equals(object), and Equals({this value type}) methods for value types. Right now the Equals({this value type}) implementation basically generates il equivalent to the il generated by this c#:
public bool Equals(ThisType o)
{
return Field1 == o.Field1 && Field2 == o.Field2 && Field3 == o.Field3;//etc
}
My compiler pushes all of these objects on to the stack and then begins comparing and "and"ing them together. This causes the method's .maxstack to become large very quickly. Is there a penalty for this? If so, at what point should I start pushing values into locals?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不衡量对您来说重要的场景并找出答案呢?您的性能可能会因 .NET 版本或处理器架构的不同而有所不同,因此您需要真正衡量您关心的变体。据我了解,
maxstack
主要用于验证,所以我猜测不会有太大的性能损失,但唯一确定的方法是要实际测量。此外,就您而言,无论如何,您不需要任何当地人来避免堆栈的增长。您可以这样做:
无论字段数量如何,它都使用恒定的堆栈空间。
Why don't you measure the scenarios which are important to you and find out? Your performance could potentially vary across .NET versions or processor architectures, so you really out to measure the variants that you care about. As I understand it,
maxstack
is mainly used for verification, so I would guess that there won't be much of a performance penalty, but the only way to know for sure is to actually measure.Additionally, in your case you don't need any locals to avoid growing the stack, anyway. You could just do something like this instead:
This uses constant stack space regardless of the number of fields.