In response to a Performance Tip by Markus, Iain asks:
Is a Bold object little more than a Text object with an inherent formatting clue?
Quoting myself from a previous post:
Classes such as Image and DockPanel are responsible for their layout and rendering. This works fine for a simple class such as Image, which does not need to break across lines and cannot intelligently break across pages.
In contrast, classes such as Bold and Italic, are not responsible for their layout and rendering. Layout and rendering are instead handled by the parent TextPanel (or another element which can layout and render text content).
There are a variety of reasons for doing this, but the relevant reason is that TextPanel wants to be able to break the contents of a Bold or Italic tag across lines. (Of course, we could hook up the right APIs and enable these classes to perform their own layout and rendering, but this would mean more work and less performance)
The Bold element is actually an InlineElement with default formatting (FontWeight to be precise). Unlike Text, the Bold element is does not perform layout, instead it provides properties to a text range.
Back to Markus’ performance tip, why is the first example cheaper than the second?
<Text FontWeight="Bold">Hello!</Text> <Text><Bold>Hello!</Bold></Text>
The Text element has two methods for storage, depending on the nature of its content:
- String: If the Text element doesn’t contain any other elements, then Text will use a string for storage
- Text tree: This is used when the element contains additional elements, such as Bold or Image, along with the text content. This tree is faster for formatting multiple runs of text, however there is a performance hit upon initialization for this tree
If the entire Text element has the same formatting, we can optimize by using a string for storage; the use of the Bold element in the second markup example forces Text to use the more expensive text tree, thus causing a slight performance hit. (I don’t believe this optimization is in the PDC build, it may have been checked in a couple of weeks after the PDC)
Additionally, the second example instantiates an extra class (Bold); this isn’t a huge performance hit, but every little bit counts
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Trackbacks