Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

When will a key shared between two hashes?

by gyhor (Initiate)
on Dec 09, 2011 at 20:44 UTC ( [id://942730]=perlquestion: print w/replies, xml ) Need Help??

gyhor has asked for the wisdom of the Perl Monks concerning the following question:

In hv.c is written, that keys (hek-structure) are shared for space optimization. But i don't find any example for that. If i use this hashes $rgb{rot}='aaa ';$cmyk{rot}='aaa'. Is the key 'rot' shared between them? If i look in the output of:
perl -MDevel::Peek -e '$rgb{rot}="aaa";$cmyk{rot}="aaa";Dump(\%rgb);Du +mp(\%cmyk)'
I don't see any shared keys:
SV = RV(0x1351ba8) at 0x1351b98 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x136ecf0 SV = PVHV(0x1357fb0) at 0x136ecf0 REFCNT = 2 FLAGS = (SHAREKEYS) ARRAY = 0x1392a30 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "rot" HASH = 0xaffb6cf6 SV = PV(0x134fb78) at 0x1351d48 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x1369340 "aaa"\0 CUR = 3 LEN = 8 SV = RV(0x1351ba8) at 0x1351b98 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1378990 SV = PVHV(0x1357fd8) at 0x1378990 REFCNT = 2 FLAGS = (SHAREKEYS) ARRAY = 0x1368fa0 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "rot" HASH = 0xaffb6cf6 SV = PV(0x134fc58) at 0x1351f88 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x13652c0 "aaa"\0 CUR = 3 LEN = 8

Replies are listed 'Best First'.
Re: When will a key shared between two hashes?
by ikegami (Patriarch) on Dec 09, 2011 at 21:33 UTC

    That doesn't show how or where the keys are stored, so I don't know how you can conclude a lack of shared keys.

    (I don't know whether they are shared or not.)

      what does he mean by sharing ? I looked into the code(hv.c) and from what I gather, it's "sharing between threads". does he mean sharing 2 equal values from different hashes ?
Re: When will a key shared between two hashes?
by JavaFan (Canon) on Dec 09, 2011 at 22:08 UTC
    AFAIK, keys are always shared. It's an implementation detail that isn't visible from the language.
      If keys are always shared, they should use the same memory-address, or not?
        If keys are always shared, they should use the same memory-address, or not?
        Sure, but you cannot get the memory address of a hash key from the language. Do note that hash keys are not SVs.
Re: When will a key shared between two hashes?
by TJPride (Pilgrim) on Dec 09, 2011 at 23:00 UTC
    What exactly is the purpose of this question? Are you interested in the theory, or are you actually trying to apply this to something in practice, and if the latter, can you please explain what exactly? I'm not really understanding what you're getting at.
      It is for the theory. I was digging the last days in hv.c to understand the hash implementation for comparing it with the hash implementation in python. Everywhere (in the source, perl internals, etc.) is written, that keys are shared. but i didn't i find a way to "see" with a debugger or with Devel::Peek. So i am confused, if i understood it correctly.
        What were you expecting to see in the output of Devel::Peek? The output already shows the SHAREKEYS flag -- what else were you looking for?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://942730]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found