Tk Source Code

View Ticket
Login
Ticket UUID: 1564ae296d59ee9afd8f5e55905ac1b7778ba04b
Title: text widget's count subcommand can take many seconds
Type: Bug Version: 8.6.*, 8.7a*, trunk
Submitter: nemethi Created on: 2021-08-15 15:53:35
Subsystem: 18. [text] Assigned To: nobody
Priority: 5 Medium Severity: Severe
Status: Closed Last Modified: 2026-10-06 14:07:44
Resolution: Fixed Closed By: nemethi
    Closed on: 2026-10-06 14:07:44
Description:
This bug report is closely related to the one contained in https://core.tcl-lang.org/tk/info/80213d1b1c.

In the presence of a large number of elided lines, the text widget's count subcommand can take many seconds, as demonstrated by the following script:

text .t -yscrollcommand [list .vsb set]
for {set n 1} {$n <= 10000} {incr n} {
    .t insert end "Line $n\n"
}
.t tag configure Elided -elide 1
.t tag add Elided 6.0 9951.0

scrollbar .vsb -orient vertical -command [list .t yview]

pack .vsb -side right -fill y
pack .t   -side right -expand 1 -fill both

# Patch the Tk library procedure ::tk::TextUpDownLine (see file text.tcl)
# by adding code that measures the time consumed by "$w count -displaylines".
# This proc is invoked, e.g., from within the binding for <<NextLine>>.

proc ::tk::TextUpDownLine {w n} {
    variable ::tk::Priv

    set i [$w index insert]
    if {$Priv(prevPos) ne $i} {
        set Priv(textPosOrig) $i
    }

    puts "invoking \"$w count -displaylines $Priv(textPosOrig) $i\" ..."
    set startTime [clock seconds]
    set lines [$w count -displaylines $Priv(textPosOrig) $i]
    set stopTime [clock seconds]
    puts "result: $lines -- it took [expr {$stopTime - $startTime}] seconds"

    set new [$w index \
            "$Priv(textPosOrig) + [expr {$lines + $n}] displaylines"]
    set Priv(prevPos) $new
    if {[$w compare $new == "end display lineend"] \
            || [$w compare $new == "insert display linestart"]} {
        set Priv(textPosOrig) $new
    }
    return $new
}

Start the script, click anywhere into Line 5, and then press the Down key twice.  The output produced by the script on my Linux box will look like

invoking ".t count -displaylines 5.4 5.4" ...
result: 0 -- it took 0 seconds
invoking ".t count -displaylines 5.4 9951.4" ...
result: 1 -- it took 25 seconds

As seen from this output, in this case the 2nd invocation of the text widget's count subcommand takes an unacceptably long time.  On a faster computer you will see maybe 20 or "only" 15 seconds, but all these times are by far beyond the values that can be viewed as user-friendly.

For comparison: With Tk 8.5 or a Tk version built from the revised_text branch, the count subcommand takes at most a few milliseconds.  Unfortunately, with revised_text, the 2nd press on the Down key will jump to the end of the text, because the ::tk::TextUpDownLine proc returns an incorrect index.
User Comments: nemethi (claiming to be Csaba Nemethi) added on 2026-10-06 14:07:44:

Serhiy, many thanks for the brilliant fix and its improvement that avoids the panic in debug mode! Merged into main (by commits [214a5f51], [01c63d0a]), core-9-0-branch (by commits [01174ca8], [ff77f31e]), and core-8-6-branch (by commits [46a94e56], [19a6d09b]).


serhiy.storchaka added on 2026-09-18 20:51:54:

Same cause as [4c595d4d78] (count -displaylines updates the line metrics through TkTextUpdateOneLine()). Fix in branch [24ac48f567]: 6 ms instead of 7.8 s here with the script above.


nemethi (claiming to be Csaba Nemethi) added on 2022-09-05 16:31:00:

That problem was the subject of ticket [934cab5005].


fvogel added on 2022-09-04 19:48:40:
The problem with the revised text widget mentioned at the end of this report must have been solved, I cannot reproduce it anymore.