in reply to Re^2: array overhead
in thread array overhead

Assigning a number to a scalar doesn't free the string buffer, it just marks it as unused. You "solution" can actually increase the memory used.

$ perl -MDevel::Size=total_size -E' $a[5]="5678"; say total_size($a[5]); $x=int($a[5]); say total_size($a[5]); ' 40 44

undef $s (but not $s = undef;) will free the string buffer.

$ perl -MDevel::Size=total_size -E' $a[5]="5678"; say total_size($a[5]); $t=int($a[5]); undef $a[5]; $a[5]=$t; say total_size($a[5]); ' 40 36

It doesn't downgrade the scalar from PVIV to IV, though. You'd need to start with a new scalar to do that.

$ perl -MDevel::Peek -MDevel::Size=total_size -E' $a[5]="5678"; say total_size($a[5]); $t=int($a[5]); delete $a[5]; $a[5]=$t; say total_size($a[5]); ' 40 16

Replies are listed 'Best First'.
Re^4: array overhead
by dave_the_m (Monsignor) on Jan 06, 2011 at 19:57 UTC
    Yeah sorry, bad example.

    I was thinking in terms of using it within in your $a=[@$a], e.g. $a= [ @$a[0..4], int($a->[5]) ], where it *would* get a new SV.

    Dave.