views:

144

answers:

2

I'm constructing a formatted FlowDocument from XML. The XML is well formed and consists mainly of 10,000 nodes each with a single node with a 6 character string value.

Parsing the XML to an XElement and constructing the FlowDocument in memory takes about 5 seconds. Assigning the FlowDocument to the Document property of a RichTextBox in my application then takes about 7 minutes, and maxes out the CPU for that time.

Here is the relevant piece of code:

// The following six lines of code execute in about 5 seconds

var xml = XElement.Parse(response.Data);

PrettyXmlConverter px = new PrettyXmlConverter();
FlowDocument fd = px.Render(xml);

Paragraph p = new Paragraph();
p.Inlines.Add(new Run(response.TimeStamp.ToShortDateString() + " " + response.TimeStamp.ToLongTimeString()));
fd.Blocks.InsertBefore(fd.Blocks.ElementAt(0), p);

// This line of code takes about 7 minutes and maxes out the CPU for that time.
tbResponse.Document = fd;

I'm wondering what's going on here. I've profiled the code and see scores of thousands of calls to unmanaged methods such as fsFormatSubtrackBottomless and SubtrackFormatParaBottomless.

Can anyone shed any light on the problem, or come up with a workaround?

A: 

To understand the reason of slowdown you need to use some profiler. For example, YourKit .NET profiler http://www.yourkit.com/dotnet/ It has free trial period.

Serge
I used dotTrace, by JetBrains. It showed that the problems lay in the framework, in mostly unmanaged code. Not sure what I can do about that, other than introduce a workaround of not using a FlowDocument if the message is over a certain size. I've saw similar posts on the web, it seems assigning a FlowDocument with a large number of blocks to a RichTextBox or FlowDocumentScrollViewer causes a serious performance degradation.
Winston Smith
A: 

In the end, I couldn't find a solution to this.

I'm using a workaround - I simply don't "pretty print" messages over a certain size.

If anyone has a better solution, feel free to post it.

Winston Smith