Highlighting in Cantor

Disclaimer: another post about 4.6 when 4.5 is barely out. Please don’t hate me. If you do anyway, scroll to the end of the post.

When I started working on Cantor, most of its highlighters worked like this:

There was a struct HiglightingRule with two members: a QRegExp and a QTextCharFormat. The highlighter would make a QList of these rules, iterate over them every time the cursor moved, and formatted all matches of each regex with the associated format. As there could be a lot of these rules (~1600 for Octave), things could get slow. The other backends didn’t have that many rules, that’s why I was most bothered with it. Thousands of iterations and especially regex-matchings for every key pressed is really too much for my poor laptop. It was enough to make scrolling non-smooth. Because QSyntaxHighlighter can’t easily be made asynchronous, holding down the right arrow key caused the cursor to hang and jump randomly.

Fortunately, Alexander (Cantor’s author) and Oleksiy (who works on the R backend) tried a different implementation: The text was broken down into words, and each word checked against a database. We then decided that a database is not needed and I replaced it with a QHash<QString, QTextCharFormat>. Iterating over 1600 regexes was replaced with iterating over less than ten words and searching for them in a hash.

I preserved the QRegExp-based API for things like strings (‘.*’ and/or ".*"), but most of the rules are now converted to the word-based highlighting. Even without a proper benchmark, it’s obvious that scrolling is much faster now. It’s also not something screenshot-worthy, so I have to disappoint you again. Please don’t hate me again.

Final note for spoiler-haters: Even though I’m talking like I’m already skipping it, and I don’t have it because it’s not in Arch yet, there is something special for me about the 4.5 release. It’s the very first release of KDE with a piece of my code. Quite literally a piece, because in total it’s two new lines, but it makes me prowd. I added auto-prefetching to the Comic plasmoid.

Advertisements

4 responses to this post.

  1. Posted by arieder on 24. August 2010 at 21:05

    just for the record: you do have some more lines in 4.5: you changed the ScriptEditor widget to use KXmlGui in Cantor.

    Reply

    • Posted by noughmad on 25. August 2010 at 07:30

      Correct, thanks. I just threw all my Cantor work in the same bin and forgot some of it happened before.

      Reply

  2. Posted by Parker on 25. August 2010 at 15:37

    Good news! SC 4.5 just hit extra! Time to “pacman -Syu”.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: