← Index
NYTProf Performance Profile   « block view • line view • sub view »
For /usr/local/src/github.com/foswiki/core/bin/view
  Run on Sun Dec 4 17:17:59 2011
Reported on Sun Dec 4 17:26:45 2011

Filename/usr/local/src/github.com/foswiki/core/lib/Foswiki/ListIterator.pm
StatementsExecuted 66781 statements in 182ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
7851127109ms109msFoswiki::ListIterator::::hasNextFoswiki::ListIterator::hasNext
39206562.6ms82.7msFoswiki::ListIterator::::nextFoswiki::ListIterator::next
1397371µs421µsFoswiki::ListIterator::::newFoswiki::ListIterator::new
11125µs32µsFoswiki::ListIterator::::BEGIN@16Foswiki::ListIterator::BEGIN@16
11116µs53µsFoswiki::ListIterator::::BEGIN@22Foswiki::ListIterator::BEGIN@22
11116µs34µsFoswiki::ListIterator::::BEGIN@17Foswiki::ListIterator::BEGIN@17
11112µs12µsFoswiki::ListIterator::::resetFoswiki::ListIterator::reset
1119µs9µsFoswiki::ListIterator::::BEGIN@19Foswiki::ListIterator::BEGIN@19
0000s0sFoswiki::ListIterator::::allFoswiki::ListIterator::all
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;
16245µs239µs
# spent 32µs (25+7) within Foswiki::ListIterator::BEGIN@16 which was called: # once (25µs+7µs) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 16
use strict;
# spent 32µs making 1 call to Foswiki::ListIterator::BEGIN@16 # spent 7µs making 1 call to strict::import
17243µs252µs
# spent 34µs (16+18) within Foswiki::ListIterator::BEGIN@17 which was called: # once (16µs+18µs) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 17
use warnings;
# spent 34µs making 1 call to Foswiki::ListIterator::BEGIN@17 # spent 18µs making 1 call to warnings::import
18
19263µs19µs
# spent 9µs within Foswiki::ListIterator::BEGIN@19 which was called: # once (9µs+0s) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 19
use Foswiki::Iterator ();
# spent 9µs making 1 call to Foswiki::ListIterator::BEGIN@19
2019µsour @ISA = ('Foswiki::Iterator');
21
222956µs290µs
# spent 53µs (16+37) within Foswiki::ListIterator::BEGIN@22 which was called: # once (16µs+37µs) by Foswiki::Users::TopicUserMapping::BEGIN@35 at line 22
use Assert;
# spent 53µs making 1 call to Foswiki::ListIterator::BEGIN@22 # spent 37µ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 421µs (371+49) within Foswiki::ListIterator::new which was called 13 times, avg 32µs/call: # 3 times (52µs+10µs) by Foswiki::Users::BaseUserMapping::eachGroupMember at line 291 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/BaseUserMapping.pm, avg 21µs/call # 2 times (70µs+7µs) by Foswiki::Users::TopicUserMapping::eachGroup at line 719 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm, avg 38µs/call # 2 times (45µs+7µs) by Foswiki::Users::TopicUserMapping::eachGroupMember at line 675 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm, avg 26µs/call # once (45µs+6µs) by Foswiki::Store::VC::Store::eachTopic at line 496 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Store/VC/Store.pm # once (45µs+4µs) by Foswiki::Users::HtPasswdUser::fetchUsers at line 141 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/HtPasswdUser.pm # once (41µs+4µs) by Foswiki::Store::Interfaces::QueryAlgorithm::getWebIterator at line 206 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Store/Interfaces/QueryAlgorithm.pm # once (29µs+4µs) by Foswiki::Search::InfoCache::new at line 53 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Search/InfoCache.pm # once (26µs+4µs) by Foswiki::__ANON__[/usr/local/src/github.com/foswiki/core/lib/Foswiki/Macros/FORMAT.pm:65] at line 45 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Macros/FORMAT.pm # once (19µs+3µs) by Foswiki::Users::TopicUserMapping::eachGroupMember at line 624 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm
sub new {
3552359µs my ( $class, $list ) = @_;
36
371349µs ASSERT( !defined($list) || UNIVERSAL::isa( $list, 'ARRAY' ) ) if DEBUG;
# spent 49µs making 13 calls to Assert::ASSERTS_OFF, avg 4µs/call
38
39 my $this = bless(
40 {
41 list => $list,
42 index => 0,
43 process => undef,
44 filter => undef,
45 next => undef,
46 },
47 $class
48 );
49 return $this;
50}
51
52=begin TML
53
54---++ hasNext() -> $boolean
55
56Returns false when the iterator is exhausted.
57
58<verbatim>
59my $it = new Foswiki::ListIterator(\@list);
60while ($it->hasNext()) {
61 ...
62</verbatim>
63
64=cut
65
66
# spent 109ms within Foswiki::ListIterator::hasNext which was called 7851 times, avg 14µs/call: # 3920 times (20.1ms+0s) by Foswiki::ListIterator::next at line 165, avg 5µs/call # 3403 times (76.8ms+0s) by Foswiki::Iterator::FilterIterator::hasNext at line 66 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Iterator/FilterIterator.pm, avg 23µs/call # 401 times (9.30ms+0s) by Foswiki::Users::TopicUserMapping::_loadMapping at line 1600 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm, avg 23µs/call # 26 times (578µs+0s) by Foswiki::Users::TopicUserMapping::isGroup at line 703 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm, avg 22µs/call # 25 times (581µs+0s) by Foswiki::Iterator::FilterIterator::hasNext at line 64 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Iterator/FilterIterator.pm, avg 23µs/call # 25 times (564µs+0s) by Foswiki::UserMapping::isInGroup at line 408 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/UserMapping.pm, avg 23µs/call # 23 times (586µs+0s) by Foswiki::Search::ResultSet::hasNext at line 98 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Search/ResultSet.pm, avg 25µs/call # 22 times (463µs+0s) by Foswiki::AggregateIterator::hasNext at line 70 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/AggregateIterator.pm, avg 21µs/call # 2 times (60µs+0s) by Foswiki::Users::TopicUserMapping::isGroup at line 701 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm, avg 30µs/call # 2 times (51µs+0s) by Foswiki::UserMapping::isInGroup at line 402 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/UserMapping.pm, avg 25µs/call # once (32µs+0s) by Foswiki::Users::TopicUserMapping::_loadMapping at line 1598 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm # once (17µs+0s) by Foswiki::Search::formatResults at line 711 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Search.pm
sub hasNext {
673140478.1ms my ($this) = @_;
68 return 1
69 if defined( $this->{next} )
70 ; #SMELL: this is still wrong if the array element == undef, but at least means zero is an element
71 my $n;
72 do {
73786014.3ms if ( $this->{list} && $this->{index} < scalar( @{ $this->{list} } ) ) {
74 $n = $this->{list}->[ $this->{index}++ ];
75 }
76 else {
77 return 0;
78 }
79393128.0ms } while ( $this->{filter} && !&{ $this->{filter} }($n) );
80 $this->{next} = $n;
81 print STDERR "ListIterator::hasNext -> $this->{index} == $this->{next}\n"
82 if Foswiki::Iterator::MONITOR;
83 return 1;
84}
85
86=begin TML
87
88---++ skip(count) -> $countremaining
89
90skip X elements (returns 0 if successful, or number of elements remaining to skip if there are not enough elements to skip)
91skip must set up next as though hasNext was called.
92
93=cut
94
95sub skip {
96 my $this = shift;
97 my $count = shift;
98
99 if ( defined( $this->{next} ) ) {
100 $count--;
101 }
102
103 return 0 if ( $count <= 0 );
104 print STDERR
105"--------------------------------------------ListIterator::skip($count) $this->{index}, "
106 . scalar( @{ $this->{list} } ) . "\n"
107 if Foswiki::Iterator::MONITOR;
108
109 if ( ( $this->{index} + $count ) >= scalar( @{ $this->{list} } ) ) {
110
111 #list too small
112 $count = ( $this->{index} + $count ) - scalar( @{ $this->{list} } );
113 $this->{index} = 1 + scalar( @{ $this->{list} } );
114 }
115 else {
116 $this->{index} += $count;
117 $count = 0;
118 }
119 $this->{next} = undef;
120 my $hasnext = $this->hasNext();
121 print STDERR
122"--------------------------------------------ListIterator::skip() => $this->{index} $count, $hasnext\n"
123 if Foswiki::Iterator::MONITOR;
124
125 return $count;
126}
127
128=begin TML
129
130---++ next() -> $data
131
132Return the next entry in the list.
133
134The iterator object can be customised to pre- and post-process entries from
135the list before returning them. This is done by setting two fields in the
136iterator object:
137
138 * ={filter}= can be defined to be a sub that filters each entry. The entry
139 will be ignored (next() will not return it) if the filter returns false.
140 * ={process}= can be defined to be a sub to process each entry before it
141 is returned by next. The value returned from next is the value returned
142 by the process function.
143
144For example,
145<verbatim>
146my @list = ( 1, 2, 3 );
147
148my $it = new Foswiki::ListIterator(\@list);
149$it->{filter} = sub { return $_[0] != 2 };
150$it->{process} = sub { return $_[0] + 1 };
151while ($it->hasNext()) {
152 my $x = $it->next();
153 print "$x, ";
154}
155</verbatim>
156will print
157<verbatim>
1582, 4
159</verbatim>
160
161=cut
162
163
# spent 82.7ms (62.6+20.1) within Foswiki::ListIterator::next which was called 3920 times, avg 21µs/call: # 3426 times (54.6ms+17.6ms) by Foswiki::Iterator::FilterIterator::hasNext at line 65 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Iterator/FilterIterator.pm, avg 21µs/call # 401 times (6.39ms+2.03ms) by Foswiki::Users::TopicUserMapping::_loadMapping at line 1599 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm, avg 21µs/call # 28 times (436µs+205µs) by Foswiki::Users::TopicUserMapping::isGroup at line 702 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Users/TopicUserMapping.pm, avg 23µs/call # 25 times (426µs+127µs) by Foswiki::UserMapping::isInGroup at line 403 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/UserMapping.pm, avg 22µs/call # 22 times (408µs+116µs) by Foswiki::Search::ResultSet::hasNext at line 99 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Search/ResultSet.pm, avg 24µs/call # 18 times (289µs+97µs) by Foswiki::AggregateIterator::hasNext at line 71 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/AggregateIterator.pm, avg 21µs/call
sub next {
1642352060.3ms my $this = shift;
165392020.1ms $this->hasNext();
# spent 20.1ms making 3920 calls to Foswiki::ListIterator::hasNext, avg 5µs/call
166 my $n = $this->{next};
167 $this->{next} = undef;
168 $n = &{ $this->{process} }($n) if $this->{process};
169 return $n;
170}
171
172=begin TML
173
174---++ ObjectMethod all() -> @list
175
176Exhaust the iterator. Return all remaining elements in the iteration
177as a list. The returned list should be considered to be immutable.
178
179This method is cheap if it is called when the cursor is at the first
180element in the iteration, and expensive otherwise, as it requires a list
181copy to be made.
182
183=cut
184
185sub all {
186 my $this = shift;
187 if ( $this->{index} ) {
188 my @copy = @{ $this->{list} }; # don't damage the original list
189 splice( @copy, 0, $this->{index} );
190 $this->{index} = scalar( @{ $this->{list} } );
191 return @copy;
192 }
193 else {
194
195 # At the start (good)
196 $this->{index} = scalar( @{ $this->{list} } );
197 return @{ $this->{list} };
198 }
199}
200
201=begin TML
202
203---++ reset() -> $boolean
204
205Start at the begining of the list
206<verbatim>
207$it->reset();
208while ($it->hasNext()) {
209 ...
210</verbatim>
211
212=cut
213
214
# spent 12µs within Foswiki::ListIterator::reset which was called: # once (12µs+0s) by Foswiki::Iterator::FilterIterator::reset at line 127 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Iterator/FilterIterator.pm
sub reset {
215416µs my ($this) = @_;
216 $this->{next} = undef;
217 $this->{index} = 0;
218
219 return 1;
220}
221
22216µs1;
223__END__