Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Memory usage double expected -- further questions

by Discipulus (Canon)
on Oct 28, 2022 at 07:55 UTC ( [id://11147773]=note: print w/replies, xml ) Need Help??


in reply to Memory usage double expected

Hello all,

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

  • (1) $x = 'a' x (2**32) provokes the wrong behavior. I read RHS is compile-time constant so what I understand is that Right Hand Side expression is evaluated during compile time. But why doubles the memory used? And why 2**${\32} forces it to be posponed to runtime? And why the latter does not double the memory?
  • (2) COW aka Copy On Write should be the default in an assignement and this should prevent the memory to be doubled. So in this case is effectively a bug?
  • (3) in the two snippets I posted here I cannot spot any real difference in the assignement but they behaves differently. Why?

# 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 (Monsignor) 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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found