Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 7daca860d32f66c46419fec4c9c195fe8e5b43eabc379c8e0faabbac5f5ffccb
Ticket: c71eea294d41f03e61324a5d76f779f2a1fe19a3
namespace delete is unbearably slow -- variable edition
User & Date: dmcooke on 2023-01-13 03:45:45
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-markdown"
  4. comment changed to:
    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](https://core.tcl-lang.org/tcl/tktview/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).
    
  5. foundin changed to: "8.6.13"
  6. is_private changed to: "0"
  7. login: "dmcooke"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "83fa3b1a2b6adef16d0b2b7db376346ca07759ef"
  10. resolution changed to: "None"
  11. severity changed to: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "dmcooke"
  14. subsystem changed to: "07. Variables"
  15. title changed to:
    namespace delete is unbearably slow -- variable edition
    
  16. type changed to: "Bug"