Skip to content
Tcl Source Code History Of Ticket c71eea294d
Login

History Of Ticket c71eea294d

Artifacts Associated With Ticket c71eea294d

  1. Ticket change [7daca860d3] (rid 208144) by dmcooke on 2023-01-13 03:45:45:

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