Tcl Source Code

View Ticket
Login
Ticket UUID: fa5c8a823085c0cd88bdd2d10a5c24f5ebb4bf07
Title: memory cost and execution time exploding of nested namespaces
Type: Bug Created on: 2017-05-18 05:49:19
Submitter: pooryorick Assigned to: sebres
Subsystem: 21. [namespace] Severity: Minor
Priority: 5 Medium Last modified: 2017-05-18 08:12:41
Status: Open Closed by: nobody
Resolution: None Closed on:
Version: >= 8.5
Description:

The following script consumes at least 1GB of memory:

for {set i 0} {$i < 10000} {incr i} {
    set name [namespace eval ${name}[set name {}]::namespace$i {
        namespace current
    }]
}

It seems that the fully-qualified name of each created namespace is getting stored somewhere.

User Comments:
sebres added on 2017-05-18 07:26:06:

Seems to affect 8.5 and 8.6 in same manner also.

Additionally it've very large execution time exploding. Just as comparison to not-nested (>= 8.6, x86):

# not nested:
set i 0; time {namespace eval ::namespace[incr i] {}} 10000
----> 4.74 microseconds per iteration (and 8.0MB memory)
# nested:
set name {}; 
set i 0; time {namespace eval [set name ${name}::namespace[incr i]] {}} 10000
----> 70110.60 microseconds per iteration (and 770.0MB memory)

This difference looks in 8.5 still many times worse and the execution time exploding is very grave (with the similar memory consumption like 8.6).

% # not nested:
% set i 0; time {namespace eval ::namespace[incr i] {}} 10000
2.91753049225468 microseconds per iteration
%
% # nested:
% set name {};
% # 1. thousand:
% set i 0; time {namespace eval [set name ${name}::namespace[incr i]] {}} 1000
1153.3163043544507 microseconds per iteration
% # 2. thousand:
% set i 0; time {namespace eval [set name ${name}::namespace[incr i]] {}} 1000
7032.770893726276 microseconds per iteration
% # 3. thousand:
% set i 0; time {namespace eval [set name ${name}::namespace[incr i]] {}} 1000
18984.08208553774 microseconds per iteration
%


sebres added on 2017-05-18 08:12:41:

The memory consumption going to large full-qualified namespace name. Following example explain it (has comparable memory consumption as "namespace eval"):

set name {};
set i 0; time {set ns($i) [set name ${name}::namespace[incr i]]} 10000

> pooryorick:

It seems that the fully-qualified name of each created namespace is getting stored somewhere.

Yes, see struct Namespace declaration "fullName" in tclInt.h. It is currently impossible without this member, because used everywhere and the auto-retrieving on demand would be admittedly possible, but will used almost immediately.

I'll take a look later what would be possible to optimize it.