Skip to content
Tcl Source Code View Ticket
Login

View Ticket

freeing lists takes nonlinear time

Closed / Invalid735388ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 23 years ago
Descriptionplain2003-05-09 18:48:00
Freeing large arrays (or large numbers of arrays?)
takes non-linear time on linux:

% info patch
8.5a0
% for { set x 1 } { $x <  5} {incr x } {
     set data [list]
     set newcount [expr $x * 100000]
     for  {set i 0}  {$i <  $newcount } {incr i} {
            lappend data [ list $i.0 $i.1 $i.2 ]
      }
      puts -nonewline "listsize =  [llength $data]  "
      set unsetime  [ lindex [time { unset data }] 0 ]
      set unitTime [expr {$unsetime / $newcount}]
      puts " $unitTime us/item"
}
listsize =  100000   5 us/item
listsize =  200000   20 us/item
listsize =  300000   41 us/item
listsize =  400000   60 us/item

Notes:

1. This does *not* happen with the threaded allocator

2. The problem does not seem to appear in either W2000
or Solaris

3. It may be depend on the glibc/gcc version used: the
above is with gcc-2.96 and glibc-2.2.4 (RH 7.1 builds).
Rolf Ade reported linear time with gcc-3.2 and glibc-2.3.2

See also clt threads at
http://groups.google.com/groups?threadm=3EBBC0DC.7050805%40houston.rr.com
http://groups.google.com.ar/groups?threadm=vbngfpddpjr1a7%40corp.supernews.com
msoferplain2003-05-10 02:02:24
Logged In: YES 
user_id=148712

Don Porter sees the same problem both on i386 and alpha
platforms:
  Intel: glibc-2.2.4 on 2.4.10 kernel
  Alpha: glibc-2.1.3 on 2.2.23 kernel
mitchuplain2003-05-10 03:52:02
Logged In: YES 
user_id=775554

my Tcl and Wish are now  8.4.2
from the precompiled distribution available from  ActiveState,

 I get the same results as before,   so my glibc doesn't
matter I presume.

slackware linux 2.4.20    smp
twin Xeon-2.2   2.0 Gb   Ram

just for th erecord

glibc  2.2.5
gcc version 2.95.3 20010315 (release)
msoferplain2003-05-10 04:25:58
Logged In: YES 
user_id=148712

Roy Terry reports on clt that he sees the nonlinear
behaviour under win2k in tcl8.4.2 but *not* in tcl8.3.2
dkf Ticket [735388ffff]: 1 change [History diff]2003-05-12 15:51:28
dkfplain2003-05-12 15:51:30
Logged In: YES 
user_id=79902

Hmm.  Sounds to me like an unpleasant interaction between
the low-level memory management engine and Tcl.  AIUI, the
problem is that libc tries to free unused blocks of memory,
which is laudable but potentially very expensive in time
cost.  If the problem goes away with newer libc's the
resolution is 'upgrade libc' though that's a really major
undertaking... :^/

I see no way to avoid this BTW.  The cost is not in Tcl
itself and who knows how well the libc copes with things
like madvise()?

The difference between 8.3.2 and 8.4.2 seems strange to me.
dkfplain2003-05-12 15:54:09
Logged In: YES 
user_id=79902

Hmm.  Sounds to me like an unpleasant interaction between
the low-level memory management engine and Tcl.  AIUI, the
problem is that libc tries to free unused blocks of memory,
which is laudable but potentially very expensive in time
cost.  If the problem goes away with newer libc's the
resolution is 'upgrade libc' though that's a really major
undertaking... :^/

I see no way to avoid this BTW.  The cost is not in Tcl
itself and who knows how well the libc copes with things
like madvise()?

The difference between 8.3.2 and 8.4.2 seems strange to me.
dgp Ticket [735388ffff]: 1 change [History diff]2003-10-03 05:26:08
jenglishplain2004-02-21 08:15:24
Logged In: YES 
user_id=68433

This behaviour is symptomatic of all reference-counting
memory management schemes.  As long as the working set fits
in available RAM, everything is fine; but when you're
freeing a large number of blocks each call to
Tcl_DecrRefCount() may potentially cause a page fault. 
Locality of reference plays a large part too -- freeing 100
Tcl_Obj's in a row is very quick if they happen to be in the
same cache line, expensive if they happen to be in different
ones (and *very* expensive if they're paged out in different
pages).

FWIW, on my Fast Overpowered machine, (dual 1.6 Mhz Athlon,
256K cache, ~1Gb RAM), the above test takes ~0.8 us/item all
the way up to 900000 items; while on my Slightly Wimpy
machine (400 Mhz PIII, 60 Mb RAM) I get the following:

listsize = 100000  2.71754 us/item
listsize = 200000  2.62894 us/item
listsize = 300000  10.7855933333 us/item
listsize = 400000  49.1984825 us/item
listsize = 500000  45.585162 us/item
listsize = 600000  61.9319466667 us/item
listsize = 700000  67.7324 us/item
listsize = 800000  77.3131225 us/item
listsize = 900000  79.3220922222 us/item

Note that the time is relatively constant for the first two
iterations, jumps sharply, then stays relatively constant. 
Watching a 'gkrellm' monitor, the jump occurred almost
exactly at the time the RAM meter pegged out.

This has more to do with your CPU and cache size and how
many other processes are active on the system than it does
with glibc.
dkfplain2004-02-21 17:59:17
Logged In: YES 
user_id=79902

Perhaps we should make it easier for people to tune the
Tcl_Obj allocator for special applications?  Currently we're
allocating in blocks of 100 (look for symbol
OBJS_TO_ALLOC_EACH_TIME in tclObj.c) and it is quite
possible that setting that to a few thousand would be better
for apps that have heavy peak object use.
msoferplain2004-03-30 09:34:45
Logged In: YES 
user_id=148712

Software is getting better and faster! 

On the same hardware (but with added ram), running tcl8.5a2
(today's HEAD), gcc-3.3.3 and glibc-2.3.2 (debian unstable),
kernel 2.4.20 (debian unstable) I now get:
listsize = 100000  1 us/item
listsize = 200000  1 us/item
listsize = 300000  1 us/item
listsize = 400000  1 us/item

I guess we should close this as "not a tcl bug"? Or one that
is no more?
hobbsplain2004-03-31 00:18:24
Logged In: YES 
user_id=72656

My guess is that it was related to the old malloc
implementation of gcc (glibc).
jenglishplain2004-03-31 03:58:38
Logged In: YES 
user_id=68433

Just a followup for future reference  -- it's much more
likely that "(but with added ram)"  is the most important
factor.  The timing tests I ran used identical glibc
versions; the machine with less RAM exhibited the slowdown,
the one with more RAM did not.
Log in (anonymous is fine) to add a comment.