我将文本文件中的许多数字解析为包含键和对值数组的相应引用的哈希条目。使用 Devel::Peek 和 Devel::Size 我注意到数字的字符串表示形式存储在此数据结构中,这会浪费内存。我怎样才能摆脱这些字符串表示的记忆(换句话说,我怎样才能把 PVIV 变成 IV)?
问问题
345 次
2 回答
4
将“numify 运算符”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
于 2019-07-03T16:17:36.670 回答
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
or的标量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
于 2019-07-04T08:42:34.523 回答