Skip to content

gh-103089: Colorize only the beginning of very long lines in IDLE - #157647

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-103089-long-lines
Open

serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:gh-103089-long-lines

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

The Tk text widget itself handles million-character lines fine (with wrap='none', as in the editor), but adding a tag to a line takes time proportional to the number of tags already in the line: Tk walks the line's segment list for the index and then CleanupLine() scans the whole line. So colorizing a line with k tokens costs O(k²) — about 2.5 s for 7,000 tokens, and about half an hour for each of the ~200,000-token lines of the file from the issue. In addition, the colorizer resolved every position as chunk_head+Nc, through all segments of the previous lines of the chunk, and rescanned the whole line with the regular expression on every pass.

Now only the first 2000 characters of a line are colorized (tokens starting after that are skipped, and the scan jumps to the next line), positions are relative to the line start, and tags are added from the end to the start, which is about 1.5 times faster in Tk.

The file from the issue now opens in about 8 s instead of hours. Most of the remaining time is IndentSearcher tokenizing the whole 15 MB file in search of the first indented block; that is proportional to the file size, not the line length, and can be limited separately.

🤖 Generated with Claude Code

Adding a tag to a Tk text line takes time proportional to the number
of tags already in the line, so a line with hundreds of thousands of
tokens took hours to colorize.  Now only the first 2000 characters of
a line are colorized, tag positions are relative to the line start,
and tags are added from the end to the start.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant