garbage collection of eval'd subs is slow(?)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

I recently stumbled across this problem while using generated subs
(Perl v5.8.8):

for(;;) {
$sub = eval "sub { ... }";

The line containing the eval will take much longer (with about 15.000
s/// statements in the sub in my case) when the $sub is already
defined (from a previous iteration).

To put this into perspective - same "sub { ... }" definition (or
minimal differences, otherwise there would be no point in eval'ing
- if $sub is undefined: about 0.5s (first iteration)
- if $sub is defined (value from previous eval): about 35-40s

If the old $sub reference is kept alive (push @junk, $sub before it is
overwritten), the second case is as fast as it should be.

Conclusion: garbage collection (or whatever else happens when the old
sub reference is no longer used) of large subs (possibly only if they
contain many s/// statements - due to lack of time I did not test
other "large" subs) is extremely slow and the workaround is rather
ugly (= keeping them alive somewhere, wasting memory). This can affect
code that uses eval'd subs rather badly, so perhaps it should be


Site Timeline