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.


In reply to Re^5: Memory usage double expected -- further questions (deparse) by dave_the_m
in thread Memory usage double expected by sectokia

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.