Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and where can I learn more about the Perl optimizer?

Tags:

perl

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:

  1. In the absence of perldoc, what is the official resource to learn about such optimizations?

  2. Is there a reason why perldoc does not document these optimizations?

  3. What other commonly known optimizations are there?

like image 548
Zaid Avatar asked Nov 01 '11 20:11

Zaid


4 Answers

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.

like image 168
Eric Strom Avatar answered Nov 08 '22 02:11

Eric Strom


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.

like image 32
hobbs Avatar answered Nov 08 '22 04:11

hobbs


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

like image 1
Torst Avatar answered Nov 08 '22 03:11

Torst


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.

like image 1
Alex Avatar answered Nov 08 '22 03:11

Alex