14

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?

Community
  • 1
  • 1
Zaid
  • 36,680
  • 16
  • 86
  • 155
  • Not sure why there's not much documentation on the subject, but since the compiler is open source you could just check out the code and see the optimizations first hand. – Polynomial Nov 01 '11 at 20:17
  • 9
    Implementation details don't belong in perldoc because they can change even when the underlying behavior of the language doesn't change. These details are often interesting, but the perl docs are already too large. – mob Nov 01 '11 at 20:51
  • 1
    I agree that something like this might be nice. as mob says, the problem is that they might change. I know that another nice optimization is that `map` in a scalar context does not generate a return array (which would be ignore). These things would be nice to know, without having to collect them along the way, at least for common idioms and common optimizations. – Joel Berger Nov 01 '11 at 21:52
  • @daxim : Thanks for putting a bounty on this question. I should have done so before. – Zaid Nov 08 '11 at 07:09

5 Answers5

9

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
8

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.

daxim
  • 39,270
  • 4
  • 65
  • 132
hobbs
  • 223,387
  • 19
  • 210
  • 288
  • Of course, you could also feed any answers you get to interesting questions back into StackOverflow — or bribe the people with the answers to start posting them on SO directly :) – hobbs Nov 02 '11 at 04:52
  • Actually I was thinking of offering a bounty for this question so that those with the answers could pour in their knowledge :) – Zaid Nov 02 '11 at 06:49
1

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

Torst
  • 11
  • 1
  • This doesn't answer the big question: how and where can I learn more about the Perl optimizer? It's simply restating what the optimizer does with `reverse sort` and provides a link about it. – Zaid Nov 09 '11 at 08:03
1

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.

Alex
  • 5,863
  • 2
  • 29
  • 46
0

The B::Deparse module might give something interesting:

$ perl -MO=Deparse -e 'reverse sort { $a <=> $b } @array'
reverse +(sort {$a <=> $b} @array);
-e syntax OK

(For some reason, the reverse optimization doesn't show here.)

tuomassalo
  • 8,717
  • 6
  • 48
  • 50
  • This is a question, not an answer – Zaid Nov 12 '11 at 20:10
  • Sorry. I tried to give a hint about using the `B::Deparse` module as a way of learning something about the perl optimizer. – tuomassalo Nov 12 '11 at 21:11
  • Well if the `reverse` optimization doesn't show here, I'm not sure about how useful `B:Deparse` is for the needs of this question – Zaid Nov 13 '11 at 06:02
  • As I said, this method _might_ give something interesting. Your question was about perl's optimizations in general, not just this `reverse` case. This method does reveal _some_ optimizations, as well as cases where they don't happen for non-trivial reasons, such as possible operator overloading. Try `perl -MO=Deparse -e 'print 1+2+$a'` and `perl -MO=Deparse -e 'print $a+1+2'` for some trivial examples. – tuomassalo Nov 13 '11 at 11:58