in reply to Why is const x const not a const?

Do you mean: why isn't constant-folding applied to x?

It is odd that it doesn't. Maybe its weird «( EXPR ) x EXPR» syntax interferes? I doubt it, though. It was probably just overlooked.

Replies are listed 'Best First'.
Re^2: Why is const x const not a const?
by The Perlman (Scribe) on Jan 23, 2012 at 00:59 UTC
    Hmm...

    seems like I misunderstood the question, but maybe constant-folding something like "a" x 100000 is not always efficient?

      Then why does «sprintf '%-100000s', 'a'» create a string of 100,000 characters at compile-time like «"a" x 100_000» currently does at run-time.

      And then there's 1..100_000 that creates an array and 100,000 scalars at compile-time.

      If something could fold into something that's considered too large, that particular folding should be prevented, not all foldings of that operator.

        And then there's 1..100_000 that creates an array and 100,000 scalars at compile-time.

        It does? How can you tell?

        Neither B::Deparse nor B::Concise seem to indicate that, nor the compile time memory consumption.

        And then there's 1..100_000 that creates an array and 100,000 scalars at compile-time.

        Really? Can you demonstrate that?

        Because I see no evidence for it:

        c:\test>perl -le"++$n for 1..1e2; system qq[tasklist /nh /fi \"pid eq +$$\"] " perl.exe 7500 Console 1 5 +,716 K c:\test>perl -le"++$n for 1..1e3; system qq[tasklist /nh /fi \"pid eq +$$\"] " perl.exe 3676 Console 1 5 +,712 K c:\test>perl -le"++$n for 1..1e4; system qq[tasklist /nh /fi \"pid eq +$$\"] " perl.exe 6048 Console 1 5 +,732 K c:\test>perl -le"++$n for 1..1e5; system qq[tasklist /nh /fi \"pid eq +$$\"] " perl.exe 5648 Console 1 5 +,720 K c:\test>perl -le"++$n for 1..1e6; system qq[tasklist /nh /fi \"pid eq +$$\"] " perl.exe 8620 Console 1 5 +,736 K

        Whilst the memory usage varies a little, it sometime goes down a few k when the notional size of this array increases by an order of magnitude?

        Conversely, if you explicitly create an array of that size, the memory requirement increases by 100MB as expected:

        c:\test>perl -le"@a = 1 .. 1e6; system qq[tasklist /nh /fi \"pid eq $$ +\"] " perl.exe 2664 Console 1 109 +,576 K

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re^2: Why is const x const not a const?
by PerlGrey (Initiate) on Jan 24, 2012 at 17:03 UTC
    Yes, that's what I meant. Just didn't know the term constant-folding. It's even more weird because
    use constant FOOBAR => "a" x 3;
    does what one expects. So the expression definitely can be evaluated at compile time.
      ...the expression definitely can be evaluated at compile time.

      Depends on how you define "compile time" :)

      The constant pragma implies a BEGIN block, which has its own compile and execution time — with the execution happening right after the BEGIN block has been compiled, but before the rest of the code is being parsed and compiled.

      In other words,

      use constant FOOBAR => "a" x 3;

      is roughly equivalent to

      BEGIN { my $x = "a" x 3; *::FOOBAR = sub () { $x } }

      And when you investigate it with B::Concise, you'll see that the opcodes in question have just moved to a different place and execution time, but they're still there:

      $ perl -MO=Concise,BEGIN,-main -e'use constant FOOBAR => "a" x 3; prin +t FOOBAR' | grep -C 3 '"a' 14 <$> const[PV "constant"] sM ->15 15 <$> const[PV "FOOBAR"] sM/BARE ->16 18 <2> repeat[t1] sKM/2 ->19 # <--- 16 <$> const[PV "a"] s ->17 # <--- 17 <$> const[IV 3] s ->18 # <--- 19 <$> method_named[PV "import"] ->1a BEGIN 5: -e syntax OK -- 4i <;> nextstate(main 102 -e:1) v:{ ->4j 4l <@> print vK ->4m 4j <0> pushmark s ->4k 4k <$> const[PV "aaa"] s ->4l

      The mechanism to define constants via Constant Functions is rather different from constant folding in the sense of 42+99 being replaced with 141 at compile time.

      Consider the following

      $ perl -MO=Concise -e'use constant FOOBAR => scalar <STDIN>; print FOO +BAR' abc 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 102 -e:1) v:{ ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const[PV "abc\n"] s ->5 # <---

      Clearly, the value "abc" isn't part of the code, but entered via the keyboard at runtime (of the respective BEGIN block), yet it can be inlined as a constant in code sections compiled subsequently.

      So the expression definitely can be evaluated at compile time.

      Any expression can be evaluated at compile-time.

      use constant FOOBAR => time; say FOOBAR; sleep 2; say FOOBAR;
      1327438753 1327438753