← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 19:05:14 2015
Reported on Fri Jul 31 19:08:10 2015

Filename/var/www/foswiki11/lib/Foswiki/ListIterator.pm
StatementsExecuted 992877 statements in 1.38s
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
7072255484ms574msFoswiki::ListIterator::::nextFoswiki::ListIterator::next
141616106420ms420msFoswiki::ListIterator::::hasNextFoswiki::ListIterator::hasNext
235743.40ms3.96msFoswiki::ListIterator::::newFoswiki::ListIterator::new
10011310µs310µsFoswiki::ListIterator::::allFoswiki::ListIterator::all
8111271µs271µsFoswiki::ListIterator::::resetFoswiki::ListIterator::reset
11113µs26µsFoswiki::ListIterator::::BEGIN@16Foswiki::ListIterator::BEGIN@16
11112µs17µsFoswiki::ListIterator::::BEGIN@17Foswiki::ListIterator::BEGIN@17
1118µs21µsFoswiki::ListIterator::::BEGIN@22Foswiki::ListIterator::BEGIN@22
1114µs4µsFoswiki::ListIterator::::BEGIN@19Foswiki::ListIterator::BEGIN@19
0000s0sFoswiki::ListIterator::::skipFoswiki::ListIterator::skip
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1# See bottom of file for license and copyright information
2
3=begin TML
4
5---+ package Foswiki::ListIterator
6*implements* Foswiki::Iterator
7
8Iterator over a perl list
9
10WARNING: this Iterator will skip any elements that are == undef.
11SMELL: hasNext should not 'return 1 if defined($this->{next}), but rather use a boolean - to allow array elements to be undef too.
12
13=cut
14
15package Foswiki::ListIterator;
16229µs239µs
# spent 26µs (13+13) within Foswiki::ListIterator::BEGIN@16 which was called: # once (13µs+13µs) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 16
use strict;
# spent 26µs making 1 call to Foswiki::ListIterator::BEGIN@16 # spent 13µs making 1 call to strict::import
17225µs222µs
# spent 17µs (12+5) within Foswiki::ListIterator::BEGIN@17 which was called: # once (12µs+5µs) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 17
use warnings;
# spent 17µs making 1 call to Foswiki::ListIterator::BEGIN@17 # spent 5µs making 1 call to warnings::import
18
19232µs14µs
# spent 4µs within Foswiki::ListIterator::BEGIN@19 which was called: # once (4µs+0s) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 19
use Foswiki::Iterator ();
# spent 4µs making 1 call to Foswiki::ListIterator::BEGIN@19
2016µsour @ISA = ('Foswiki::Iterator');
21
222537µs233µs
# spent 21µs (8+12) within Foswiki::ListIterator::BEGIN@22 which was called: # once (8µs+12µs) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 22
use Assert;
# spent 21µs making 1 call to Foswiki::ListIterator::BEGIN@22 # spent 12µs making 1 call to Assert::import
23
24=begin TML
25
26---++ new(\@list)
27
28Create a new iterator over the given list. Designed primarily for operations
29over fully defined lists of object references. The list is not damaged in
30any way.
31
32=cut
33
34
# spent 3.96ms (3.40+568µs) within Foswiki::ListIterator::new which was called 235 times, avg 17µs/call: # 102 times (992µs+119µs) by Foswiki::Store::VC::Store::eachWeb at line 442 of /var/www/foswiki11/lib/Foswiki/Store/VC/Store.pm, avg 11µs/call # 81 times (1.84ms+381µs) by Foswiki::Search::InfoCache::new at line 51 of /var/www/foswiki11/lib/Foswiki/Search/InfoCache.pm, avg 27µs/call # 41 times (442µs+57µs) by Foswiki::Store::VC::Store::eachTopic at line 418 of /var/www/foswiki11/lib/Foswiki/Store/VC/Store.pm, avg 12µs/call # 4 times (34µs+4µs) by Foswiki::Users::BaseUserMapping::eachGroupMember at line 293 of /var/www/foswiki11/lib/Foswiki/Users/BaseUserMapping.pm, avg 10µs/call # 3 times (53µs+3µs) by Foswiki::Users::TopicUserMapping::eachGroupMember at line 729 of /var/www/foswiki11/lib/Foswiki/Users/TopicUserMapping.pm, avg 19µs/call # 3 times (26µs+3µs) by Foswiki::Users::TopicUserMapping::eachGroup at line 773 of /var/www/foswiki11/lib/Foswiki/Users/TopicUserMapping.pm, avg 10µs/call # once (12µs+1µs) by Foswiki::Users::TopicUserMapping::eachGroupMember at line 675 of /var/www/foswiki11/lib/Foswiki/Users/TopicUserMapping.pm
sub new {
35235301µs my ( $class, $list ) = @_;
36
3723590µs $list = [] unless defined $list;
38
39235546µs235568µs ASSERT( UNIVERSAL::isa( $list, 'ARRAY' ) ) if DEBUG;
# spent 568µs making 235 calls to Assert::ASSERTS_OFF, avg 2µs/call
40
412351.40ms my $this = bless(
42 {
43 list => $list,
44 index => 0,
45 process => undef,
46 filter => undef,
47 next => undef,
48 },
49 $class
50 );
51235917µs return $this;
52}
53
54=begin TML
55
56---++ hasNext() -> $boolean
57
58Returns false when the iterator is exhausted.
59
60<verbatim>
61my $it = new Foswiki::ListIterator(\@list);
62while ($it->hasNext()) {
63 ...
64</verbatim>
65
66=cut
67
68
# spent 420ms within Foswiki::ListIterator::hasNext which was called 141616 times, avg 3µs/call: # 70722 times (89.5ms+0s) by Foswiki::ListIterator::next at line 171, avg 1µs/call # 46206 times (224ms+0s) by Foswiki::Iterator::FilterIterator::hasNext at line 48 of /var/www/foswiki11/lib/Foswiki/Iterator/FilterIterator.pm, avg 5µs/call # 17686 times (74.3ms+0s) by Foswiki::Search::ResultSet::hasNext at line 93 of /var/www/foswiki11/lib/Foswiki/Search/ResultSet.pm, avg 4µs/call # 6358 times (28.3ms+0s) by Foswiki::Iterator::FilterIterator::hasNext at line 50 of /var/www/foswiki11/lib/Foswiki/Iterator/FilterIterator.pm, avg 4µs/call # 301 times (1.26ms+0s) by Foswiki::UserMapping::isInGroup at line 426 of /var/www/foswiki11/lib/Foswiki/UserMapping.pm, avg 4µs/call # 228 times (830µs+0s) by Foswiki::Users::TopicUserMapping::isGroup at line 757 of /var/www/foswiki11/lib/Foswiki/Users/TopicUserMapping.pm, avg 4µs/call # 100 times (891µs+0s) by Foswiki::deepWebList at line 1575 of /var/www/foswiki11/lib/Foswiki.pm, avg 9µs/call # 10 times (55µs+0s) by Foswiki::UserMapping::isInGroup at line 420 of /var/www/foswiki11/lib/Foswiki/UserMapping.pm, avg 6µs/call # 3 times (16µs+0s) by Foswiki::Users::TopicUserMapping::isGroup at line 755 of /var/www/foswiki11/lib/Foswiki/Users/TopicUserMapping.pm, avg 5µs/call # 2 times (19µs+0s) by Foswiki::deepWebList at line 1571 of /var/www/foswiki11/lib/Foswiki.pm, avg 10µs/call
sub hasNext {
6914161653.4ms my ($this) = @_;
70141616318ms return 1
71 if defined( $this->{next} )
72 ; #SMELL: this is still wrong if the array element == undef, but at least means zero is an element
73708947.94ms my $n;
74 do {
75 if ( $this->{list} && $this->{index} < scalar( @{ $this->{list} } ) ) {
76 $n = $this->{list}->[ $this->{index}++ ];
77 }
78 else {
79172796µs return 0;
80 }
8170894168ms } while ( $this->{filter} && !&{ $this->{filter} }($n) );
827072224.2ms $this->{next} = $n;
83 print STDERR "ListIterator::hasNext -> $this->{index} == $this->{next}\n"
84 if Foswiki::Iterator::MONITOR;
8570722361ms return 1;
86}
87
88=begin TML
89
90---++ skip(count) -> $countremaining
91
92skip X elements (returns 0 if successful, or number of elements remaining to skip if there are not enough elements to skip)
93skip must set up next as though hasNext was called.
94
95=cut
96
97sub skip {
98 my $this = shift;
99 my $count = shift;
100
101 if ( defined( $this->{next} ) ) {
102 $count--;
103 }
104
105 $count ||= 0;
106
107 return 0 if ( $count <= 0 );
108 print STDERR
109"--------------------------------------------ListIterator::skip($count) $this->{index}, "
110 . scalar( @{ $this->{list} } ) . "\n"
111 if Foswiki::Iterator::MONITOR;
112
113 my $length = scalar( @{ $this->{list} } );
114
115 if ( ( $this->{index} + $count ) >= $length ) {
116
117 #list too small
118 $count = $this->{index} + $count - $length;
119 $this->{index} = 1 + $length;
120 }
121 else {
122 $this->{index} += $count;
123 $count = 0;
124 }
125 $this->{next} = undef;
126 my $hasnext = $this->hasNext();
127 print STDERR
128"--------------------------------------------ListIterator::skip() => $this->{index} $count, $hasnext\n"
129 if Foswiki::Iterator::MONITOR;
130
131 return $count;
132}
133
134=begin TML
135
136---++ next() -> $data
137
138Return the next entry in the list.
139
140The iterator object can be customised to pre- and post-process entries from
141the list before returning them. This is done by setting two fields in the
142iterator object:
143
144 * ={filter}= can be defined to be a sub that filters each entry. The entry
145 will be ignored (next() will not return it) if the filter returns false.
146 * ={process}= can be defined to be a sub to process each entry before it
147 is returned by next. The value returned from next is the value returned
148 by the process function.
149
150For example,
151<verbatim>
152my @list = ( 1, 2, 3 );
153
154my $it = new Foswiki::ListIterator(\@list);
155$it->{filter} = sub { return $_[0] != 2 };
156$it->{process} = sub { return $_[0] + 1 };
157while ($it->hasNext()) {
158 my $x = $it->next();
159 print "$x, ";
160}
161</verbatim>
162will print
163<verbatim>
1642, 4
165</verbatim>
166
167=cut
168
169
# spent 574ms (484+89.5) within Foswiki::ListIterator::next which was called 70722 times, avg 8µs/call: # 52483 times (377ms+68.2ms) by Foswiki::Iterator::FilterIterator::hasNext at line 49 of /var/www/foswiki11/lib/Foswiki/Iterator/FilterIterator.pm, avg 8µs/call # 17605 times (104ms+20.5ms) by Foswiki::Search::ResultSet::hasNext at line 93 of /var/www/foswiki11/lib/Foswiki/Search/ResultSet.pm, avg 7µs/call # 303 times (1.50ms+376µs) by Foswiki::UserMapping::isInGroup at line 421 of /var/www/foswiki11/lib/Foswiki/UserMapping.pm, avg 6µs/call # 231 times (1.01ms+261µs) by Foswiki::Users::TopicUserMapping::isGroup at line 756 of /var/www/foswiki11/lib/Foswiki/Users/TopicUserMapping.pm, avg 5µs/call # 100 times (767µs+179µs) by Foswiki::deepWebList at line 1574 of /var/www/foswiki11/lib/Foswiki.pm, avg 9µs/call
sub next {
1707072219.6ms my $this = shift;
1717072262.9ms7072289.5ms $this->hasNext();
# spent 89.5ms making 70722 calls to Foswiki::ListIterator::hasNext, avg 1µs/call
1727072233.3ms my $n = $this->{next};
1737072224.5ms $this->{next} = undef;
1747072219.2ms $n = &{ $this->{process} }($n) if $this->{process};
17570722281ms return $n;
176}
177
178=begin TML
179
180---++ ObjectMethod all() -> @list
181
182Exhaust the iterator. Return all remaining elements in the iteration
183as a list. The returned list should be considered to be immutable.
184
185This method is cheap if it is called when the cursor is at the first
186element in the iteration, and expensive otherwise, as it requires a list
187copy to be made.
188
189=cut
190
191
# spent 310µs within Foswiki::ListIterator::all which was called 100 times, avg 3µs/call: # 100 times (310µs+0s) by Foswiki::Store::VC::Store::eachWeb at line 436 of /var/www/foswiki11/lib/Foswiki/Store/VC/Store.pm, avg 3µs/call
sub all {
19210026µs my $this = shift;
19310060µs if ( $this->{index} ) {
194 my @copy = @{ $this->{list} }; # don't damage the original list
195 splice( @copy, 0, $this->{index} );
196 $this->{index} = scalar( @{ $this->{list} } );
197 return @copy;
198 }
199 else {
200
201 # At the start (good)
20210059µs $this->{index} = scalar( @{ $this->{list} } );
203100261µs return @{ $this->{list} };
204 }
205}
206
207=begin TML
208
209---++ reset() -> $boolean
210
211Start at the begining of the list
212<verbatim>
213$it->reset();
214while ($it->hasNext()) {
215 ...
216</verbatim>
217
218=cut
219
220
# spent 271µs within Foswiki::ListIterator::reset which was called 81 times, avg 3µs/call: # 81 times (271µs+0s) by Foswiki::Iterator::FilterIterator::reset at line 70 of /var/www/foswiki11/lib/Foswiki/Iterator/FilterIterator.pm, avg 3µs/call
sub reset {
2218139µs my ($this) = @_;
2228137µs $this->{next} = undef;
2238130µs $this->{index} = 0;
224
22581228µs return 1;
226}
227
22813µs1;
229__END__