Text, TextPanel, and ContentElements

posted 2004-03-11

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:

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

Editor's Note: I realize it's been a while since I last posted. I'm trying to get back into the blogging habit, if you'll take me back. Baby, I didn't mean it! You know I still love you!