oxone has asked for the wisdom of the Perl Monks concerning the following question:
It seems that it takes a long time to undefine a large hash on some operating systems. The following code illustrates the problem:
use strict; use warnings; # Create a large hash my %hash; my $count = 3_000_000; $hash{$count} = rand(10) while $count--; # Undefine it print "Undefining hash.\n"; undef %hash; print "Done.\n";
Now, on Windows this does what I'd expect: the time taken for the script to undefine the hash is minimal. However, on FreeBSD using Perl 5.8.8 the same script takes around 14 seconds to complete that 'undef'.
Searching on PM, there is some relevant discussion in this node, which notes "there is/was a malloc bug that led to slow destruction of big data structures".
My first question is: is this bug documented anywhere, in terms of which OS'es and Perls are affected, and exactly when the problem arises?
Second question: is this fixed in a version of Perl later than 5.8.8?
Final question: what are the possible workarounds?
From my own investigations, I can delay the problem to the end of the script by not undefining any large hashes, AND making sure all large hashes are globals (so they don't get undefined when they go out of scope at the end of a function block, for example). However on doing this (eg. by commenting out the 'undef' line in the script above), the problem still occurs in that there is a long delay after the script says "Done", but before it actually finishes.
I could combine this approach with the POSIX-based hack suggested here, thereby pushing the problem to the end of script execution, then skipping past Perl's own garbage collection with "POSIX::_exit(0);".
However, this all feels very hack-y, especially making all large hashes globals, which goes directly against the best practice of limiting scope of variables wherever appropriate.
Anybody know of any better workarounds?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bug when undefining a large hash
by moritz (Cardinal) on Aug 22, 2008 at 13:37 UTC | |
by oxone (Friar) on Aug 22, 2008 at 13:55 UTC | |
by dextius (Monk) on Aug 27, 2008 at 13:15 UTC | |
Re: Bug when undefining a large hash
by SuicideJunkie (Vicar) on Aug 22, 2008 at 13:32 UTC | |
by oxone (Friar) on Aug 22, 2008 at 13:50 UTC | |
Re: Bug when undefining a large hash
by FunkyMonk (Chancellor) on Aug 22, 2008 at 13:52 UTC | |
by oxone (Friar) on Aug 22, 2008 at 14:00 UTC | |
Re: Bug when undefining a large hash
by dHarry (Abbot) on Aug 22, 2008 at 13:26 UTC | |
by oxone (Friar) on Aug 22, 2008 at 13:46 UTC | |
by dHarry (Abbot) on Aug 22, 2008 at 13:55 UTC | |
Re: Bug when undefining a large hash
by aufflick (Deacon) on Aug 23, 2008 at 09:59 UTC | |
by ggvaidya (Pilgrim) on Aug 25, 2008 at 04:45 UTC |