Posts Tagged ‘Highlighting’

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