Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-26
11:01
Merge [9b8a66aff6923a69]: Fix [6811a00819]: lsearch performance degradation on Tcl 8.6.11 release (t... check-in: 9f27ea7880 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-03-21
14:18 • Ticket [6811a00819] lsearch performance degradation on Tcl 8.6.11 release st... artifact: 5cd790fb91 user: jan.nijtmans
2024-03-20
20:29
Fix [6811a00819]: lsearch performance degradation on Tcl 8.6.11 release (thanks, Sergey!) check-in: 9b8a66aff6 user: jan.nijtmans tags: trunk, main
20:09
Fix [6811a00819]: lsearch performance degradation on Tcl 8.6.11 release (thanks, Sergey!) check-in: 3186ba81a2 user: jan.nijtmans tags: core-8-branch
18:01 • Closed ticket [6811a00819]: lsearch performance degradation on Tcl 8.6.11 releas... artifact: 883e7476db user: sebres
17:56
optimize TclUtfToUCS4 for single code units (non high surrogates), especially for ascii; fixes perfo... check-in: ad35def80d user: sebres tags: core-8-6-branch
16:37
added performance regression tests for list facilities (initially only few lsearch cases, illustrati... check-in: cd1bb80fb9 user: sebres tags: core-8-6-branch
15:51 • Ticket [6811a00819] lsearch performance degradation on Tcl 8.6.11 release st... artifact: 9754a0737a user: sebres
2024-03-19
21:31 • New ticket [6811a00819]. artifact: e9675b8513 user: anonymous

lsearch performance degradation on Tcl 8.6.11 release

Closed / Fixed6811a0081940b76c438c22c55d1d3ec7ddf622d1Created 3 years agoUpdated 3 years ago
Descriptionplain2024-03-19 21:31:16
We have noticed certain situations where lsearch is much slower (4x or so? but depends on the size of the list) after going to Tcl 8.6.11.   Specifically, it affects long lists with big strings that are similar to the search key.

I did a fossil bisect and found this checkin on the 8.6 branch:

26e57ca1486e497b

comment:      More usage of TclUtfToUCS4(), so we can use the whole Unicode range better in TCL_UTF_MAX>3 builds. (user: jan.nijtmans)

Here is a script that will reproduce the issue:

http://paste.tclers.tk/5929 (script also included below)

The issue can be worked around by using -exact.  Perhaps there is an optimization opportunity?  We aren't searching for any sort of glob pattern, so perhaps, for big lists, the search item could be examined for any glob characters?


package require fileutil

#     Benchmark showing the impact on lsearch in Tcl 8.6.11
#
#     $ fossil info 26e57ca1486e497b
#     hash:         26e57ca1486e497b32e75a074c0ee81b62beefb8 2020-05-05 16:00:06 UTC
#     parent:       5d38a6b3c64572f808163ae7656333fea1e478a7 2020-05-05 15:56:14 UTC
#     child:        b275de7c6434b50adf9f27ff24d01bd972efa624 2020-05-06 08:15:49 UTC
#     merged-into:  b07935f03d33d1bdfb89a4b13f1fee914d78080d 2020-05-05 16:20:24 UTC
#     tags:         core-8-6-branch
#     comment:      More usage of TclUtfToUCS4(), so we can use the whole Unicode range better in TCL_UTF_MAX>3 builds. (user: jan.nijtmans)

#
#     $ /usr/local/tcl8.6.11/bin/tclsh8.6 lbench.tcl 
#     stringlist has 5000 items.
#     lsearch returned -1 took 2297 microseconds.
#     lsearch -exact returned -1 took 23 microseconds.
#     $ /usr/local/tcl8.6.10/bin/tclsh8.6 lbench.tcl 
#     stringlist has 5000 items.
#     lsearch returned -1 took 512 microseconds.
#     lsearch -exact returned -1 took 24 microseconds.
#
# 

set alphabet [ list X M R 0 1 2 3 4 5 6 7 8 9 / ]

# based on suchenwi's random string generator from the Tcl Wiki:
proc lpick L {lindex $L [expr {int(rand()*[llength $L])}]}
proc randomlyPicked { length {chars { X 0 1 2 3 4 5 6 7 8 9 / }} } {
    for {set i 0} {$i<$length} {incr i} {append res [lpick $chars]}
    return $res
}

set stringlist [ list ]

# Generate some repetitive random strings
# Start all strings with a common prefix
set string_prefix "X14/X33/X5/X1848/X1/X3/X1/X1/X1/X1/X8/X49/X34"

set numstrings 5000

for { set n 0 } { $n < $numstrings } { incr n } {
    set random_length [ expr { 5 + int(rand()*25) } ]
    set new_string "$string_prefix/[ randomlyPicked $random_length ]"
    lappend stringlist $new_string
}

# Search for a unique string that also has the same common prefix
set search_for "$string_prefix/M0"
puts "stringlist has [ llength $stringlist ] items."

set timer [ clock microseconds ]

set finder [ lsearch $stringlist $search_for ]

puts "lsearch returned $finder took [ expr { [ clock microseconds ] - $timer } ] microseconds."

set timer [ clock microseconds ]

set finder [ lsearch -exact $stringlist $search_for ]

puts "lsearch -exact returned $finder took [ expr { [ clock microseconds ] - $timer } ] microseconds."
Referenced by3
sebreswiki2024-03-20 15:51:29

My few cents for the issue:

1. it seems to affect -glob search (as its docu says "If option is omitted then it defaults to -glob"), so definitely affected by changes of Tcl_StringCaseMatch etc.

2. The regression is also obvious with much simpler test, here as diff between current 8.6 from core-repo and 8.6.9 (no speed degradation), and it's as correctly estimated above indeed 4x slower:

  % timerate -calibrate {}
  % set str [join [lrepeat 12 "XXX"] /]; set l [lrepeat 5000 $str]; llength $l
  % proc test {} {global l str; timerate { lsearch $l $str/M }}; test
- 2287.38 µs/# 438 # 437.18 #/sec 1001.874 net-ms
+ 546.060 µs/# 1832 # 1831.3 #/sec 1000.382 net-ms
  % proc test {} {global l str; timerate { lsearch -exact $l $str/M }}; test
- 8.060231 µs/# 123457 # 124065 #/sec 995.092 net-ms
+ 8.104484 µs/# 122794 # 123388 #/sec 995.182 net-ms
(as one can see, the second measurement with lsearch -exact is indeed not affected.)

3. Viewing the found commit [26e57ca1486e497b], it is striking the usage of TclUtfToUCS4 instead of TclUtfToUniChar en masse. However TclUtfToUniChar was a macro (quasi inline function), where TclUtfToUCS4 is an internal function (not even static, so wouldn't be inlined in -O2) and it also internally uses Tcl_UtfToUniChar in opposite to TclUtfToUniChar (with the same consequences), what can theoretically explain this 2x + 2x degradation.

I'll try to fix it now.

sebreswiki2024-03-20 18:01:57

Fixed in [ad35def80d7a25f8] (as minimalistic rewrite of intern TclUtfToUCS4 to a macro). This'd speed-up it for non surrogate chars 2x (and for ascii utf-chars ca. 4x) for whole core code-base.

Thus close it now.

jan.nijtmanswiki2024-03-21 14:18:34

Now also forwared to 8.7 and 9.0. Thanks!

Log in (anonymous is fine) to add a comment.