Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2023-09-15
01:03 • Closed ticket [f5c01e42a7]: Performance regression in 9.0 accessing dicts as lis... artifact: 912ee31b93 user: griffin
2023-08-21
00:32 • Ticket [f5c01e42a7]: 3 changes artifact: fa39a29458 user: griffin
2023-08-20
13:36 • Open ticket [f5c01e42a7]. artifact: ef2fa00bca user: apnadkarni
2023-08-17
21:13 • Closed ticket [f5c01e42a7]. artifact: 84df64e2e9 user: griffin
2023-08-16
22:30 • Ticket [f5c01e42a7]: 3 changes artifact: fe37f6c6e7 user: griffin
2023-08-10
09:48 • Ticket [f5c01e42a7]: 3 changes artifact: 70cbb15089 user: pointsman
2023-08-08
20:04 • Ticket [f5c01e42a7]: 3 changes artifact: 3779b1de5c user: pspjuth
06:08 • Ticket [f5c01e42a7]: 4 changes artifact: 89b5bcc916 user: apnadkarni
06:02
Bug [f5c01e42a7]. Disable DictAsListIndex for the time being. check-in: bd17e3ed36 user: apnadkarni tags: trunk, main
05:53 • Ticket [f5c01e42a7] Performance regression in 9.0 accessing dicts as lists s... artifact: fd7a61a0f0 user: apnadkarni
05:52 • New ticket [f5c01e42a7]. artifact: 537805bb98 user: apnadkarni

Performance regression in 9.0 accessing dicts as lists

Closed / Fixedf5c01e42a759e6a01329256ab4c21b4402719526Created 3 years agoUpdated 3 years ago
Descriptionmarkdown2023-08-08 05:52:06

Reported by Paul O. on the core mailing list:

Can anybody confirm the slow behaviour of the "array get" command in Tcl 9? Noticed on both Windows and Linux.

Regards, Paul

puts "Using Tcl [info patchlevel] on $tcl_platform(platform)"
catch { puts "Build-Info: [tcl::build-info]" }

set startTime [clock milliseconds]
for { set i 1 } { $i < 6000 } { incr i } {
     set arr(Key-$i) "Value-$i"
}
puts "Build time: [expr { [clock milliseconds] - $startTime }] msec"

set startTime [clock milliseconds]
set tmp [list]
foreach {key val} [array get arr "*"] {
     lappend tmp $val
}
puts "Get time  : [expr { [clock milliseconds] - $startTime }] msec"

exit 0
Referenced by1
apnadkarniplain2023-08-08 05:53:23
Confirmed the slowdown occurs even with an empty loop and with any dictionary (not just that returned by array get).
apnadkarniplain2023-08-08 06:08:38
Temporarily fixed in [bd17e3ed36] by NULL-ing DictAsListIndex from the Dict type dispatch table.

My opinion is that once list operations are used on a dictionary, it is reasonable and in fact appropriate to shimmer it. That's the general philosophy across all of Tcl's shimmering operation. It is the script responsibility to use the appropriate commands.

But may be it is possible to have the best of both worlds in this case if some use can be made of dict's list-like internal capabilities.

Leaving open and assigning to Brian for his opinion.
pspjuthplain2023-08-08 20:04:12
A main problem with the Index and Length functions are that they generate the string representation, which is *much* more expensive than shimmering.

I agree with Ashok's assessment that shimmering is the sensible option for those operations. Especially since dict-list shimmering is well optimised.
pointsmanplain2023-08-10 09:48:16
Disabling DictAsListIndex as done by Ashok with [bd17e3ed36] even
fixed a panic, seen by me. See [b6257b82833b] for the details.
griffinmarkdown2023-08-16 22:30:19

With some details added to the testcase:

puts "Using Tcl [info patchlevel] on $tcl_platform(platform)"
catch { puts "Build-Info: [tcl::build-info]" }

set startTime [clock milliseconds]
for { set i 1 } { $i < 60000 } { incr i } {
     set arr(Key-$i) "Value-$i"
}
set bt [expr { [clock milliseconds] - $startTime }]
puts "Build time: $bt msec"

set startTime [clock milliseconds]
set agx [array get arr "*"]
set at [expr { [clock milliseconds] - $startTime }]
puts "array get time  : $at msec"
puts agx-is-a:[lrange [tcl::unsupported::representation $agx] 1 3]

set startTime [clock milliseconds]
set llen [llength $agx]
set lt [expr { [clock milliseconds] - $startTime }]
puts "llength time: $lt msec"
puts llen=$llen
set startTime [clock milliseconds]
set tmp [list]
foreach {key val} $agx {
     lappend tmp $val
}
set gt [expr { [clock milliseconds] - $startTime }]
puts "Get time  : $gt msec"

exit 0

Part 1 fix improves llength operation:

# main code
#
Using Tcl 9.0a4 on unix
Build-Info: 9.0a4+35275c285b06f3932d83aadec6dd9df85dd34214ca0202316409e117b8abe496.compiledebug.debug.gcc-1104.memdebug.no-optimize
Build time: 1287 msec
array get time  : 160 msec
agx-is-a:is a dict
llength time: 80 msec
llen=119998
Get time  : 916 msec


# with dict llength function (branch [bug-f5c01e42a7])
#
Using Tcl 9.0a4 on unix
Build-Info: 9.0a4+840ebea1b54a1edbefb5f74244bee34c43afef69111ac4f12f0b4a165003bd1a.compiledebug.debug.gcc-1104.memdebug.no-optimize
Build time: 1035 msec
array get time  : 142 msec
agx-is-a:is a dict
llength time: 0 msec
llen=119998
Get time  : 499 msec

More to come...

griffinmarkdown2023-08-17 21:13:47

Shimmering is the best performance possible w/o major rewrite of dict.

The llength operation has been optimized by computing and caching the llength when the dict is created and modified. Having llength value available helps a bit in general.

Fixed:[2a01c62b1a]

apnadkarniplain2023-08-20 13:36:31
Brian,

Re-opening as the commit triggered failures in tcllib tests. Have reverted the commit on trunk for the time being so tcllib testing can proceed. You can revert the reversion (and a fix) if you think the optimization is worthwhile but my opinion is that use of a llength operation on a dictionary is more likely than not  followed by other list operations in the cases that are not programming errors so the shimmering will happen anyways. So it not clear to me that the specialization is worth the effort.
griffinmarkdown2023-08-21 00:32:10

Thanks Ashok! I agree, the benefit is limited, so the overall advantage is questionable.

Reviewing the changes, I think I see the problem. When I have some free time I will attempt a fix, just to satisfy my curiosity, but may not commit.

The lack of performance benchmarks makes this kind of thing tricking to analyze and validate.

griffinmarkdown2023-09-15 01:03:48

Closing this ticket. Current trunk is optimal. Will not "fix", i.e., implementing abstract list for dict beyond llength is not worthwhile.

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