I am interested in learning more about behind-the-scene optimizations performed by Perl.
An example is where the optimizer treats reverse sort { $a <=> $b } @array;
as sort { $b <=> $a } @array;
It looks like good ol' perldoc
doesn't have anything on this subject.
A few questions here:
In the absence of perldoc
, what is the official resource to learn about such optimizations?
Is there a reason why perldoc
does not document these optimizations?
What other commonly known optimizations are there?
For things like this, you may find bits and pieces in the docs (particularly those dealing with the C api), but most of it is in the C source code itself.
For sort
, I believe the relevant function is S_simplify_sort
in op.c
The core of the optimizer is in Perl_peep
in the same file.
Some of it is documented in the perldeltas — anyone wanting to learn more about how Perl operates can gain some insight by reading them, starting with perl5004delta. Some of it is reasonably well commented in the code, and of course RTFS is another way to learn. A good source for places to start looking in the source is the section "Compiled Code" of perlguts, another doc worth reading.
Some, but by no means all, of it is documented in Chapter 18, "Compiling", of Programming Perl (yes, the 3rd edition really is 10 years old and covers 5.6.0 and/or 5.6.1, but the 4th edition is finally in the works!)
A lot of it, however, is documented nowhere, except possibly the mailing list threads where the feature was being discussed while it was under development. If you're interested in learning more about perl internals and maybe contributing a bit to the documentation, I'd suggest bringing questions up on the perl5-porters mailing list or the #p5p IRC channel, since that's where most of the perl-guts tribal knowledge resides.
I suspect that using the inbuilt sort without any comparison sub{} would be the fastest.
Hence, if you use a custom sub{} to encourage reverse sorting, it would slow down.
I also thought I has read that there is no longer a cost for doing 'reverse sort' - the compiler sorts it out for you, or at least reverses the result quicker than using a custom sub{}.
This older article on "A Fresh Look at Efficient Perl Sorting" seems to concur with these ideas:
http://www.sysarch.com/Perl/sort_paper.html
If you can get hold of a copy, and can cope with a description of a slightly older than current Perl (although much of this hasn't changed much), the book Extending and Embedding Perl might be helpful, it has chapters on the internals, optrees and some details of the optimizer.
Mostly, though, as other suggest, looking at the source, and playing with the appropriate B modules, is the best way to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With