开发者

How is the WPF property system economical?

It is said t开发者_StackOverflowhat 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?


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜