in reply to Devel::Size reports different size after hash access

I am not experienced enough to know most of the inner details, but it's fair to say that perl does a lot of work internally when the items are first accessed. If you change to Devel::Peek, you can see how the structure changes after access:

use strict; use warnings; use Devel::Peek qw(Dump); my $s = 'AAAAAAAAAAAAAAA'; my %hash = map {$s++ => 1} 1 .. 2; print "before:\n"; Dump \%hash; open my $fh, '>', 'j1.txt' or die $!; for my $key (keys %hash) { print $fh "$key $hash{$key}\n"; } print "after:\n"; Dump \%hash;

Output:

before: SV = IV(0x1e5cca8) at 0x1e5ccb8 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1e88420 SV = PVHV(0x1e63ef0) at 0x1e88420 REFCNT = 2 FLAGS = (PADMY,SHAREKEYS) ARRAY = 0x1ed7880 (0:6, 1:2) hash quality = 125.0% KEYS = 2 FILL = 2 MAX = 7 Elt "AAAAAAAAAAAAAAA" HASH = 0xa39c9065 SV = IV(0x1e7a9a8) at 0x1e7a9b8 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 Elt "AAAAAAAAAAAAAAB" HASH = 0x7747aa6e SV = IV(0x1e7a990) at 0x1e7a9a0 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1 after: SV = IV(0x1e883e0) at 0x1e883f0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1e88420 SV = PVHV(0x1e63ef0) at 0x1e88420 REFCNT = 2 FLAGS = (PADMY,OOK,SHAREKEYS) ARRAY = 0x1e75110 (0:6, 1:2) hash quality = 125.0% KEYS = 2 FILL = 2 MAX = 7 RITER = -1 EITER = 0x0 RAND = 0x69863a90 Elt "AAAAAAAAAAAAAAA" HASH = 0xa39c9065 SV = PVIV(0x1e7f860) at 0x1e7a9b8 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 1 PV = 0x1ed7810 "1"\0 CUR = 1 LEN = 16 Elt "AAAAAAAAAAAAAAB" HASH = 0x7747aa6e SV = PVIV(0x1e7f878) at 0x1e7a9a0 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 1 PV = 0x1e98b90 "1"\0 CUR = 1 LEN = 16

Here's a brief on memory usage.

Replies are listed 'Best First'.
Re^2: Devel::Size reports different size after hash access
by GrandFather (Saint) on Oct 25, 2016 at 20:22 UTC

    It's not the "first access" that does it, it's the "give it to me different". Stored as a number and accessed as a string in the OP's case.

    Premature optimization is the root of all job security
Re^2: Devel::Size reports different size after hash access
by Cristoforo (Curate) on Oct 26, 2016 at 01:02 UTC
    stevieb

    Thanks for the nice link to Mastering Perl. It was helpful and I learned from all the replies given here. In a thousand years, I don't think I would have caught the distinction between using the hash value as a string vs. a number.