Running into a similar deal, so I figured I'd share what I learned here.

First, you can't ever insert into a hash with only the hash value - you need the original string so that the hash implementation can do the final official comparison.

Next, while needing a SV is annoying, it's also related to performance. Perl has an internal pool of constants, and can make SVs from them. If your hash is using those as keys, and you perform the lookup using one, then the hash can skip the strncmp() and just compare the pointers. This is a lot like Java's String.intern(). I would presume that any time you use a literal key in your perl code, Perl probably adds that to the global pool and then hash get/set using those compiled scalars gets a lot faster. You can allocate one of these scalars with newSVpvn_share, if it is appropriate to do so. (obviously it would be bad to pollute the global string table with keys that come from transient data)

This doesn't solve the problem of wanting to have a more optimized way of fetching/storing a char* with a known hash, though. Looks like it's possible with the underlying implementation, but I haven't found any official API for it.

----- UPDATE -----

Looks like the authors of Class::XSAccessor had the same desire, so they just wrote new wrappers around hv_common_key_len. Of course it's probably a bad idea to reach that deep into perl for casual downstream modules, but the interesting bits can be found at: https://metacpan.org/source/SMUELLER/Class-XSAccessor-1.19/XS/Hash.xs


In reply to Re^5: PerlApi: hashes by NERDVANA
in thread PerlApi: hashes by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.