Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: What is the difference between the constant construct in Perl and the Readonly construct in Perl?

by diotalevi (Canon)
on Jul 06, 2006 at 01:26 UTC ( [id://559462]=note: print w/replies, xml ) Need Help??


in reply to What is the difference between the constant construct in Perl and the Readonly construct in Perl?

The author of Readonly has inspired my ire by neglecting to mention the positive reasons to use constant. I wrote a note at AnnoCPAN and I'm reproducing it for you here as well.

You didn't mention that the constant pragma allows the value to get inlined into the optree. You also didn't mention that when constants are used in conditionals, perl is able to optimize away the non-executable branches.

Further you didn't mention that constant.pm tracks perl's preferred implementation for constants. Prior to 5.9.4 constants were implemented as functions like sub FOO () { 1 }. After that perl does something a little bit different which allows for a smaller memory footprint.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: What is the difference between the constant construct in Perl and the Readonly construct in Perl?
by tinita (Parson) on Jul 06, 2006 at 08:35 UTC
    I agree. that was one of the few things I didn't like while reading PBP.
    To many people speed doesn't seem to be important. But if I hadn't used constant in my HTC module, it would probably be much slower than it is (I use a lot of constants there...)
    and I think at least in modules speed is a bit more important than in application code.
      From PBP page 58:
      If you decide not [to] use the Readonly module in production code (for performance or political reasons), then using constant is still better than using literal values.

      From that quote I get the impression that TheDamian is aware of the performance issues but takes for granted that others are not as familiar. Regardless however, the performance issue is mentioned in PBP (albeit briefly).

      Update: Fixed typos

Re^2: What is the difference between the constant construct in Perl and the Readonly construct in Perl?
by esskar (Deacon) on Jul 07, 2006 at 14:57 UTC
    well, i think speed is always an issue!

      While you're often right with this, and certainly in this case too (++), be careful not to generalise:

      In many other places, speed may not be so much of an issue =)

      Cheers, Sören

        In many other places, speed may not be so much of an issue

        Not only that, but what "speed" means can vary wildly. Performance is not a simple, single variable that can be tweaked at will. There are so many variables at play that any widely general rule is going to be wrong some time or another.

        Take this specific thread as an example. This is a classic case of micro-optimization. Certainly, using constant may be way, way faster than using Readonly. But what if the majority of an application's time is spent on network IO? Does the time it takes to resolve a constant really matter? It takes careful consideration and testing in the proper context to know for sure.

        To be clear, I'm not poo-pooing optimization by any means. I'm just trying to point out that it's important to optimize the right thing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://559462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found