How is the WPF property system economical?

做~自己de王妃 提交于 2019-12-07 02:59:27

问题


It is said that the designers of WPF have made it economical or higher performance. Can someone please explain with an example of what happens under the hood that makes the WPF property system more economical?


回答1:


You are probably referring to the fact that Dependency Properties are "cheaper" than normal CLR properties.

In a few words:

Dependency properties are implemented using sparse data structures that only allocate memory for the property value if it is set on an object. In contrast, a standard CLR property value is stored as a field inside every object of the class in which the property is defined, even if all of these objects have the property set to its default value.

So for example, if we have 100 objects with 100 CLR properties of type int each, then we are using 10000 ints worth of memory even if all of those have the same default value (0).

If the property were a dependency property instead, we would not be using any additional memory at all: WPF does not need to remember the value of any property since it knows that you didn't change it from the default.

Of course the above is rather a simplistic explanation and does not cover all the advantages of dependency properties over CLR properties, but it should explain the "DPs have higher performance" statement adequately.




回答2:


Most of the 'properties' of a WPF control are in fact not present on the Control itself. Instead of adding dozens of (mostly unused) properties to the (base-)classes, they elected to add a "property bag" instead, a Dictionary holding only the properties that are actually set.

As a bonus it allows for ambient and injected properties.




回答3:


The WPF dependency property system stores actual property values in optimized data structures behind the scenes.

This has several advantages over storing property values as fields:

  • The dependency property system by NOT storing default values for properties for each object instance it can save allot of memory (basically if a property has a default value for a target object, space is not allocated for the value. This is opposed to the having properties with backing fields where the values are always stored, and the memory is always reserved for the object).

  • The dependency property system can have optimized event mechanism that avoids storing handler references on a per object basis (like using backing field based events), this means more space savings can be made.

There is of course a small overhead for such a system. Property access is not as lightweight as using a normal property, but the WPF team has decided the small overhead more than makes it up due to lower memory usage.




回答4:


In addition to the other answers:

Dependency properties in WPF support property value inheritance. With normal CLR properties it is much harder to push values down to any "child" objects without modifying the child object. This can obviously be done with attached methods and a static mapping, but would probably not be a very generic solution. While there is some overhead with inherited properties, they are fairly efficient at passing down values.



来源:https://stackoverflow.com/questions/5552943/how-is-the-wpf-property-system-economical

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!