为什么不自动属性默认情况下,内联? [英] Why aren't Automatic Properties inlined by default?

查看:166
本文介绍了为什么不自动属性默认情况下,内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为该物业只是引擎盖下的方法,这是可以理解的逻辑,他们可能会执行的性能可能会或可能不会提高性能 - 所以为什么JIT需要检查的方法是值得的内联这是可以理解的。



自动属性(然而据我了解),不能有任何的逻辑,并简单地返回或设置基础字段的值。据我所知,自动属性是由编译器和JIT就像任何其他方法治疗。

(下一切都将依赖于这样的假设上段是正确的。)



值类型的属性显示出比变量本身不同的行为,而引用类型的属性按说应该具有完全相同的行为直接访问底层变量。

  //自动属性范例
公共对象MyObj中获得{;私人集; }



是否有任何情况下自动属性为引用类型可以显示性能由内联打?

。如果没有,是什么妨碍任何编译器或自动内联他们JIT?



请注意:据我所知,性能提升很可能是微不足道的,特别是当如果使用足够多次的JIT可能无论如何内联 - 但小的增益可能是,它似乎是合乎逻辑,这样的看似简单的优化将考虑出台。


解决方案

自动但是性能(据我所知)不能有任何
的逻辑,并简单地返回或设置底层字段的值。由于
据我所知,自动属性由编译器和
的JIT就像任何其他方法治疗。




这自动属性不能有任何的逻辑是一个实现细节,没有的所需编译该事实任何专业知识。事实上,正如你所说的自动属性被编译到方法调用。



假设汽车propes被内联和类和属性在不同的程序集中定义。这意味着,如果属性实现改变,你就必须重新编译应用程序看到的变化。这违背了使用中应允许您更改内部实现,而无需重新编译应用程序消耗的首位属性。


Being that properties are just methods under the hood, it's understandable that the performance of any logic they might perform may or may not improve performance - so it's understandable why the JIT needs to check if methods are worth inlining.

Automatic properties however (as far as I understand) cannot have any logic, and simply return or set the value of the underlying field. As far as I know, automatic properties are treated by the Compiler and the JIT just like any other methods.
(Everything below will rely on the assumption that the above paragraph is correct.)

Value Type properties show different behavior than the variable itself, but Reference Type properties supposedly should have the exact same behavior as direct access to the underlying variable.

// Automatic Properties Example
public Object MyObj { get; private set; }

Is there any case where automatic properties to Reference Types could show a performance hit by being inlined?
If not, what prevents either the Compiler or the JIT from automatically inlining them?

Note: I understand that the performance gain would probably be insignificant, especially when the JIT is likely to inline them anyway if used enough times - but small as the gain may be, it seems logical that such a seemingly simple optimization would be introduced regardless.

解决方案

Automatic properties however (as far as I understand) cannot have any logic, and simply return or set the value of the underlying field. As far as I know, automatic properties are treated by the Compiler and the JIT just like any other methods.

That automatic properties cannot have any logic is an implementation detail, there is not any special knowledge of that fact that is required for compilation. In fact, as you say auto properties are compiled down to method calls.

Suppose auto propes were inlined and the class and property are defined in a different assembly. This would mean that if the property implementation changes, you would have to recompile the application to see that change. That defeats using properties in the first place which should allow you to change the internal implementation without having to recompile the consuming application.

这篇关于为什么不自动属性默认情况下,内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆