Question

I was just reading this article from Gabor Szabo and he pointed out that Devel::Size reports that a simple sub {} is reported as requiring 8,516 bytes of space. Is this reported size correct? If so, why does Perl need to allocate so much space for a single empty subroutine?

Was it helpful?

Solution

$ perl -MDevel::Size=total_size -E'
   my $s = "x" x 100_000;
   my $x = \$s;
   my $y = \$s;
   say total_size($x);
   say total_size($y);
'
100048
100048

Does that mean that the size of $x and $y combined is 200KB? No. Same idea here.

It's not the size of the sub, but the size of the references, and everything it references, directly and indirectly.

$ perl -MDevel::Size=total_size -E'
    sub f { }   say total_size(\&f);
    ${"xxx"}=1; say total_size(\&f);
    ${"yyy"}=1; say total_size(\&f);
'
5847
5908
5969

As you can see, this is measuring more than just the sub. There appears to be a pointer to the sub's namespace.

OTHER TIPS

Devel::Size doesn't have easily understandable rules for what to include when measuring the size of a a complex item like a subroutine.

Devel::SizeMe is an experimental fork of Devel::Size which uses reference counting to decide what to include, so the result is much more understandable. It also includes ways to visualize the internal structure of the data.

You can find more information of Devel::SizeMe, including links to slides and screencasts, here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top