Skip to content
Tcl Source Code History Of Ticket 6811a0081940b76c
Login

History Of Ticket 6811a0081940b76c

Artifacts Associated With Ticket 6811a0081940b76c

  1. Ticket change [e9675b8513] (rid 223400) by anonymous on 2024-03-19 21:31:16:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-plain"
    4. comment initialized to:
      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."
      
    5. foundin initialized to: "8.6.11"
    6. is_private initialized to: "0"
    7. login: "anonymous"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "084881249f6efe07e05d28fe9007e25c423f48c1"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "anonymous"
    14. subsystem initialized to: "17. Commands I-L"
    15. title initialized to:
      lsearch performance degradation on Tcl 8.6.11 release
      
    16. type initialized to: "Bug"
  2. Ticket change [9754a0737a] (rid 223549) by sebres on 2024-03-20 15:51:29:

    1. icomment:
      My few cents for the issue:
      
      1. it seems to affect <b>-glob</b> search (as its docu says "If option is omitted then it defaults to <b>-glob</b>"), so definitely affected by changes of <i>Tcl_StringCaseMatch</i> 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:
      
      <code><pre style="padding-left:10pt">
        % 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
      </pre></code>
      (as one can see, the second measurement with <b>lsearch -exact</b> is indeed not affected.)
      
      3. Viewing the found commit [26e57ca1486e497b], it is striking the usage of 
      <i>TclUtfToUCS4</i> instead of <i>TclUtfToUniChar</i> en masse. However <i>TclUtfToUniChar</i> was a macro (quasi inline function), where <i>TclUtfToUCS4</i> is an internal function (not even static, so wouldn't be inlined in -O2) and it also internally uses <i>Tcl_UtfToUniChar</i> in opposite to <i>TclUtfToUniChar</i> (with the same consequences), what can theoretically explain this 2x + 2x degradation.
      
      I'll try to fix it now.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  3. Ticket change [883e7476db] (rid 223560) by sebres on 2024-03-20 18:01:57:

    1. closedate changed to: "2460390.25136333"
    2. closer changed to: "sebres"
    3. icomment:
      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.
      
    4. login: "sebres"
    5. mimetype: "text/x-fossil-wiki"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
  4. Ticket change [5cd790fb91] (rid 223669) by jan.nijtmans on 2024-03-21 14:18:34:

    1. closedate changed to: "2460391.09622801"
    2. closer changed to: "jan.nijtmans"
    3. icomment: "Now also forwared to 8.7 and 9.0. Thanks!"
    4. login: "jan.nijtmans"
    5. mimetype: "text/x-fossil-wiki"