我正在将文本文件中的许多数字解析为由键和对值数组的对应引用组成的哈希条目。使用Devel :: Peek和Devel :: Size时,我注意到数字的字符串表示形式存储在此数据结构中,这浪费了内存。如何清除这些字符串表示形式的内存(换句话说,如何将PVIV转换为IV)?
答案 0 :(得分:4)
向他们应用“数字运算符” 0+
。
use strict;
use warnings;
use Devel::Peek;
# PVIVs (strings that were used in a numeric context)
my @values = grep 0+$_, qw/123 234/;
my %hash = ( "key" => [ @values ] );
Dump \%hash;
# make them just IVs
%hash = ( "key" => [ map 0+$_, @values ] );
Dump \%hash;
输出:
SV = IV(0x7fcaf401d9d0) at 0x7fcaf401d9e0
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x7fcaf4025290
SV = PVHV(0x7fcaf400ac60) at 0x7fcaf4025290
REFCNT = 2
FLAGS = (SHAREKEYS)
ARRAY = 0x7fcaf3c1ad00 (0:7, 1:1)
hash quality = 100.0%
KEYS = 1
FILL = 1
MAX = 7
Elt "key" HASH = 0x11e2db55
SV = IV(0x7fcaf401d928) at 0x7fcaf401d938
REFCNT = 1
FLAGS = (ROK)
RV = 0x7fcaf401d908
SV = PVAV(0x7fcaf4005c58) at 0x7fcaf401d908
REFCNT = 1
FLAGS = ()
ARRAY = 0x7fcaf3c04550
FILL = 1
MAX = 1
FLAGS = (REAL)
Elt No. 0
SV = PVIV(0x7fcaf4021080) at 0x7fcaf401d920
REFCNT = 1
FLAGS = (IOK,POK,IsCOW,pIOK,pPOK)
IV = 123
PV = 0x7fcaf3c04900 "123"\0
CUR = 3
LEN = 10
COW_REFCNT = 2
Elt No. 1
SV = PVIV(0x7fcaf4021098) at 0x7fcaf401d950
REFCNT = 1
FLAGS = (IOK,POK,IsCOW,pIOK,pPOK)
IV = 234
PV = 0x7fcaf3c0e2b0 "234"\0
CUR = 3
LEN = 10
COW_REFCNT = 2
SV = IV(0x7fcaf401d9d0) at 0x7fcaf401d9e0
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x7fcaf4025290
SV = PVHV(0x7fcaf400ac60) at 0x7fcaf4025290
REFCNT = 2
FLAGS = (SHAREKEYS)
ARRAY = 0x7fcaf3c1ad00 (0:7, 1:1)
hash quality = 100.0%
KEYS = 1
FILL = 1
MAX = 7
Elt "key" HASH = 0x11e2db55
SV = IV(0x7fcaf40252f8) at 0x7fcaf4025308
REFCNT = 1
FLAGS = (ROK)
RV = 0x7fcaf401d9f8
SV = PVAV(0x7fcaf4005ca8) at 0x7fcaf401d9f8
REFCNT = 1
FLAGS = ()
ARRAY = 0x7fcaf3c0c6d0
FILL = 1
MAX = 1
FLAGS = (REAL)
Elt No. 0
SV = IV(0x7fcaf401da78) at 0x7fcaf401da88
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 123
Elt No. 1
SV = IV(0x7fcaf401da60) at 0x7fcaf401da70
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 234
答案 1 :(得分:3)
首先,如果您想在此级别上节省内存,Perl可能不是适合您的工具。当Perl提供速度优势时,它会肆意地“浪费”内存。
$ perl -e'
use feature qw( say );
use Devel::Size qw( size );
sub f {
my $x;
say size($x);
$x = "x" x 100;
say size($x);
}
f() for 1..2;
'
24
134
134
134
0+$scalar
和int($scalar)
都返回类型为SVt_IV
或SVt_NV
的标量。这些都可以。
$ perl -e'
use feature qw( say );
use Devel::Size qw( size );
my $x = 1234567890;
my $y = "1234567890";
say size($x);
say size($y);
say size($0+$y);
say size(int($y));
'
24
44
24
24
不可能降级现有的标量,但是您可以通过“别名”来替换它。
$ perl -e'
use feature qw( say );
use experimental qw( refaliasing );
use Devel::Size qw( size );
my $x = 1234567890;
my $y = "1234567890";
say size($x);
say size($y);
\$y = \(0+$y);
say size($y);
'
24
44
24