views:

816

answers:

1

I'm trying to display basic syntax highlighting in a WPF RichTextBox. It mostly works, but the rendering performance is awful.

First I naively tried:

/// <summary>
/// Main event handler for syntax highlighting.
/// </summary>
private void XmlChanged(object sender, TextChangedEventArgs e)
{
    VM.Dirty = true;
    if (VM.Pretty)
    {
        var range = new TextRange(XmlView.Document.ContentStart, XmlView.Document.ContentEnd);
        Render(range.Text);
    }
}

/// <summary>
/// Entry point for programmatically resetting the textbox contents
/// </summary>
private void Render(string text)
{
    XmlView.TextChanged -= this.XmlChanged;

    if (VM.Pretty)
    {
        var tokens = tokenizer.Tokenize(text);
        Format(XmlView.Document, tokens);
    }

    XmlView.TextChanged += this.XmlChanged;     
}

private void Format(FlowDocument doc, List<Token> tokens)
{
    var start = doc.ContentStart;
    foreach (var token in tokens)
    {
        TextRange range = new TextRange(start.GetPositionAtOffset(token.StartPosition, LogicalDirection.Forward),
                                        start.GetPositionAtOffset(token.EndPosition, LogicalDirection.Forward));
        range.ApplyPropertyValue(TextElement.ForegroundProperty, m_syntaxColors[token.Type]);
    }
}

Testing on a 2KB document with just over 100 tokens, it took 1-2 seconds to redraw after every keystroke; clearly not acceptable. Profiling showed that my tokenizer was orders of magnitude faster than the Format() function. So I tried some double-buffering:

private void Render(string text)
{
    XmlView.TextChanged -= this.XmlChanged;

    // create new doc offscreen
    var doc = new FlowDocument();
    var range = new TextRange(doc.ContentStart, doc.ContentEnd);
    range.Text = text;

    if (VM.Pretty)
    {
        var tokens = tokenizer.Tokenize(text);
        Format(doc, tokens);
    }

    // copy to active buffer
    var stream = new MemoryStream(65536);
    range.Save(stream, DataFormats.XamlPackage);
    var activeRange = new TextRange(XmlView.Document.ContentStart, XmlView.Document.ContentEnd);
    activeRange.Load(stream, DataFormats.XamlPackage);

    XmlView.TextChanged += this.XmlChanged;     
}

Benchmarks show that Format() runs slightly faster rendering offscreen, but the perceived performance is even worse now!

What's the right way to go about this?

+1  A: 

I'd try taking as much object instantiation out of the method/loop as possible and pass in references instead. You're calling new quite a few times per loop per keystroke.

karl.r
Unfortunately the Start and End properties on the TextRange class are read-only. Only way I've found to represent the correct range on each iteration is to create new ones.
Richard Berg