Skip to content
Tcl Source Code Changes To Faster UTF encoding
Login

Changes To Faster UTF encoding

Changes to "Faster UTF encoding" between 2026-02-24 15:49:20 and 2026-02-25 07:57:24







1



2


























3

4
5
6
7
8
9
10
1
2
3
4
5
6

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

37
38
39
40
41
42
43
44
+
+
+
+
+
+
-
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+







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.
Explanatory text forthcoming...

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.

Benchmarks (times in microseconds)
(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

︙
29
30
31
32
33
34
35




















































































63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 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
        }
    }
}
```