Tk Source Code

View Ticket
Login
Ticket UUID: 5d983ebf4865037ed85ad5b43e567c5485edc2dc
Title: Text widget: RTL auto-detection makes mapping and editing slow
Type: Bug Version: 9.1b1
Submitter: serhiy.storchaka Created on: 2026-09-24 10:46:49
Subsystem: 18. [text] Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-10-13 04:14:18
Resolution: Fixed Closed By: nab
    Closed on: 2026-10-13 04:14:18
Description:

Since [8ce823d20c] (merge of rtl_text) the Text class bindings scan every line of the text in Tcl (::tk::TextDetectAndTagRtl) and run tag remove on each line, on <Map> and, via after idle, after every insert or paste. Each tag remove invalidates line metrics and restarts the metric timers.

Mapping 900 text widgets with 300 lines each takes 2.1 s instead of 54 ms (9.0.5); a single text with 100000 lines takes about 1.3 s to map.

Two more problems in the same code:

  • ::tk::Priv(rtl_detect_pending) is shared by all text widgets, so a second widget changed in the same idle cycle is not re-tagged.
  • The per-widget flag __tk_text_rtl_auto_$w is a global variable that is never removed, so a recreated widget with the same path is not scanned on <Map>.

Only 9.1 is affected. This is a different problem from [1348333fff].

User Comments: nab added on 2026-10-13 04:14:18:

Merged into trunk, [3a6ffd3b98|3a6ffd3b98]. Kevin Walzer, who wrote the RTL auto-detection, gave his green light. Thanks Serhiy for combining the two branches.


nab added on 2026-10-12 14:21:53:
Hi,
it would be very nice to have this one on trunk.

many thanks,
nicolas

serhiy.storchaka added on 2026-10-08 14:10:34:

Combined with Nicolas's nab-text-rtl-scan (from [3d6de81648]) in branch text-rtl-detection, [c47ce655fd]:

  • the text is searched once by the widget (search -all -regexp, with Unicode white space listed so that a match does not run into the next lines), and the tk_rtl_right tag is changed only where it differs;
  • RTL characters are all characters with Bidi_Class R or AL (::tk::RtlChars), used by the search, the line pattern and tk::TextIsRtlLine; U+FEFF is excluded;
  • the pending flag is per widget; a text without the tag is checked when mapped, so no <Destroy> binding is needed.

Tests text-46.1 to 46.6 and textTag-19.1 to 19.5 (the duplicates of text-46.2 and 46.3 removed). They pass on X11 and Windows. A keystroke in 10000 lines takes 2 ms (Latin), 15 ms (Hebrew) and 8 ms (mixed), instead of 150, 79 and 71 ms on main (X11).


serhiy.storchaka added on 2026-09-29 07:58:21:

Attached a sample script. Results on Linux/X11 (Xvfb):

9.09.19.1 with the fix
map 900 texts with 300 lines708 ms5351 ms662 ms
map 1 text with 100000 lines1 ms1079 ms14 ms
keystroke in 5000 Hebrew lines0 ms30 ms12 ms
two texts changed in the same idle cycle are tagged-only the firstboth
recreated text is tagged on map-noyes


serhiy.storchaka added on 2026-09-24 11:11:59:

Fix on text-rtl-detect-speed: skip the scan if the text contains no RTL characters, remove the tag once instead of line by line, and check lines with regular expressions. The pending and auto-detection flags are now per widget and are removed on <Destroy>. Tests textTag-19.1 to 19.4.

RTL characters now include all characters with Unicode Bidi_Class R or AL (Unicode 18): besides U+0590–U+08FF, also U+200F, the Hebrew and Arabic presentation forms, and U+10800–U+10FFF and U+1E800–U+1EFFF (e.g. Adlam).

Mapping 900 text widgets with 300 lines now takes 48 ms instead of 2.1 s. A keystroke in a 5000-line Hebrew text takes 12 ms instead of 30 ms: text with RTL characters is still rescanned in full after each insert. A more complete fix would rescan only the changed lines; this is left for a follow-up.


Attachments: