I was wondering whether undeffing "chaff" in the CLONE subroutine to save
memory would make sense. The test program is the same as the last
benchmark. But in half of the benchmarks a CLONE subroutine was added that
would undef() all of the variables.
==========================================
use threads ();
use threads::shared ();
use Benchmark;
%hasha = (1..5000);
%hashb = (1..5000);
%hashc = (1..5000);
%hashd = (1..5000);
%hashe = (1..5000);
%hashf = (1..5000);
%hashg = (1..5000);
%hashh = (1..5000);
%hashi = (1..5000);
%hashj = (1..5000);
warn "hashes initialized\n";
for ( my $i=1; $i <= 10; $i++ ) {
my $t0 = new Benchmark;
threads->new( sub {} );
my $t1 = new Benchmark;
my $td = timediff( $t1,$t0 );
warn timestr( $td )."\n";
warn "thread $i added\n";
}
warn "threads started\n";
sub CLONE {
undef( %hasha );
undef( %hashb );
undef( %hashc );
undef( %hashd );
undef( %hashe );
undef( %hashf );
undef( %hashg );
undef( %hashh );
undef( %hashi );
undef( %hashj );
}
====================================
Again, I ran this script for the variable permutations that would make
sense (the non-unique and non-shared ones) and recorded the usr time and
wallclock time from Benchmark, as well as memory size from "top". I
ordered these in ascending usr time.
number of
threads 1 2 3 4 5 6 7 8 9 10
----------------------------------------------------------------------------
------
type of
variable usr usr usr usr usr usr usr usr usr usr
SIZE wall wall wall wall wall wall wall wall
wall wall
----------------------------------------------------------------------------
------
%hash 0.56 0.53 0.55 0.55 0.60 0.58 0.59 0.62
0.62 0.62
32576 0 1 1 0 1 0 1 1
0 1
%hash with
CLONE 6.25 6.68 7.04 7.41 7.62 7.58 8.00 8.15 8.25 8.36
30004 7 7 7 7 8 8 9 8
9 8
----------------------------------------------------------------------------
------
our
%hash 0.58 0.56 0.58 0.58 0.58 0.58 0.60 0.58 0.59 0.57
32600 1 0 1 0 1 1 0 1
1 0
our %hash with
CLONE 0.60 0.62 0.60 0.63 0.64 0.65 0.66 0.65 0.65 0.64
29548 0 1 1 0 1 1 0 1
1 0
----------------------------------------------------------------------------
------
my %hash 13.91 14.26 15.61 16.47 16.99 17.50 17.93 18.34
18.69 18.91
40408 13 15 16 17 18 18 18 19
20 19
my %hash with CLONE 13.13 14.60 15.88 16.70 17.40 17.92 18.33 18.31
17.33 18.81
36860 13 15 16 17 18 18 18 19
20 19
----------------------------------------------------------------------------
------
Conclusions:
- undeffing doesn't add much CPU in the "my" or "our" case. But it _does_
add a lot in the unqualified globals case. I wonder why...
- memory savings are only about 10%, a lot less than what you would expect.
So, if you do have the possibility to undef stuff that you don't need in a
thread, it _does_ make sense to do that if you are interested in as low a
memory footprint as possible. The extra CPU usage is minimal (except for
the unqualified globals case). However the savings will only be about 10%
(apparently).
Again, I hope this gives some food for thought and ideas to people...
Liz