gh-103089: Colorize only the beginning of very long lines in IDLE - #157647
Open
serhiy-storchaka wants to merge 1 commit into
Open
serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thenCleanupLine()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 aschunk_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
IndentSearchertokenizing 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