If I want high performance I'm used to write this idiom when want create hash as set:
my %h;
for my $key (@some_vals) {
...
$h{$key} = undef unless exists $h{$key};
...
}
return keys %h;
This code is little bit faster than commonly used $h{$key}++
. exists
avoids useless assignment and undef
avoids allocation for value. Best answer for you is: Benchmark it! I guess that exists $ids{$name}
is little bit faster than $id=$ids{$name}
and if you have big miss ratio your version with exists can be faster than assignment and test after.
For example if I want fast sets intersection I would wrote something like this.
sub intersect {
my $h;
@$h{@{shift()}} = ();
my $i;
for (@_) {
return unless %$h;
$i = {};
@$i{grep exists $h->{$_}, @$_} = ();
$h = $i;
}
return keys %$h;
}