in reply to Benchmark: Constant List in Hash Slice

Literal keys in hash subscripts and hash slices have the the hash (as in checksum) values of their strings pre-computed, and the string part of the string constant is stored in such a way that its easy and quick to pass directly to the hash lookup API functions.

Technically: at compile time, the PV part of the const SV is converted to a HEK, with SvPV(sv) pointing to the string part within the HEK. So it looks like a string but is also a HEK.

Dave.

  • Comment on Re: Benchmark: Constant List in Hash Slice

Replies are listed 'Best First'.
Re^2: Benchmark: Constant List in Hash Slice
by LanX (Saint) on Mar 13, 2019 at 17:38 UTC
    OK I thought the inlining of the constant list might happen early enough to trigger this pre-computation.

    But the optimizer doesn't see the inlined keys as literal strings, so it has to calculate this lookup-index dynamically at run-time.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: Benchmark: Constant List in Hash Slice
by LanX (Saint) on Mar 14, 2019 at 12:03 UTC
    does this also explain why using a normal @array variable is faster than a constant list?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      does this also explain why using a normal @array variable is faster than a constant list?
      No. That's because constant arrays are implemented as constant array refs. So
      use constant foo qw(a b c); @a = $h{foo()}
      is implemented roughly as
      use constant foo [qw(a b c)]; @a = $h{@{foo()}}
      And neither (as far as I'm aware) get the compile-time HEK treatment

      Dave.

        Oh that's good to know, thanks! :)

        Hence constant arrays won't help much for tuning performance.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice