财产决定论
C# 中有没有办法将属性标记为确定性?
我问的原因是我经常发现自己声明一个局部变量并将属性读入其中,而不是多次访问该属性。
有什么方法可以将属性装饰为确定性的,以便编译器可以优化对该属性的多次访问?我猜测在这种情况下,该类需要是不可变的,并进行这样的装饰。
这是真的存在的东西还是我抓住了救命稻草?
Is there any way in C# to mark a property as deterministic?
The reason I ask is that I often find myself declaring a local variable and reading a property into it, instead of accessing the property multiple times.
Is there any way that I can decorate the property as deterministic, such that the compiler could then optimize multiple accesses to that property? I am guessing in such a scenario that the class would need to be immutable, and decorated as such.
Is this something that even exists or am I clutching at straws?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果属性很简单,例如隐式属性:
或从局部变量读取:
那么编译器将优化代码,以便多次访问该属性与将属性放入变量并访问该属性之间没有区别。
我通过比较访问属性十次的 1 亿次迭代和将属性复制到变量并访问该十次来验证这一点,并且根本没有可测量的差异。
一般来说,属性应该是轻量级的,这样您就不必在每次访问它时都进行繁重的处理。如果属性值的获取成本很高,则类应该在内部缓存该值,以便读取属性只会在第一次执行成本高昂的操作(延迟加载模式)。
如果每次获取一个属性的成本都很高,那么它根本不应该是一个属性,而应该是一个 getter 方法。
If the property is simple, like an implicit property:
or reading from a local variable:
then the compiler will optimise the code so that there is no difference between accessing the property multiple times and putting the property in a variable and access that.
I verified this by comparing 100 million iterations of accessing a property ten times and copying the property to a variable and access that ten times, and there is no measuarable difference at all.
Generally properties should be leight-weight, so that you don't have to expect any heavy processing each time you access it. If the value for the property is costly to get, the class should cache the value internally so that reading the property only makes the costly operation the first time (lazy loading pattern).
If a property is costly to get each time, it shouldn't be a property at all, but a getter method.
C# 中没有任何机制允许您引入 const 属性 getter,即不更改对象状态的 getter。
Microsoft 文档只是建议不要在您的应用程序中引入任何副作用吸气剂:
正如 Daren 所提到的,要考虑的另一个方面是多线程(除非您的对象确实是不可变的)。如果另一个线程更改了对象状态,以便 getter 在第二次调用时返回不同的值,该怎么办?编译器没有简单的方法可以做出任何保证,例如在下面的场景中:
There is no mechanism in C# that allows you to introduce const property getters, i.e. getters that do not change the state of the object.
The Microsoft documentation simply recommends not to introduce any side-effects in your getters:
As mentioned by Daren, another aspect to consider is multi-threading (unless your object is truly immutable). What if another thread changed the object state so that the getter should return a different value on the second call? There is no easy way for the compiler to make any guarantees, e.g. in the scenario below:
我猜您正在寻找
只读
,但是我不确定性能与局部变量相比如何。而且它只适用于字段,不适用于属性。此外,只读并不意味着确定性。
仅仅意味着fixedList对象不能被替换,但是内容仍然可以改变。
I guess you're looking for
readonly
, however I'm not sure about how the performance is compared to a local variable. And it is only applicable to fields, not properties.Besides
readonly
does not imply determinism.just means that the fixedList object can not be replaced, but the content can still be changed.
除非您的属性的支持字段是只读的,否则您将如何解决线程问题?
Unless the backing field of your property is
readonly
, how are you going to account for threading issues?