Re: Option to warn/die on fetch with invalid hash key?

[email protected] (Sam Kington)
Newsgroups perl.perl5.porters
Message-ID <[email protected]>
On 27 Dec 2025, at 03:07, Ricardo Signes <[email protected]> wrote:
> On Fri, Dec 26, 2025, at 21:06, Sam Kington wrote:
>> Schwern’s answer mentions Hash::Util, which appears to do what you want?
>> […]
>> I’m sure there are performance implications of doing this, though.
> 
> Why?  I don't see a clear reason this would be slow, and measuring doesn't show any significant difference.
> 
> Both of these consistently come in at 0.12 ± 0.005 seconds for me.
> 
> perl -MHash::Util=lock_keys -e '%hash = map {; $_ => $_ } (0 .. 1_000); for (1 .. 1_000_000) { my $x = $hash{ int(rand() * 1_000) } }'
> 
> perl -MHash::Util=lock_keys -e '%hash = map {; $_ => $_ } (0 .. 1_000); lock_keys(%hash); for (1 .. 1_000_000) { my $x = $hash{ int(rand() * 1_000) } }'


It was late, and I was being paranoid, but I’m glad to be shown that I’m wrong! I will consider explicitly using Hash::Util::lock_keys more often in my code.

Sam
-- 
Website: https://www.illuminated.co.uk/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.