Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Are you sure that doesn't make a copy?

Yes. Quite sure. I have two independent demonstrations for proof of that.

  1. Using Devel::Peek::Dump():

    This is not a Perl-level 'aliasing' effect; it is a C-level pointer swap, so you're looking at the wrong thing. It isn't the Perl reference values you should be considering, but rather the PV component of the two scalars as shown below.

    Note that whilst the the two scalars have different heads and bodies (the two hex values on the first line of each dump); the address of the actual data on the fourth line of each dump is identical in both:

    use Devel::Peek;; $h{ XXX } = 'test';; Dump $h{ XXX };; SV = PV(0x2ab2a0) at 0x3dc99d8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x3e28f98 "test"\0 CUR = 4 LEN = 8 $a[0] = delete $h{ XXX };; Dump $a[ 0 ];; SV = PV(0x2ab2b0) at 0x3dc9978 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x3e28f98 "test"\0 CUR = 4 LEN = 8

    Contrast that with a normal assignment where all three hex values in the scalar Dump()s are different:

    use Devel::Peek;; $h{ XXX } = 'test';; Dump $h{ XXX };; SV = PV(0x11b2a0) at 0x3e199d8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x3e78f98 "test"\0 CUR = 4 LEN = 8 $a[0] = $h{ XXX };; Dump $a[ 0 ];; SV = PV(0x11b2b0) at 0x3e19978 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x3e78f68 "test"\0 CUR = 4 LEN = 8
  2. Using empirical observation.

    In the trace below mem is a function that returns the process memory utilisation in K bytes.

    • When the REPL session has just started, the memory used is just under 10MB.
    • After I create the hash key with a 100e6 byte value, the memory has grown to 107MB.
    • After I transfer that value to the array element, the memory has change by a few kb, but is still basically the same 107MB.
    C:\test>p1 Perl> print mem;; 9,340 K Perl> $h{ XXX } = 'X' x 100e6;; Perl> print mem;; 107,248 K Perl> $a[ 0 ] = delete $h{ XXX };; Perl> print mem;; 107,308 K Perl>

    If the data had been copied, the footprint would have been over 200MB.

    (I cannot Dump() the scalars in this latter case because Dump() would spend a week trying to format a 100e6 byte string nicely, before dumping it to the console, which would take another week (or two:)!)

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". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: push to array without copying by BrowserUk
in thread push to array without copying by chris212

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-08-18 01:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found