Tcl Source Code

View Ticket
Login
Ticket UUID: c71eea294d41f03e61324a5d76f779f2a1fe19a3
Title: namespace delete is unbearably slow -- variable edition
Type: Bug Created on: 2023-01-13 03:45:45
Submitter: dmcooke Assigned to: nobody
Subsystem: 07. Variables Severity: Important
Priority: 5 Medium Last modified: 2023-01-13 03:45:45
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: 8.6.13
Description:

Using namespace delete on a namespace with many set variables appears to take time roughly quadratic in the number of variables.

proc create_variables {ns n} {
    namespace eval $ns {}
    for {set i 0} {$i < $n} {incr i} {
        namespace upvar $ns X$i x
        set x 0
    }
}
foreach n {100 1000 10000 100000 200000} {
    puts "n = $n"
    puts "Creating: [time {create_variables ::junk $n}]"
    puts "Deleting: [time {namespace delete ::junk}]"
}

These are the results on my machine, using tcl 8.6.13 from Debian unstable.

n = 100
Creating: 48 microseconds per iteration
Deleting: 26 microseconds per iteration
n = 1000
Creating: 280 microseconds per iteration
Deleting: 405 microseconds per iteration
n = 10000
Creating: 3445 microseconds per iteration
Deleting: 13388 microseconds per iteration
n = 100000
Creating: 33980 microseconds per iteration
Deleting: 1532539 microseconds per iteration
n = 200000
Creating: 73129 microseconds per iteration
Deleting: 13936896 microseconds per iteration

Using 8.7a5 shows little difference (although creating the variables is a bit slower).

When deleting 1 million variables, it'll take over 5 minutes for the namespace delete to complete.

This appears to have the same root cause as e76f0a6ca52a2ff3c2b1 (which is about large numbers of procs/commands slowing down namespace delete). Namely, deleting entries of a hash table that's being iterated over.

There is a workaround, however: unset the variables first.

    puts "Creating: [time {create_variables ::junk $n}]"
    puts "Unsetting: [time {unset -nocomplain -- {*}[info vars ::junk::*]}]"
    puts "Deleting: [time {namespace delete ::junk}]"

With this, for n = 200000, I get

Creating: 70431 microseconds per iteration
Unsetting: 100803 microseconds per iteration
Deleting: 465 microseconds per iteration

The deletion time still grows approximately quadratically, but with a constant so small that the Linux out-of-memory killer killed my X session while I testing. Indeed, for n = 30000000,

Creating: 10020193 microseconds per iteration
Unsetting: 13753545 microseconds per iteration
Deleting: 20398 microseconds per iteration
(after consuming 12 GB of memory).