← Index
NYTProf Performance Profile   « line view »
For ../prof.pl
  Run on Wed Dec 14 15:57:08 2022
Reported on Wed Dec 14 16:00:35 2022

Filename/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Data/Perl/Role/Collection/Hash.pm
StatementsExecuted 338850 statements in 661ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
7364811368ms6.67sData::Perl::Role::Collection::Hash::::getData::Perl::Role::Collection::Hash::get
3913811266ms284msData::Perl::Role::Collection::Hash::::kvData::Perl::Role::Collection::Hash::kv
6511791µs9.78msData::Perl::Role::Collection::Hash::::setData::Perl::Role::Collection::Hash::set
2933217µs243µsData::Perl::Role::Collection::Hash::::valuesData::Perl::Role::Collection::Hash::values
11140µs147µsData::Perl::Role::Collection::Hash::::BEGIN@5Data::Perl::Role::Collection::Hash::BEGIN@5
11123µs46µsData::Perl::Role::Collection::Hash::::BEGIN@77Data::Perl::Role::Collection::Hash::BEGIN@77
1119µs35µsData::Perl::Role::Collection::Hash::::BEGIN@8Data::Perl::Role::Collection::Hash::BEGIN@8
1118µs128µsData::Perl::Role::Collection::Hash::::BEGIN@7Data::Perl::Role::Collection::Hash::BEGIN@7
1116µs18µsData::Perl::Role::Collection::Hash::::BEGIN@9Data::Perl::Role::Collection::Hash::BEGIN@9
0000s0sData::Perl::Role::Collection::Hash::::_array_classData::Perl::Role::Collection::Hash::_array_class
0000s0sData::Perl::Role::Collection::Hash::::accessorData::Perl::Role::Collection::Hash::accessor
0000s0sData::Perl::Role::Collection::Hash::::allData::Perl::Role::Collection::Hash::all
0000s0sData::Perl::Role::Collection::Hash::::clearData::Perl::Role::Collection::Hash::clear
0000s0sData::Perl::Role::Collection::Hash::::countData::Perl::Role::Collection::Hash::count
0000s0sData::Perl::Role::Collection::Hash::::definedData::Perl::Role::Collection::Hash::defined
0000s0sData::Perl::Role::Collection::Hash::::deleteData::Perl::Role::Collection::Hash::delete
0000s0sData::Perl::Role::Collection::Hash::::existsData::Perl::Role::Collection::Hash::exists
0000s0sData::Perl::Role::Collection::Hash::::is_emptyData::Perl::Role::Collection::Hash::is_empty
0000s0sData::Perl::Role::Collection::Hash::::keysData::Perl::Role::Collection::Hash::keys
0000s0sData::Perl::Role::Collection::Hash::::newData::Perl::Role::Collection::Hash::new
0000s0sData::Perl::Role::Collection::Hash::::shallow_cloneData::Perl::Role::Collection::Hash::shallow_clone
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Data::Perl::Role::Collection::Hash;
210s$Data::Perl::Role::Collection::Hash::VERSION = '0.002011';
3# ABSTRACT: Wrapping class for Perl's built in hash structure.
4
5360µs3253µs
# spent 147µs (40+107) within Data::Perl::Role::Collection::Hash::BEGIN@5 which was called: # once (40µs+107µs) by Role::Tiny::_load_module at line 5
use strictures 1;
# spent 147µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@5 # spent 67µs making 1 call to strictures::import # spent 39µs making 1 call to strictures::VERSION
6
7234µs2248µs
# spent 128µs (8+120) within Data::Perl::Role::Collection::Hash::BEGIN@7 which was called: # once (8µs+120µs) by Role::Tiny::_load_module at line 7
use Role::Tiny;
# spent 128µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@7 # spent 120µs making 1 call to Role::Tiny::import
8224µs261µs
# spent 35µs (9+26) within Data::Perl::Role::Collection::Hash::BEGIN@8 which was called: # once (9µs+26µs) by Role::Tiny::_load_module at line 8
use Scalar::Util qw/blessed/;
# spent 35µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@8 # spent 26µs making 1 call to Exporter::import
92331µs230µs
# spent 18µs (6+12) within Data::Perl::Role::Collection::Hash::BEGIN@9 which was called: # once (6µs+12µs) by Role::Tiny::_load_module at line 9
use Module::Runtime qw/use_package_optimistically/;
# spent 18µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@9 # spent 12µs making 1 call to Module::Runtime::import
10
11sub new { my $cl = shift; bless({ @_ }, $cl) }
12
13sub _array_class { 'Data::Perl::Collection::Array' }
14
15
# spent 6.67s (368ms+6.30) within Data::Perl::Role::Collection::Hash::get which was called 73648 times, avg 91µs/call: # 73648 times (368ms+6.30s) by Data::Perl::Collection::Hash::MooseLike::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Data/Perl/Collection/Hash/MooseLike.pm:22] at line 14 of Data/Perl/Collection/Hash/MooseLike.pm, avg 91µs/call
sub get {
167364816.7ms my $self = shift;
17
1873648186ms if (@_ > 1) {
19 my @res = @{$self}{@_};
20
21 blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
22 }
23 else {
2473648143ms476686.30s $self->{$_[0]};
# spent 6.30s making 47668 calls to Mojo::URL::__ANON__[Mojo/URL.pm:3], avg 132µs/call
25 }
26}
27
28
# spent 9.78ms (791µs+8.98) within Data::Perl::Role::Collection::Hash::set which was called 65 times, avg 150µs/call: # 65 times (791µs+8.98ms) by Data::Perl::Collection::Hash::MooseLike::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Data/Perl/Collection/Hash/MooseLike.pm:22] at line 14 of Data/Perl/Collection/Hash/MooseLike.pm, avg 150µs/call
sub set {
296515µs my $self = shift;
3065130µs my @keys_idx = grep { ! ($_ % 2) } 0..$#_;
316555µs my @values_idx = grep { $_ % 2 } 0..$#_;
32
3365187µs394.73ms @{$self}{@_[@keys_idx]} = @_[@values_idx];
# spent 4.73ms making 39 calls to Mojo::URL::__ANON__[Mojo/URL.pm:3], avg 121µs/call
34
3565116µs394.22ms my @res = @{$self}{@_[@keys_idx]};
# spent 4.22ms making 39 calls to Mojo::URL::__ANON__[Mojo/URL.pm:3], avg 108µs/call
36
3765287µs6535µs blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
# spent 35µs making 65 calls to Scalar::Util::blessed, avg 538ns/call
38}
39
40sub delete {
41 my $self = shift;
42 my @res = CORE::delete @{$self}{@_};
43
44 blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
45}
46
47sub keys {
48 my ($self) = @_;
49
50 my @res = keys %{$self};
51
52 blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
53}
54
55sub exists { CORE::exists $_[0]->{$_[1]} }
56
57sub defined { CORE::defined $_[0]->{$_[1]} }
58
59
# spent 243µs (217+26) within Data::Perl::Role::Collection::Hash::values which was called 29 times, avg 8µs/call: # 16 times (103µs+10µs) by JSON::Schema::Modern::_canonical_resources at line 17 of (eval 341)[Sub/Quote.pm:3], avg 7µs/call # 12 times (103µs+15µs) by JSON::Schema::Modern::Document::_canonical_resources at line 17 of (eval 293)[Sub/Quote.pm:3], avg 10µs/call # once (11µs+1µs) by JSON::Schema::Modern::_get_vocabulary_values at line 17 of (eval 354)[Sub/Quote.pm:3]
sub values {
602916µs my ($self) = @_;
61
622968µs my @res = CORE::values %{$_[0]};
63
6429156µs2926µs blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
# spent 26µs making 29 calls to Scalar::Util::blessed, avg 897ns/call
65}
66
67
# spent 284ms (266+18.4) within Data::Perl::Role::Collection::Hash::kv which was called 39138 times, avg 7µs/call: # 39138 times (266ms+18.4ms) by JSON::Schema::Modern::Document::resource_pairs at line 17 of (eval 296)[Sub/Quote.pm:3], avg 7µs/call
sub kv {
683913819.2ms my ($self) = @_;
69
7039138130ms my @res = CORE::map { [ $_, $self->{$_} ] } CORE::keys %{$self};
71
7239138164ms3913818.4ms blessed($self) ? use_package_optimistically($self->_array_class)->new(@res) : @res;
# spent 18.4ms making 39138 calls to Scalar::Util::blessed, avg 470ns/call
73}
74
75
76{
773306µs269µs
# spent 46µs (23+23) within Data::Perl::Role::Collection::Hash::BEGIN@77 which was called: # once (23µs+23µs) by Role::Tiny::_load_module at line 77
no warnings 'once';
# spent 46µs making 1 call to Data::Perl::Role::Collection::Hash::BEGIN@77 # spent 23µs making 1 call to warnings::unimport
78
79 sub all {
80 my ($self) = @_;
81
82 my @res = CORE::map { $_, $self->{$_} } CORE::keys %{$self};
83
84 @res;
85 }
86
8711µs *elements = *all;
88}
89
90sub clear { %{$_[0]} = () }
91
92sub count { CORE::scalar CORE::keys %{$_[0]} }
93
94sub is_empty { CORE::scalar CORE::keys %{$_[0]} ? 0 : 1 }
95
96sub accessor {
97 if (@_ == 2) {
98 $_[0]->{$_[1]};
99 }
100 elsif (@_ > 2) {
101 $_[0]->{$_[1]} = $_[2];
102 }
103}
104
105sub shallow_clone { blessed($_[0]) ? bless({%{$_[0]}}, ref $_[0]) : {%{$_[0]}} }
106
10714µs1;
108
109=pod
110
111=encoding UTF-8
112
113=head1 NAME
114
115Data::Perl::Role::Collection::Hash - Wrapping class for Perl's built in hash structure.
116
117=head1 VERSION
118
119version 0.002011
120
121=head1 SYNOPSIS
122
123 use Data::Perl qw/hash/;
124
125 my $hash = hash(a => 1, b => 2);
126
127 $hash->values; # (1, 2)
128
129 $hash->set('foo', 'bar'); # (a => 1, b => 2, foo => 'bar')
130
131=head1 DESCRIPTION
132
133This class provides a wrapper and methods for interacting with a hash.
134All methods that return a list do so via a Data::Perl::Collection::Array
135object.
136
137=head1 PROVIDED METHODS
138
139=over 4
140
141=item B<new($key, $value, ...)>
142
143Given an optional list of keys/values, constructs a new Data::Perl::Collection::Hash
144object initalized with keys/values and returns it.
145
146=item B<get($key, $key2, $key3...)>
147
148Returns a list of values in the hash for the given keys.
149
150This method requires at least one argument.
151
152=item B<set($key =E<gt> $value, $key2 =E<gt> $value2...)>
153
154Sets the elements in the hash to the given values. It returns the new values
155set for each key, in the same order as the keys passed to the method.
156
157This method requires at least two arguments, and expects an even number of
158arguments.
159
160=item B<delete($key, $key2, $key3...)>
161
162Removes the elements with the given keys.
163
164Returns a list of values in the hash for the deleted keys.
165
166=item B<keys>
167
168Returns the list of keys in the hash.
169
170This method does not accept any arguments.
171
172=item B<exists($key)>
173
174Returns true if the given key is present in the hash.
175
176This method requires a single argument.
177
178=item B<defined($key)>
179
180Returns true if the value of a given key is defined.
181
182This method requires a single argument.
183
184=item B<values>
185
186Returns the list of values in the hash.
187
188This method does not accept any arguments.
189
190=item B<kv>
191
192Returns the key/value pairs in the hash as an array of array references.
193
194 for my $pair ( $object->option_pairs ) {
195 print "$pair->[0] = $pair->[1]\n";
196 }
197
198This method does not accept any arguments.
199
200=item B<elements/all>
201
202Returns the key/value pairs in the hash as a flattened list..
203
204This method does not accept any arguments.
205
206=item B<clear>
207
208Resets the hash to an empty value, like C<%hash = ()>.
209
210This method does not accept any arguments.
211
212=item B<count>
213
214Returns the number of elements in the hash. Also useful for not empty:
215C<< has_options => 'count' >>.
216
217This method does not accept any arguments.
218
219=item B<is_empty>
220
221If the hash is populated, returns false. Otherwise, returns true.
222
223This method does not accept any arguments.
224
225=item B<accessor($key)>
226
227=item B<accessor($key, $value)>
228
229If passed one argument, returns the value of the specified key. If passed two
230arguments, sets the value of the specified key.
231
232When called as a setter, this method returns the value that was set.
233
234=item B<shallow_clone>
235
236This method returns a shallow clone of the hash reference. The return value
237is a reference to a new hash with the same keys and values. It is I<shallow>
238because any values that were references in the original will be the I<same>
239references in the clone.
240
241=item B<_array_class>
242
243The name of the class which returned lists are instances of; i.e.
244C<< Data::Perl::Collection::Array >>.
245
246Subclasses of this class can override this method.
247
248=back
249
250Note that C<each> is deliberately omitted, due to its stateful interaction
251with the hash iterator. C<keys> or C<kv> are much safer.
252
253=head1 SEE ALSO
254
255=over 4
256
257=item * L<Data::Perl>
258
259=item * L<MooX::HandlesVia>
260
261=back
262
263=head1 AUTHOR
264
265Matthew Phillips <mattp@cpan.org>
266
267=head1 COPYRIGHT AND LICENSE
268
269This software is copyright (c) 2020 by Matthew Phillips <mattp@cpan.org>.
270
271This is free software; you can redistribute it and/or modify it under
272the same terms as the Perl 5 programming language system itself.
273
274=cut
275
276__END__