对于大型 .maxstack,性能损失是什么(如果有)?

发布于 2024-09-07 12:35:29 字数 423 浏览 6 评论 0原文

我正在编写一个编译器,该编译器的一个功能是它自动为值类型生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2024-09-14 12:35:29

为什么不衡量对您来说重要的场景并找出答案呢?您的性能可能会因 .NET 版本或处理器架构的不同而有所不同,因此您需要真正衡量您关心的变体。据我了解,maxstack主要用于验证,所以我猜测不会有太大的性能损失,但唯一确定的方法是要实际测量。

此外,就您而言,无论如何,您不需要任何当地人来避免堆栈的增长。您可以这样做:

  load Field1
  load o.Field1
  branch to end if not equal
  load Field2
  load o.Field2
  branch to end if not equal
  ...
  return true
end:
  return false

无论字段数量如何,它都使用恒定的堆栈空间。

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:

  load Field1
  load o.Field1
  branch to end if not equal
  load Field2
  load o.Field2
  branch to end if not equal
  ...
  return true
end:
  return false

This uses constant stack space regardless of the number of fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文