I was doing some research for a talk I'm doing here at work on how the using 'use constant' works at compile time and how the interpreters does substitutions. I've always been under the assumption that when you have a boolean constant say
use constant DEBUG => 0;
and if you use that in an if statement, then the entire if block will get compiled out if its false with no penalty at run time. I wanted to show how this works in my presentation and decided to run this sample code through B::Deparse to show how its optimized
use strict; use warnings; use constant DEBUG => 0; print "Hello World\n"; print "This is a debug Statement\n" if DEBUG; print "Goodbye Cruel Word\n";
I was suprised when I got this from the output of perl -MO=Deparse
use constant ('DEBUG', 0); use warnings; use strict 'refs'; print "Hello World\n"; '???'; print "Goodbye Cruel Word\n";
I thought to myself, "WTF is this '???'; string in void context, I assumed it would just remove the line". Now this isn't a huge deal because a string in void context is infinitely faster that an if statement, but I was surprised to see it. So surprised that I decided to bench it.
use strict; use warnings; use Benchmark qw( cmpthese ); use constant DEBUG => 0; my $b = 100; cmpthese -10, { constant => sub { my $a = 10 * $b; $a = 10 * $b if DEBUG; }, noconstant => sub { my $a = 10 * $b; }, };
And was surprised by the results...
Rate constant noconstant constant 1498536/s -- -17% noconstant 1796946/s 20% --
I mentioned my findings at our Perl Monger meeting last night and Rocco Caputo was also under the same assumption that there was zero penalty for using constants and they were compiled out.

Of course the penalty is _very_ small, but what's the point of the '???'; and why not just compile it out as I've heard over and over - the big benefit of using optimized subs as constants (use constant DEBUG => 1; or sub DEBUG () { 1 })

I also noticed that instead of ???; sometimes it gets compiled down to 0;

-biz-


In reply to use constant and Compiler Optimizations by jbisbee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.