D 2026-02-25T07:58:38.073 L Faster\sUTF\sencoding N text/x-markdown P 5523e62e05293dc2b84b884a2312488b687a8bf4c2f84a024311fa96ddc7980b U apnadkarni W 6012 The [apn-utf-fastpath](https://core.tcl-lang.org/tcl/timeline?r=apn-utf-fastpath) branch introduces some changes to the `UtfToUtfProc` function for improved performance. This function is called to convert standard UTF-8 to and from Tcl's internal modified UTF-8. It is invoked through the `encoding convert*` commands or by channel I/O when the channel encoding is set to utf-8 (which is almost always the case on modern systems). Channels configured for other encodings are unaffected. The benchmarks below are based on the following categories of languages with proportion of ASCII in the content based on the language (as per the Internet) * English - 100% ascii * West European - 95% ASCII * Cyrillic - < 20% ASCII (mostly whitespace and punctuation) * Far East - 0% ASCII The benchmark table shows timings when run against content based on the statistics above as well as pathological worst case where ASCII characters alternate with non-ASCII characters. As shown in the table, * English (pure ASCII) timing is reduced by 75% * West European timing is reduced by 50% * Cyrillic is more or less unchanged * Far East is reduced by 10% On the flip side, for pathological cases timings are increased by 11-25% depending on the language and operation. **Given that the pathological cases are very unlikely to occur in practice and the marked improvements for the likely statistical distribution of characters, it is proposed that the changes be adopted.** ### Benchmarks The I/O benchmarks reflect only the time for encoding, not Tcl command, channel overhead etc. (times in microseconds) #### [apn-utf-fastpath](https://core.tcl-lang.org/tcl/timeline?r=apn-utf-fastpath) branch... ``` c:\src\tcltk\wip\tcl\win>Release_AMD64_VC1944\tcltest91.exe utfbench.tcl 10000 Calibrating...0.03633541899879032 µs/#-overhead 0.037819 µs/# 51613951 # 26441573 #/sec convertto convertfrom puts read ascii (10000) 6.721 6.587 6.055 6.881 western (10000) 13.247 13.323 12.536 13.921 cyrillic (10000) 65.343 70.621 63.727 72.637 eastern (10000) 81.155 88.541 79.834 91.147 western - pathological (10000) 60.158 61.249 59.822 62.990 cyrillic - pathological (10000) 59.327 60.862 57.919 66.279 eastern - pathological (10000) 64.834 67.425 63.501 69.091 ``` #### Current trunk ``` c:\src\tcltk\main\tcl\win>Release_AMD64_VC1944\tcltest91.exe ..\..\..\wip\tcl\win\utfbench.tcl 10000 Calibrating...0.0345694057395217 µs/#-overhead 0.035182 µs/# 55482315 # 28423317 #/sec convertto convertfrom puts read ascii (10000) 22.834 22.802 22.279 23.333 western (10000) 25.434 25.694 24.925 26.320 cyrillic (10000) 65.016 70.840 64.997 72.914 eastern (10000) 92.401 96.693 92.304 101.501 western - pathological (10000) 48.926 52.301 49.131 53.466 cyrillic - pathological (10000) 48.742 52.250 49.659 53.592 eastern - pathological (10000) 58.243 59.968 56.589 62.204 ``` The benchmark itself is below: ``` puts -nonewline "Calibrating..." flush stdout puts [timerate -calibrate {}] puts "" proc format_timerate {timerate} { return [format %12.3f [lindex $timerate 0]] } proc measure {lang len alternate} { global texts if {$alternate} { # Alternate ascii with language-specific character set text [string cat X [string index $texts($lang) end]] set label "$lang - pathological" } else { set text $texts($lang) set label $lang } set repetitions [expr {$len/[string length $text]}] set text [string repeat $text $repetitions] set utf8 [encoding convertto utf-8 $text] puts -nonewline [format "%-35s" " $label ($len)"] puts -nonewline [format_timerate \ [timerate {encoding convertto utf-8 $text}]] puts -nonewline \ [string cat \ " " \ [format_timerate [timerate {encoding convertfrom utf-8 $utf8}]]] if {[llength [info commands testchancreate]]} { set chan [testchancreate sink] fconfigure $chan -encoding utf-8 -buffering none puts -nonewline \ [string cat \ " " \ [format_timerate [timerate {puts -nonewline $chan $text}]]] close $chan set chan [testchancreate source $utf8] fconfigure $chan -encoding utf-8 -buffering none puts -nonewline \ [string cat \ " " \ [format_timerate [timerate {read $chan $len}]]] close $chan } puts "" } set lengths [expr {[llength $argv] ? $argv : [list 100 1000 10000]}] # Basic text patterns. Last character must be non-ASCII set texts(ascii) xy # Western Europe >95% ASCII set texts(western) 0123456789012345678\xe9 # Far Eastern, almost no ASCII set texts(eastern) \u4e4e # Cyrillic, 20% - mostly punctuation set texts(cyrillic) x\u0411\u0411\u0411\u0411 puts [format "%35s%12s %12s %12s %12s" " " convertto convertfrom puts read] # Measure "statistical sample" foreach lang {ascii western cyrillic eastern} { foreach len $lengths { measure $lang $len 0 } } # Measure "pathological" case where ascii,non-ascii alternate foreach lang {ascii western cyrillic eastern} { if {$lang ne "ascii"} { foreach len $lengths { measure $lang $len 1 } } } ``` Z 2419c3b08113d7503cf901dc81f380bd