Why are text editors slow when editing very long lines?

Most text editors work slowly when the lines are very long. The proposed data storage structure for a text editor seems like a rope that should be protected from changing long lines. By the way, editors are even slow when they just go into long lines.

Example: A single character, such as 0, repeated 100,000 times in the PSPad or 1,000,000 times in Vim on the same line, slows down the cursor when you are at the end of the line. If there are as many bytes in the file but sent over several lines, the cursor does not slow down at all, so I suppose this is not a memory problem.

What is the origin of this problem?

I mainly use Windows, so maybe this is related to the processing of Windows fonts?

+6
source share
3 answers

You are probably using variable length encoding like utf8. The editor wants to keep track of which column you are in with each cursor movement, and with variable-length encoding, there is no shortcut to scan each byte to find out how many characters there are; with a long line that scans a lot.

I suspect that you will not see this slowdown with long strings using single-byte encoding such as iso8859-1 (latin1). If you use single-byte encoding, character length = byte length, and the column can be quickly calculated using simple pointer arithmetic. A fixed-length multibyte encoding such as ucs-2 should be able to use the same label (just dividing by a constant character size), but editors may not be smart enough to take advantage of this.

+8
source

As the evil one suggested, linear encoding can cause the line to be reanalyzed, and for long lines it causes all kinds of performance problems.

But not only the encoding causes the string to be processed again.

Tabs also require a full line scan, since you need to analyze the entire line to calculate the true cursor location.

Certain syntax highlighting definitions (i.e. block comments, quoted lines, etc.) also require a complete line analysis.

+1
source

You mentioned vim, so I will assume that the editor you use. Vim does not use rope, as described here and here . It uses an array of strings, so your assumption that the ropes should be protected from such long lines may not matter here because the ropes are not used.

0
source

Source: https://habr.com/ru/post/897148/


All Articles