Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2014-08-28
09:13 • Closed ticket [dfccbeb623]: lappend very slow in tcl 8.6 plus 7 other change... artifact: 52f11052f0 user: dkf
2014-07-18
12:19 • Closed ticket [b43f2b49f7]: Non-linear lappend performance plus 7 other chan... artifact: 5d4cf3c23b user: dkf
12:18
[b43f2b49f7] New compilation strategy for lappend that allows multi-value lappend to not have quadra... check-in: 17150b71d2 user: dkf tags: trunk
12:01 • Ticket [b43f2b49f7] Non-linear lappend performance status still Open with 4 ... artifact: d96737365a user: dkf
2014-07-16
19:51 • Ticket [b43f2b49f7]: 3 changes artifact: 3bffa62f52 user: dgp
2014-07-15
22:50 • Ticket [b43f2b49f7]: 3 changes artifact: a86f05a5fd user: aku
22:25 • Ticket [b43f2b49f7]: 4 changes artifact: 9f815a8a3a user: dkf
22:21 • Ticket [b43f2b49f7]: 4 changes artifact: 4f048532ae user: dkf
15:50 • Ticket [b43f2b49f7]: 3 changes artifact: 24f338b622 user: dgp
15:33 • Ticket [b43f2b49f7]: 3 changes artifact: a5b9b09fab user: dgp
14:26 • Ticket [b43f2b49f7]: 3 changes artifact: 7fa08d0330 user: dgp
14:09 • Ticket [b43f2b49f7]: 4 changes artifact: 1e96bc57ff user: dkf
12:04 • Ticket [b43f2b49f7]: 3 changes artifact: 63558f473e user: dgp
2014-07-13
11:24 • Ticket [b43f2b49f7]: 4 changes artifact: 28ad0c8e85 user: dkf
11:23 • New ticket [b43f2b49f7]. artifact: a1239f3c47 user: dkf

Non-linear lappend performance

Closed / Fixedb43f2b49f75ec8b60c7e5b59a21556528657e77aCreated 12 years agoUpdated 12 years ago
Descriptionhtml2014-07-13 11:23:18

This code (in a procedure):

lappend someVar $a $b
does not scale linearly (but rather quadratically) with the length of someVar. This is very bad. 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 Tcl_Obj.

Referenced by2
  • 52f11052f0Closed ticket [dfccbeb623]: lappend very slow in tcl 8.6 plus 7 other changes ticket2014-08-28 09:13:17
  • 17150b71d2[b43f2b49f7] New compilation strategy for lappend that allows multi-value lappend to not have quadratic performance (through better reference management). check-in2014-07-18 12:18:44
dkf Ticket [b43f2b49f7]: 4 changes [History diff]2014-07-13 11:24:30
dgpplain2014-07-15 12:04:44
Any idea yet whether this represents a regression?
dkfhtml2014-07-15 14:09:13

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.

The workaround is to use two lappends, which is fast.

dgpplain2014-07-15 14:26:32
So this is not the same issue as

http://paste.tclers.tk/3224

then?
dgpplain2014-07-15 15:33:33
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]
dgpplain2014-07-15 15:50:12
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.
dkfhtml2014-07-15 22:21:18

By linear, I mean that the overall time should be linear in the number of items, i.e. O(1) lappend. What we've got isn't.

Using the ActiveTcl distro of 8.6.1 (not to pick on them, but it's common in the wild) I see:

% 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
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 lappends. (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.)

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 in silico 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 append-based string code instead (taking the processing time to around a minute on the same input). Switching to using single-value lappends accelerated the code to where it took 5-10 seconds…

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.)

dkfhtml2014-07-15 22:25:30

I don't know if http://paste.tclers.tk/3224 describes the same problem. If it does, the multi-lappend is buried inside a package.

# 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

akuplain2014-07-15 22:50:08
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.
dgpplain2014-07-16 19:51:42
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.
dkfhtml2014-07-18 12:01:52

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.)

dkfhtml2014-07-18 12:19:29

Committed. Passes test suite completely, both with and without full memory debugging.

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