in reply to Memory usage double expected

Hello all,

can someone explain this in a more layman way? I have still many doubts:

# wrong beaviour as it doubles the memory # first code of my previous post, very similar to the OP one my $x = 'a' x (2**30); # RIGHT beaviour, it does NOT double memory used # second code posted above my $x; foreach my $order ( qw(20 24 30 32) ){ $x = 'a' x ( 2 ** $order ); ... # RIGHT beaviour, even with my $x declared inside the foreach loop foreach my $order ( qw(20 24 30 32) ){ my $x = 'a' x ( 2 ** $order ); ...

In addition every perl I have atm ( strawberry portable: 5.26.0 5.22.3 5.24.2 5.26.2 ) I observe the same beahviours of the two above programs, ie. doubled and not doubled; I read also Linux users experience the same. So it must be something really bound to Perl itself and I'd like to know why and how to prevent this: a doubled memory footprint is not such a great feauture to have :)

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.

Replies are listed 'Best First'.
Re^2: Memory usage double expected -- further questions
by bliako (Abbot) on Oct 28, 2022 at 09:23 UTC
    (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.

      > 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.