Acme-CPANModules-OrderedHash
view release on metacpan or search on metacpan
lib/Acme/CPANModules/OrderedHash.pm view on Meta::CPAN
package Acme::CPANModules::OrderedHash;
use strict;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2025-04-15'; # DATE
our $DIST = 'Acme-CPANModules-OrderedHash'; # DIST
our $VERSION = '0.004'; # VERSION
our $LIST = {
summary => "List of modules that provide ordered hash data type",
description => <<'MARKDOWN',
When you ask a Perl's hash for the list of keys, the answer comes back
unordered. In fact, Perl explicitly randomizes the order of keys it returns
everytime. The random ordering is a (security) feature, not a bug. However,
sometimes you want to know the order of insertion. These modules provide you
with an ordered hash; most of them implement it by recording the order of
insertion of keys in an additional array.
Other related modules:
<pm:Tie::SortHash> - will automatically sort keys when you call `keys()`,
`values()`, `each()`. But this module does not maintain insertion order.
MARKDOWN
entries => [
{
module => 'Tie::IxHash',
bench_code => sub {
my ($op, $numkeys, $numrep) = @_;
tie my %hash, "Tie::IxHash";
for (1..$numkeys) { $hash{"key$_"} = $_ }
if ($op eq 'delete') {
for (1..$numkeys) { delete $hash{"key$_"} }
} elsif ($op eq 'keys') {
for (1..$numrep) { my @keys = keys %hash }
} elsif ($op eq 'iterate') {
for (1..$numrep) { while (my ($k,$v) = each %hash) {} }
}
},
},
{
module => 'Hash::Ordered',
bench_code => sub {
my ($op, $numkeys, $numrep) = @_;
my $hash = Hash::Ordered->new;
for (1..$numkeys) { $hash->set("key$_" => $_) }
if ($op eq 'delete') {
for (1..$numkeys) { $hash->delete("key$_") }
} elsif ($op eq 'keys') {
for (1..$numrep) { my @keys = $hash->keys }
} elsif ($op eq 'iterate') {
for (1..$numrep) { my $iter = $hash->iterator; while (my ($k,$v) = $iter->()) {} }
}
},
},
{
module => 'Tie::Hash::Indexed',
description => <<'MARKDOWN',
Provides two interfaces: tied hash and OO.
MARKDOWN
bench_code => sub {
my ($op, $numkeys, $numrep) = @_;
tie my %hash, "Tie::Hash::Indexed";
for (1..$numkeys) { $hash{"key$_"} = $_ }
lib/Acme/CPANModules/OrderedHash.pm view on Meta::CPAN
MARKDOWN
bench_code => sub {
my ($op, $numkeys, $numrep) = @_;
my $tree= Tree::RB::XS->new(compare_fn => 'str', track_recent => 1, keys_in_recent_order => 1);
for (1..$numkeys) { $tree->insert("key$_") }
if ($op eq 'delete') {
for (1..$numkeys) { $tree->delete("key$_") }
} elsif ($op eq 'keys') {
for (1..$numrep) { my @keys= $tree->keys }
} elsif ($op eq 'iterate') {
for (1..$numrep) { my $iter = $tree->iter; while (my $v = $iter->next) {} }
}
},
},
],
bench_datasets => [
{name=>'insert 1000 pairs', argv => ['insert', 1000]},
{name=>'insert 1000 pairs + delete', argv => ['delete', 1000]},
{name=>'insert 1000 pairs + return keys 100 times', argv => ['keys', 1000, 100]},
{name=>'insert 1000 pairs + iterate 10 times', argv => ['iterate', 1000, 10], exclude_participant_tags => ['no_iterate']},
],
};
1;
# ABSTRACT: List of modules that provide ordered hash data type
__END__
=pod
=encoding UTF-8
=head1 NAME
Acme::CPANModules::OrderedHash - List of modules that provide ordered hash data type
=head1 VERSION
This document describes version 0.004 of Acme::CPANModules::OrderedHash (from Perl distribution Acme-CPANModules-OrderedHash), released on 2025-04-15.
=head1 SYNOPSIS
To run benchmark with default option:
% bencher --cpanmodules-module OrderedHash
To run module startup overhead benchmark:
% bencher --module-startup --cpanmodules-module OrderedHash
For more options (dump scenario, list/include/exclude/add participants, list/include/exclude/add datasets, etc), see L<bencher> or run C<bencher --help>.
=head1 DESCRIPTION
When you ask a Perl's hash for the list of keys, the answer comes back
unordered. In fact, Perl explicitly randomizes the order of keys it returns
everytime. The random ordering is a (security) feature, not a bug. However,
sometimes you want to know the order of insertion. These modules provide you
with an ordered hash; most of them implement it by recording the order of
insertion of keys in an additional array.
Other related modules:
L<Tie::SortHash> - will automatically sort keys when you call C<keys()>,
C<values()>, C<each()>. But this module does not maintain insertion order.
=head1 ACME::CPANMODULES ENTRIES
=over
=item L<Tie::IxHash>
=item L<Hash::Ordered>
=item L<Tie::Hash::Indexed>
Provides two interfaces: tied hash and OO.
=item L<Tie::LLHash>
=item L<Tie::StoredOrderHash>
=item L<Array::OrdHash>
Provide something closest to PHP's associative array, where you can refer
elements by key or by numeric index, and insertion order is remembered.
=item L<List::Unique::DeterministicOrder>
Provide a list, not hash.
=item L<Tree::RB::XS>
Multi-purpose tree data structure which can record insertion order and act as an
ordered hash. Use C<< track_recent =E<gt> 1, keys_in_recent_order =E<gt> 1 >> options. Can
be used as a tied hash, or as an object (faster).
=back
=head1 BENCHMARKED MODULES
Version numbers shown below are the versions used when running the sample benchmark.
L<Tie::IxHash> 1.23
L<Hash::Ordered> 0.014
L<Tie::Hash::Indexed> 0.08
L<Tie::LLHash> 1.004
L<Tie::StoredOrderHash> 0.22
( run in 0.639 second using v1.01-cache-2.11-cpan-39bf76dae61 )