I came across a rather cunning optimisation in perl 5.10 and it gave me a new respect for the hoops that perl jumps through to make the runtime more efficient.
What's the difference between these constructs (if $var1 and $var2 are lexicals)?:
sub { $_->{key1} == $var1 } sub { $_->{key2} == $var2 }
The answer is they're the same to perl. Despite the different fixed string and different variable name, both coderefs will end up as the same opcode tree, roughly looking like:
sub { $_->{$internal_sv_1} == $$internal_sv_2 }
Where the two internal SVs are stored on the scratchpad. Same opcode tree, different blanks.
Why does this matter? The beauty is that it doesn't, perl gets it right for you. I only came across it because of my hacking on my DBI layer that turns Perl into SQL. I had incorrectly cached the key and variable names during the parse and so looked up the wrong ones when given the second subref. By pointing to the scratchpad all was right with the world again.
In reply to Optimisation in 5.10 - clever by lukeross
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |