in reply to Why is the size even bigger after pack?

use feature qw( say ); use Devel::Size qw( total_size ); use Devel::Peek qw( Dump ); my $num = 1000; say total_size($num); Dump($num); say ''; my $packed = pack("w*",$num); say total_size($packed); Dump($packed);

5.12, 64 bit

24 SV = IV(0x768010) at 0x768018 <-- See SVt_IV image below REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 1000 <-- 8 bytes 48 SV = PV(0x752f28) at 0x768000 <-- See SVt_PV image below REFCNT = 1 Three more fields than SVt_IV FLAGS = (PADMY,POK,pPOK) PV = 0x7d6520 "\207h"\0 CUR = 2 <-- Even though only 3 are needed, LEN = 8 <-- 8 byte string buffer allocated

SVt_IV, SVt_PV

Are the greyed out fields outside of allocated memory or simply unused? [ See dave_the_m's reply ]

(The numbers still don't quite add up, but it should give you an idea.)


5.14, 32 bit

16 SV = IV(0x51a100) at 0x51a104 <-- See SVt_IV image below REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 1000 <-- 4 bytes 36 SV = PV(0x5e8cc4) at 0x51a054 <-- See SVt_PV image below REFCNT = 1 Four more fields than SVt_IV FLAGS = (PADMY,POK,pPOK) PV = 0x5b4334 "\207h"\0 CUR = 2 <-- Even though only 3 are needed, LEN = 12 <-- 12 byte string buffer allocated

SVt_IV, SVt_PV

(The numbers still don't quite add up, but it should give you an idea.)

Update: Adjusted based on dave_the_m's explanation.

Replies are listed 'Best First'.
Re^2: Why is the size even bigger after pack?
by dave_the_m (Monsignor) on Nov 03, 2011 at 09:14 UTC
    In the case of the IV, the greyed-out bits represent memory that doesn't actually exist. The IV value is actually stored in the svu_pv slot in the head, and the sv_any pointer in the head actually points back to itself (with a slight offset), so that it appears to be pointing to a struct whose fourth field is an xiv_iv. In this way only a head and not a body needs allocating, but all the perl functions which expect to find it as the fourth field of the body still work.

    Dave.

      The IV value is actually stored in the svu_pv slot in the head

      That's what I thought!

      Thanks for the explanation, that clears up a lot. What doesn't illguts mention any of this!?

Re^2: Why is the size even bigger after pack?
by PerlOnTheWay (Monk) on Nov 03, 2011 at 08:37 UTC
    I think it's simply unused.
      Based on what?

        I read it somewhere, but I can't recall it now.

        I'll also implement it this way if I was Larry:)

Re^2: Why is the size even bigger after pack?
by PerlOnTheWay (Monk) on Nov 03, 2011 at 08:50 UTC

    I get different result when I run it with command line perl -we 'xxx' and perl filename

    what about you?

      No. Show the difference