Tk Source Code

View Ticket
Login
2026-10-13
04:14 • Closed ticket [5d983ebf]: Text widget: RTL auto-detection makes mapping and editing slow plus 7 other changes artifact: e85f19be user: nab
04:13
Fix [5d983ebf48] and [3d6de81648]: speed up the RTL auto-detection of the text widget and keep its pending state per widget. Merge text-rtl-detection (Serhiy Storchaka, with nab-text-rtl-scan): text-46.1 to 46.6, textTag-19.1 to 19.5. check-in: 3a6ffd3b user: nab tags: trunk, main
2026-10-12
14:21 • Ticket [5d983ebf] Text widget: RTL auto-detection makes mapping and editing slow status still Open with 6 other changes artifact: 9592b4c0 user: nab
2026-10-08
14:10 • Ticket [5d983ebf]: 3 changes artifact: ea35293e user: serhiy.storchaka
14:08
Fix [5d983ebf48] and [3d6de81648]: speed up the RTL auto-detection of the text widget and fix its per-widget state. Combines branches text-rtl-detect-speed and nab-text-rtl-scan. The text is searched once by the widget (search -all -regexp) instead of a Tcl loop on every line, and the tk_rtl_right tag is changed only where it differs. RTL characters now include all characters with Unicode Bidi_Class R or AL (without U+FEFF), not only U+0590-U+08FF. The pending flag is per widget, and a text recreated under the same name is checked when mapped. check-in: c47ce655 user: serhiy.storchaka tags: text-rtl-detection
13:24 • Closed ticket [3d6de816]: Text widget: RTL detection rescans the whole text at each keystroke plus 6 other changes artifact: e447be16 user: nab
13:18 • Ticket [3d6de816]: 3 changes artifact: fdd9373c user: serhiy.storchaka
2026-09-29
08:18
Add tests textTag-19.5 to 19.7 for all RTL character ranges, characters outside them, and punctuation and whitespace in RTL lines [5d983ebf48]. closed check-in: 28f9b9c3 user: serhiy.storchaka tags: text-rtl-detect-speed
07:58 • Ticket [5d983ebf] Text widget: RTL auto-detection makes mapping and editing slow status still Open with 3 other changes artifact: 9e03029a user: serhiy.storchaka
07:56 • Add attachment 5d983ebf48_sample.tcl to ticket [5d983ebf] artifact: 396850d7 user: serhiy.storchaka
2026-09-24
11:11 • Ticket [5d983ebf] Text widget: RTL auto-detection makes mapping and editing slow status still Open with 3 other changes artifact: bd6a6add user: serhiy.storchaka
10:46 • New ticket [5d983ebf]. artifact: 96f45d2d user: serhiy.storchaka

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: