静态构造函数是否保证不会被内联?

发布于 2024-12-05 16:26:06 字数 61 浏览 1 评论 0原文

大概吧!

编辑:这个问题的动机是:在我看来,由于静态构造函数的语义,它们永远无法安全地内联。

I guess so!

EDIT: The motivation for this question is: It seems to me that due to the semantics of static constructors they could never be safely inlined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

可爱暴击 2024-12-12 16:26:06

抖动的内联策略是一个实现细节 - 随时可能更改 - 因此几乎无法保证什么可以或不可以内联。

话虽如此,考虑到 C# 和 CLI 规范提供的有关静态构造函数和类型初始化的保证,很难看出如何安全地内联静态构造函数。

来自 Microsoft C# 规范(第 10.12 节):

封闭类类型的静态构造函数最多执行一次
在给定的应用领域。静态构造函数的执行
由以下事件中第一个发生的事件触发
应用领域:

  • 创建了该类类型的实例。
  • 引用类类型的任何静态成员。

并来自 ECMA CLI 规范(第 8.9.5 节):

[一个类可以]选择性地指定一个方法(称为.cctor
调用以初始化类型。

何时以及什么触发此类类型的执行的语义
初始化方法,如下:

  • 类型可以有类型初始化方法,也可以没有。

可以将类型指定为其具有宽松的语义
类型初始化方法(为了下面方便起见,我们称之为宽松的
语义 BeforeFieldInit)。

如果标记为 BeforeFieldInit,则该类型的初始化方法为
在第一次访问任何静态字段时或之前执行
为该类型定义。

如果没有标记BeforeFieldInit,则该类型的初始化方法
执行于(即触发于):

  • 首先访问该类型的任何静态字段,或者
  • 首次调用该类型的任何静态方法,或者
  • 首次调用该类型的任何实例或虚拟方法,如果
    它是一个值类型或
  • 首次调用该类型的任何构造函数。

(请注意,具有静态构造函数的 C# 类不会具有 beforefieldinit 语义。没有静态构造函数的 C# 类将具有 beforefieldinit 语义。)

The jitter's inlining strategy is an implementation detail -- subject to change at any time -- so there are pretty much no guarantees about what can or can't be inlined.

Having said that, it's difficult to see how a static constructor could ever be safely inlined, bearing in mind the guarantees provided by the C# and CLI specs regarding static constructors and type initialisation.

From the Microsoft C# specification (section 10.12):

The static constructor for a closed class type executes at most once
in a given application domain. The execution of a static constructor
is triggered by the first of the following events to occur within an
application domain:

  • An instance of the class type is created.
  • Any of the static members of the class type are referenced.

And from the ECMA CLI specification (section 8.9.5):

[A class can] optionally specify a method (called .cctor) to be
called to initialize the type.

The semantics of when and what triggers execution of such type
initialization methods, is as follows:

  • A type can have a type-initializer method, or not.

A type can be specified as having a relaxed semantic for its
type-initializer method (for convenience below, we call this relaxed
semantic BeforeFieldInit).

If marked BeforeFieldInit then the type's initializer method is
executed at, or sometime before, first access to any static field
defined for that type.

If not marked BeforeFieldInit then that type's initializer method
is executed at (i.e., is triggered by):

  • first access to any static field of that type, or
  • first invocation of any static method of that type, or
  • first invocation of any instance or virtual method of that type if
    it is a value type or
  • first invocation of any constructor for that type.

(Note that C# classes with a static constructor will not have beforefieldinit semantics. C# classes without a static constructor will have beforefieldinit semantics.)

走过海棠暮 2024-12-12 16:26:06

您拥有的唯一保证如下:

  • 在创建第一个实例或引用任何静态成员之前,自动调用静态构造函数来初始化类。
  • 用户无法控制静态构造函数何时在程序中执行。

The only guarantees you have are the following :

  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • The user has no control on when the static constructor is executed in the program.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文