Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Highlighting performance issue

I have a RichTextBox and once the user loads a file, my program proceeds to scan the entire file in order to change the color of certain words. Here is my code:

static Regex cKeyWords = new Regex(@"\b(?=[a-gilr-w])(?:
     s(?:hort|i(?:gned|zeof)|t(?:atic|ruct)|witch) | c(?:ase|har|on(?:st|tinue)) |
     e(?:lse|num|xtern) | i(?:f|nt) | f(?:loat|or) | d(?:o|efault|ouble) | un(?:ion|signed) |
     re(?:gister|turn) | vo(?:id|latile) | while | break | long | typedef | auto | goto
     )\b",
     RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

...

programTextBox.Enabled = false;
int selectStart = this.programTextBox.SelectionStart;
programTextBox.SuspendLayout();
MatchCollection matches = cKeyWords.Matches(programTextBox.Text);
foreach (Match match in matches)
{
    if (match.Index == 0)
        programTextBox.Select(match.Index, match.Length/* - 1*/);
    else
        programTextBox.Select(match.Index + 1, match.Length - 1);
    programTextBox.SelectionColor = Color.Blue;
}
programTextBox.Select(selectStart, 0);
programTextBox.SelectionColor = Color.Black;
programTextBox.Enabled = true;
programTextBox.ResumeLayout();

Problem: my code takes about 5 and a half seconds to scan and change the colors of all the keywords in a file that has 200,000 characters.

I've been told before that I shouldn't use a Regex for that kind of stuff, but after doing several tests I figured out that the: MatchCollection matches = cKeyWords.Matches(programTextBox.Text);

only takes about 0.1s and removing the

programTextBox.SelectionColor = Color.Blue;

reduces the total execution time of my code from 5.5s to about 0.3s

How? Why? And most importantly: What can I do?

like image 617
OC_ Avatar asked May 11 '15 20:05

OC_


People also ask

How does syntax highlighting help programmer identify coding problems?

Syntax highlighting also helps programmers find errors in their program. For example, most editors highlight string literals in a different color. Consequently, spotting a missing delimiter is much easier because of the contrasting color of the text.

How does syntax highlighting change the program?

Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible for colorizing keywords like if or for in JavaScript differently than strings and comments and variable names.

What is syntax highlighting in idle?

Syntax highlighting is an important feature of any IDE that highlights the syntax of the language that you're working in. This helps you visually distinguish between the different Python constructs and the data used in your code. Python IDLE allows you to fully customize the appearance of your Python code.

How do I disable syntax highlighting in VS Code?

You can enable / disable semantic highlighting in the VS Code settings. Press F1 to open the command window, and then enter "Open Settings (UI)". Search "semantic" to find the Editor>Semantic Highlighting item . Set the value to true to enable semantic highlighting; otherwise set it to false .


1 Answers

I maintain the C++ syntax highlighter for VS Code, which has single regex patterns that are more than 9000 character's long. The regex you wrote is very efficient, probably too efficient (removing the lookahead would help more than breaking up the words).

This is a fundamental UI issue. The UI is slow, there is no way for something like VS Code to highlight 200,000 lines in under a second. And that's fine because the user is never going to be looking at 200,000 lines at one time. So instead Atom/VS Code intelligently only call the equivalent programTextBox.SelectionColor = Color.Blue; for the lines that are visible. Same with Sublime and other text editors; they only update what-you-see and what-was-changed, because it is very expensive to mess with the GUI.

Instead of updating the UI ~100,000 times, do what Sriram Sakthivel's comment says (not sure why he didn't make his an answer) "Suspend painting before the loop and resume it immediately after it. RichTextBox syntax highlighting in real time--Disabling the repaint or better yet find a control which supports syntax highlighting. There are several open source libraries available."

This is effectively the same solution that is used for every text editor.

like image 54
Jeff Hykin Avatar answered Sep 18 '22 08:09

Jeff Hykin