Given the following functionally similar Perl modules, I was curious what the internal Perl differences in execution were.
To recap – (from PerlDoc)
- Perl use – Imports some semantics into the current package from the named module – It is exactly equivalent to
BEGIN { require Module; Module->import( LIST ); }
- Perl require – demands that a library file be included if it hasn’t already been included. There is pseudocode there to suggest that a previously required module (successful or not) would just return the previous result
So given a set of modules that don’t define an import() function, one would expect that there would be little difference.
use Module | require Module |
main.pl
Module1::printit(); Module1.pm
BEGIN { print “ } Module2.pm
BEGIN { sub printit { 1; |
main.pl
MyBegin::printit(); Module1.pm
BEGIN { print “ sub printit { } Module2.pm
BEGIN { sub printit { 1; |
program output
|
program output
|
dtrace output
allocations / deallocations (complete program): time to run perl: 6389 uS (time on CPU 4177 uS) |
dtrace output
allocations / deallocations (complete program): time to run perl: 5368 uS (time on CPU 3814 uS) |
The (1/0) tuples in braces are counts of allocations and deallocations by the Perl interpreter in in that enter-return pair, and execution times are all using DTrace’s vtimestamp, so are adjusted for time on CPU.
This suggests to me that Perl use and require suffer from the same problems that are encountered when using #include’s inside header files in large scale C and C++, a massive, un-realized parser mess, due to the re-importation of dependencies for the short term convenience of the developer.
While the require output appears better, none the less (looking at the source code for Perl_ck_require) it does more than a nop the second time around (at minimum, its doing a single allocation, and converting the Module name to a File.pm). It is interesting (scary really) that the use case has many more allocations than the require case, but I presume that has to do with the sillyness of importing code into the same namespace several times. That main.pl’s BEGIN is parsed&allocated for 9 times, and Module2:BEGIN is parsed&allocated for twice – is not really what I had in mind.
For those of us working with large scale Perl code (in my case TWiki and Catalyst) it seems that there is not enough clarity, and there are opposing concerns for users that run Perl directly, incurring the parse and allocate steps each time, and those using accelerators such as mod_perl and speedy_cgi, who essentially start at the main-run phase (I think).
Reducing the above examples by removing the use or requires from everywhere except the top level main.pl, still shows an unexpected side effect that the use case has 34 more allocations and 26 deallocations – but… there is less going on. So it is possible, that the inconvenience of needing to manage the Perl module loading at the top level might be worth while, at least until Perl is changed to do what the ‘manual’ implies.
Another interesting side effect of replacing use with require, is that it moves the execution of the BEGIN from the parse phase to the execution phase – again reducing the effectiveness of Perl accelerators, but perhaps more inline with simplistic expectations.
Seems to me its time to move to bleadperl, and see if I can offer a fix.
The above tests are done using the dtrace enabled Perl 5.8.8 that is in my svn repository.
![[del.icio.us]](http://images.del.icio.us/static/img/delicious.small.gif)