Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl: Why do Devel::Refcount::refcount and Devel::Peek::SvREFCNT disagree?

Tags:

perl

I was reading How can I access the ref count of a Perl hash?, and there both Devel::Refcount::refcount and Devel::Peek::SvREFCNT are suggested.

But they don't return the same reference counts. Why is that?

Here a modified example from perldoc Devel::Refcount:

use Devel::Peek;
use Devel::Refcount;

my $anon = [];

printf "Anon ARRAY $anon has %d/%d reference\n",
    Devel::Refcount::refcount($anon),
    Devel::Peek::SvREFCNT($anon);

my $otherref = $anon;

printf "Anon ARRAY $anon now has %d/%d references\n",
    Devel::Refcount::refcount($anon),
    Devel::Peek::SvREFCNT($anon);

which prints out:

Anon ARRAY ARRAY(0x8b10818) has 1/1 reference
Anon ARRAY ARRAY(0x8b10818) now has 2/1 references

Notice the last 2/1 discrepancy...

(If it turns out I'm not doing something stupid, I'll add a link from How can I access the ref count of a Perl hash? to here)

like image 346
Peter V. Mørch Avatar asked Jul 05 '12 12:07

Peter V. Mørch


2 Answers

I can't say that I grok all of it yet, but your question is answered prominently in the Devel::Refcount perldoc

COMPARISON WITH SvREFCNT

This function differs from Devel::Peek::SvREFCNT in that SvREFCNT() gives the reference count of the SV object itself that it is passed, whereas refcount() gives the count of the object being pointed to. This allows it to give the count of any referent (i.e. ARRAY, HASH, CODE, GLOB and Regexp types) as well.

Consider the following example program:

 use Devel::Peek qw( SvREFCNT );
 use Devel::Refcount qw( refcount );

 sub printcount
 {
    my $name = shift;

    printf "%30s has SvREFCNT=%d, refcount=%d\n",
       $name, SvREFCNT($_[0]), refcount($_[0]);
 }

 my $var = [];

 printcount 'Initially, $var', $var;

 my $othervar = $var;

 printcount 'Before CODE ref, $var', $var;
 printcount '$othervar', $othervar;

 my $code = sub { undef $var };

 printcount 'After CODE ref, $var', $var;
 printcount '$othervar', $othervar;

This produces the output

            Initially, $var has SvREFCNT=1, refcount=1
      Before CODE ref, $var has SvREFCNT=1, refcount=2
                  $othervar has SvREFCNT=1, refcount=2
       After CODE ref, $var has SvREFCNT=2, refcount=2
                  $othervar has SvREFCNT=1, refcount=2

Here, we see that SvREFCNT() counts the number of references to the SV object passed in as the scalar value - the $var or $othervar respectively, whereas refcount() counts the number of reference values that point to the referent object - the anonymous ARRAY in this case.

Before the CODE reference is constructed, both $var and $othervar have SvREFCNT() of 1, as they exist only in the current lexical pad. The anonymous ARRAY has a refcount() of 2, because both $var and $othervar store a reference to it.

After the CODE reference is constructed, the $var variable now has an SvREFCNT() of 2, because it also appears in the lexical pad for the new anonymous CODE block.

like image 171
mob Avatar answered Oct 21 '22 05:10

mob


Devel::Refcount::refcount($anon) returns the reference count of that which is referenced by $anon.

The array is referenced by $anon and by $otherref: 2


Devel::Peek::SvREFCNT($anon) returns the reference count of $anon itself.

The scalar is referenced by the pad in which it resides: 1

Devel::Peek doesn't seem to provide a means of getting the ref count of arrays, hashes, etc.


$ perl -MDevel::Peek -E'my $aref2 = my $aref1 = []; Dump($aref1);'
SV = IV(0x99eee34) at 0x99eee38
  REFCNT = 1                    <---- Devel::Peek::SvREFCNT
  FLAGS = (PADMY,ROK)
  RV = 0x99d57d0
  SV = PVAV(0x99d6778) at 0x99d57d0
    REFCNT = 2                  <---- Devel::Refcount::refcount
    FLAGS = ()
    ARRAY = 0x0
    FILL = -1
    MAX = -1
    ARYLEN = 0x0
    FLAGS = (REAL)

Perl provides a semi-supported builtin called Internals::SvREFCNT that works on scalars, arrays and hashes.

Internals::SvREFCNT(@$anon) returns the reference count of @$anon itself.

The array is referenced by $anon and by $otherref: 2

The above only works for scalars, arrays and hashes, and you need to use the proper sigil. If you just want to pass an arbitrary reference, you can use:

&Internals::SvREFCNT($anon) + 1 returns the reference count of that which is referenced by $anon.

The array is referenced by $anon and by $otherref: 2

like image 34
ikegami Avatar answered Oct 21 '22 06:10

ikegami