Skip to content
Tcl Source Code History Of Ticket b43f2b49f75ec8b6
Login

History Of Ticket b43f2b49f75ec8b6

Artifacts Associated With Ticket b43f2b49f75ec8b6

  1. Ticket change [a1239f3c47] (rid 138754) by dkf on 2014-07-13 11:23:18:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/html"
    4. comment initialized to:
      This code (in a procedure):
      <pre>
      lappend someVar $a $b
      </pre>
      does not scale linearly (but rather quadratically) with the length of <tt>someVar</tt>. This is <i>very bad</i>. The problem is (probably) the use of a shared-list concatenation step internally (in order to get the trace semantics correct) instead of doing concatenation on an unshared <tt>Tcl_Obj</tt>.
      
    5. foundin initialized to: "8.6.1"
    6. is_private initialized to: "0"
    7. login: "dkf"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Severe"
    11. status initialized to: "Open"
    12. submitter initialized to: "dkf"
    13. subsystem initialized to: "47. Bytecode Compiler"
    14. title initialized to: "Non-linear lappend performance"
    15. type initialized to: "Bug"
  2. Ticket change [28ad0c8e85] (rid 138755) by dkf on 2014-07-13 11:24:30:

    1. assignee changed to: "dkf"
    2. login: "dkf"
    3. mimetype: "text/plain"
    4. priority changed to: "9 Immediate"
  3. Ticket change [63558f473e] (rid 138756) by dgp on 2014-07-15 12:04:44:

    1. icomment: "Any idea yet whether this represents a regression?"
    2. login: "dgp"
    3. mimetype: "text/plain"
  4. Ticket change [1e96bc57ff] (rid 138758) by dkf on 2014-07-15 14:09:13:

    1. icomment:
      Not sure; would need to compare timings with 8.5 (which doesn't byte-compile this case). However, I suspect this is a regression and a serious one.
      <p>
      The workaround is to use two <tt>lappend</tt>s, which is fast.
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  5. Ticket change [7fa08d0330] (rid 138759) by dgp on 2014-07-15 14:26:32:

    1. icomment:
      So this is not the same issue as
      
      http://paste.tclers.tk/3224
      
      then?
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  6. Ticket change [a5b9b09fab] (rid 138763) by dgp on 2014-07-15 15:33:33:

    1. icomment:
      What's the evidence for this?
      
      I'm not seeing any trouble with this,
      testing the branch tips of 8.5 and trunk:
      
      proc demo {} {
      set l [lrepeat 50000000 foo]
      lappend l bar baz
      }
      puts [tcl::unsupported::disassemble proc demo]
      puts Tcl:[package present Tcl]
      puts [time demo 1]
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  7. Ticket change [24f338b622] (rid 138764) by dgp on 2014-07-15 15:50:12:

    1. icomment:
      And also....
      
      shouldn't we be expecting constant performance
      from [lappend]?  Even linear fails to meet the
      promise I've understood from Tcl's efficient appends.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  8. Ticket change [4f048532ae] (rid 138766) by dkf on 2014-07-15 22:21:18:

    1. icomment:
      By linear, I mean that the overall time should be linear in the number of items, i.e. O(1) <tt>lappend</tt>. What we've got isn't.
      <p>
      Using the ActiveTcl distro of 8.6.1 (not to pick on them, but it's common in the wild) I see:
      <pre>
      % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x}}} 1000} 1000
      67.750023 microseconds per iteration
      % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x}}} 3000} 1000
      188.477614 microseconds per iteration
      % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x y z}}} 1000} 1000
      3220.6352030000003 microseconds per iteration
      % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x}}} 50000} 1000
      3301.317531 microseconds per iteration
      % time {apply {x {for {set i 0} {$i<$x} {incr i} {lappend l x y}}} 1500} 1000
      5049.768402 microseconds per iteration
      </pre>
      In short, the time to assemble a 3k-element list, 3 elements at a time, is more than 15 times slower than doing it by individual elements, and is in fact similar to assembling a 50k-element list by single <tt>lappend</tt>s. (Something is definitely going wrong, especially as the problem gets worse with longer lists; see the last timing run, which is also assembling a 3k-element list.)
      <p>
      The real "customer" code, shown to me at EuroTcl by Alexandru Dadalau (and which I can't reproduce here as it was tied to a complex product), had much larger lists; it was of a script that was processing a 3D geometry file so that <i>in silico</i> engineering simulations could be run on the tool parts described. (I think.) It was taking over 20 minutes to load a netlist when using a multi-value , and he had "optimized" it by using a chunk of horrible <tt>append</tt>-based string code instead (taking the processing time to around a minute on the same input). Switching to using single-value <tt>lappend</tt>s accelerated the code to where it took 5-10 seconds…
      <p>
      The lack of access to the original code shouldn't matter. The timing runs above reproduce the problem perfectly adequately. (My current near-trunk builds aren't suitable for timing problem hunting; they've got full debugging enabled and so take around 100 times longer anyway.)
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  9. Ticket change [9f815a8a3a] (rid 138767) by dkf on 2014-07-15 22:25:30:

    1. icomment:
      I don't know if <a href="http://paste.tclers.tk/3224">http://paste.tclers.tk/3224</a> describes the same problem. If it does, the multi-<tt>lappend</tt> is buried inside a package.
      <blockquote><pre>
      # Using the same tktable binary I see a huge speed difference between
      # 
      # tcl/tk 8.5.8 (all fine and good)
      #
      # and
      # 
      # tcl/tk 8.6.1 (waaay too slow)
      # 
      # Just try to scroll down.
      
      package require Tk
      package require Tktable
      
      proc cellcmd {row column} {
          return [string repeat "$row, $column " 100]
      }
      
      scrollbar .hs -orient horizontal -command ".table xview"
      scrollbar .vs -orient vertical -command ".table yview"
      
      table .table \
          -cols 20 \
          -rows 1000000 \
          -colstretchmode none \
          -xscrollcommand ".hs set" \
          -yscrollcommand ".vs set" \
          -command {cellcmd %r %c} \
          -usecommand 1 \
          -cache 0
      
      grid .table -row 0 -column 0 -sticky news
      grid .vs -row 0 -column 1 -sticky ns
      grid .hs -row 1 -column 0 -sticky we
      
      grid rowconfigure . 0 -weight 1
      grid columnconfigure . 0 -weight 1
      
      wm geometry . 1000x800
      </pre></blockquote>
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  10. Ticket change [a86f05a5fd] (rid 138768) by aku on 2014-07-15 22:50:08:

    1. icomment:
      For comparison, the times for the Tcl 8.5.7 I have/use around at the office are
      
       1000 =  1000*1   145.573 microseconds per iteration (a)
       3000 =  3000*1   427.491 microseconds per iteration (b)
       3000 =  1000*3   463.837 microseconds per iteration (c)
      50000 = 50000*1  7428.024 microseconds per iteration (d)
       3000 =  1500*2   612.606 microseconds per iteration (e)
      
      (b) and (c) are only slightly different, although (e) shows a pronounced uptick.
      Still, not as bad as shown for 8.6.1.
      
    2. login: "aku"
    3. mimetype: "text/plain"
  11. Ticket change [3bffa62f52] (rid 138797) by dgp on 2014-07-16 19:51:42:

    1. icomment:
      Trouble is in the lappendMultiple bytcode case from May 2013.
      
      It implements multivalue lappend via the LIST_CONCAT
      instructions, which is efficient so long as the value to
      append to is unshared.
      
      In this situation, it is not unshared.  The list variable
      holding the value holds one copy and another copy
      is on the stack for LIST_CONCAT to operate on.
      
      I suspect we could find a way to cheat and pull the
      value out of the variable, taking away the sharing,
      since we're about to overwrite it anyway.
      
    2. login: "dgp"
    3. mimetype: "text/plain"
  12. Ticket change [d96737365a] (rid 138869) by dkf on 2014-07-18 12:01:52:

    1. icomment:
      That's what I concluded too; I've been testing some replacement code. (Alas, I'm also handling a workshop so my time is fragmented, but I should be able to commit it later today.)
      
    2. login: "dkf"
    3. mimetype: "text/html"
    4. username: "dkf"
  13. Ticket change [5d4cf3c23b] (rid 138873) by dkf on 2014-07-18 12:19:29:

    1. closedate changed to: "2456857.01353038"
    2. closer changed to: "dkf"
    3. icomment:
      Committed. Passes test suite completely, both with and without full memory debugging.
      
    4. login: "dkf"
    5. mimetype: "text/html"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
    8. username: "dkf"