in reply to Re: Memory usage double expected -- further questions
in thread Memory usage double expected

(3) in the two snippets I posted here I cannot spot any real difference in the assignement but they behaves differently. Why?

$x = 'a' x (2**30); is different from $order=30; $x = 'a' x ( 2 ** $order );. The first one's RHS is considered a constant. The other's is not as it has that $order.

Replies are listed 'Best First'.
Re^3: Memory usage double expected -- further questions (deparse)
by LanX (Saint) on Oct 28, 2022 at 10:12 UTC
    > The first one's RHS is considered a constant

    here a little demo of the constant folding to make it clearer

    C:\tmp>perl -MO=Deparse -e "$_ =10; my $x = 'a' x 10" $_ = 10; my $x = 'aaaaaaaaaa'; -e syntax OK C:\tmp>perl -MO=Deparse -e "$_ =10; my $x = 'a' x $_" $_ = 10; my $x = 'a' x $_; -e syntax OK C:\tmp>

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      Thanks LanX,

      more words will be appreciated: do you mean in my $x = 'a' x (2**30) the right part RHS is folded by the constant folding optimization and.. it is bugged because it slurp memory twice?

      This double memory (let see if I understand it) is firstly allocated at compile time and then another time when $x is used at runtime?

      If so, then in  foreach my $order ( qw(20 24 30 32) ){ $x = 'a' x ( 2 ** $order ) there is no folding? Is because of this it does not slurp memory twice? I'd expected the folding happening 4 times (20 24 30 32) if this should be an optimization.

      This sounds really weird and bugged to me: if we spot this only with huge datastructures is only because it becomes noticeable but this will be true also for my $x = 'a' x (2**1) or (even for?) my $x = 42 ..really unexpected!

      Should I resuscitate ancient perl to see if it was always the same?

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        When both arguments of an 'x' operator are constant, perl evaluates that sub-expression at compile time and replaces the sub-expression with an opcode which returns a constant string. A block of memory will have been allocated to hold this constant string.

        So for example, $x = 'a' x 10 gets converted into $x = 'aaaaaaaaaa' at compile time, and a 10-byte block of memory will have been allocated at compile-time.

        At run time, that constant string is retrieved, and its value assigned to $x. At its most simple, $x gets a 10-byte buffer allocated, then the 10 bytes from the constant string "aaaaaaaaaa" are copied into $x's string buffer. So the program allocates 20 bytes in total. If repeated multiple times, e.g.

        sub foo { my $x = 'x' x 10; ... }
        Then on subsequent calls, the buffer for $x is re-used, so the total memory usage for the program doesn't increase beyond the 2 x 10 bytes already allocated.

        However, perl has a scheme called Copy-On_Write (COW). Often when a string is copied from one location to another, a second buffer isn't allocated; instead the string buffer is shared between the two scalars. Later, if one of the scalars is modified, the buffer is duplicated and the two scalars become independent of each other. COW normally works fine for constant assignments, but for some reason it isn't working when the constant is the result of compile-time constant-folding:

        $ perl -MDevel::Peek -e'my $x = "aaaaaaaaaa"; Dump $x' 2>&1 | grep COW FLAGS = (POK,IsCOW,pPOK) COW_REFCNT = 1 $ perl -MDevel::Peek -e'my $x = "aaaaa" . "aaaaa"; Dump $x' 2>&1 | gre +p COW $ perl -MDevel::Peek -e'my $x = "a" x 10; Dump $x' 2>&1 | grep COW
        This is probably a bug.

        Dave.

        You asked for the difference in code, and that's constant-folding here.

        But that's not per se a justification for missing COW.

        There might be other implementation details involved:

        • Dave_the_M mentioned "complex rules",
        • eyepops referenced a talk about efficiency considerations.

        • Or it's a bug,
        • or it's just a yet not implemented optimization feature.

        That's all hard to tell without being a full-time p5p.

        Sometimes things would make sense from a purist's point of view, but are just too obscure to justify the overhead to implement it.

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery