← 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:27:17 2011

Filename/usr/local/src/github.com/foswiki/core/lib/Foswiki/Render/Anchors.pm
StatementsExecuted 237 statements in 2.90ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
721686µs1.14msFoswiki::Render::Anchors::::makeFoswiki::Render::Anchors::make
92151268µs268µsFoswiki::Render::Anchors::::CORE:substFoswiki::Render::Anchors::CORE:subst (opcode)
611207µs2.20msFoswiki::Render::Anchors::::makeHTMLTargetFoswiki::Render::Anchors::makeHTMLTarget
611146µs146µsFoswiki::Render::Anchors::::addUniqueFoswiki::Render::Anchors::addUnique
2031103µs103µsFoswiki::Render::Anchors::::CORE:regcompFoswiki::Render::Anchors::CORE:regcomp (opcode)
51139µs39µsFoswiki::Render::Anchors::::clearFoswiki::Render::Anchors::clear
141133µs33µsFoswiki::Render::Anchors::::CORE:substcontFoswiki::Render::Anchors::CORE:substcont (opcode)
132127µs27µsFoswiki::Render::Anchors::::CORE:matchFoswiki::Render::Anchors::CORE:match (opcode)
11125µs33µsFoswiki::Render::Anchors::::BEGIN@16Foswiki::Render::Anchors::BEGIN@16
11123µs23µsFoswiki::Render::Anchors::::newFoswiki::Render::Anchors::new
11123µs149µsFoswiki::Render::Anchors::::addFoswiki::Render::Anchors::add
11117µs34µsFoswiki::Render::Anchors::::BEGIN@17Foswiki::Render::Anchors::BEGIN@17
11116µs63µsFoswiki::Render::Anchors::::BEGIN@18Foswiki::Render::Anchors::BEGIN@18
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::Render::Anchors
6
7Support for rendering anchors. Objects of this class represent
8a set of generated anchor names, which must be unique in a rendering
9context (topic). The renderer maintains a set of these objects, one
10for each topic, to ensure that anchor names are not re-used.
11
12=cut
13
14package Foswiki::Render::Anchors;
15
16248µs241µs
# spent 33µs (25+8) within Foswiki::Render::Anchors::BEGIN@16 which was called: # once (25µs+8µs) by Foswiki::Render::BEGIN@19 at line 16
use strict;
# spent 33µs making 1 call to Foswiki::Render::Anchors::BEGIN@16 # spent 8µs making 1 call to strict::import
17243µs252µs
# spent 34µs (17+18) within Foswiki::Render::Anchors::BEGIN@17 which was called: # once (17µs+18µs) by Foswiki::Render::BEGIN@19 at line 17
use warnings;
# spent 34µs making 1 call to Foswiki::Render::Anchors::BEGIN@17 # spent 18µs making 1 call to warnings::import
1821.23ms2110µs
# spent 63µs (16+47) within Foswiki::Render::Anchors::BEGIN@18 which was called: # once (16µs+47µs) by Foswiki::Render::BEGIN@19 at line 18
use Assert;
# spent 63µs making 1 call to Foswiki::Render::Anchors::BEGIN@18 # spent 47µs making 1 call to Assert::import
19
20=begin TML
21
22---++ ClassMethod new()
23
24Construct a new anchors set.
25
26=cut
27
28
# spent 23µs within Foswiki::Render::Anchors::new which was called: # once (23µs+0s) by Foswiki::Render::getAnchorNames at line 2206 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Render.pm
sub new {
29127µs return bless( { names => {} }, shift );
30}
31
32=begin TML
33
34---++ ObjectMethod clear()
35
36Clear the anchor set. Clearing the anchor set will cause it to forget
37any anchors generated to date.
38
39=cut
40
41
# spent 39µs within Foswiki::Render::Anchors::clear which was called 5 times, avg 8µs/call: # 5 times (39µs+0s) by Foswiki::Render::getRenderedVersion at line 1235 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Render.pm, avg 8µs/call
sub clear {
4258µs my $this = shift;
43538µs $this->{names} = {};
44}
45
46=begin TML
47
48---++ ObjectMethod add($text) -> $name
49Add a new anchor to the set. Return the name that was added.
50Note that if a name is added twice, it isn't an error, but only
51the one name is added.
52
53=cut
54
55
# spent 149µs (23+126) within Foswiki::Render::Anchors::add which was called: # once (23µs+126µs) by Foswiki::Render::getRenderedVersion at line 1240 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Render.pm
sub add {
5614µs my ( $this, $text ) = @_;
57110µs1126µs my $anchorName = make($text);
# spent 126µs making 1 call to Foswiki::Render::Anchors::make
5813µs $this->{names}->{$anchorName} = 1;
59111µs return $anchorName;
60}
61
62=begin TML
63
64---++ ObjectMethod addUnique($text [,$alreadyMade]) -> $uniqueName
65Add a new anchor to the set. if it's already present, rename it.
66
67If =$alreadyMade=, then $text is assumed to be a valid anchor name
68that was made by =make=.
69
70Return the name that was added.
71
72=cut
73
74
# spent 146µs within Foswiki::Render::Anchors::addUnique which was called 6 times, avg 24µs/call: # 6 times (146µs+0s) by Foswiki::Render::Anchors::makeHTMLTarget at line 188, avg 24µs/call
sub addUnique {
75615µs my ( $this, $text, $alreadyMade ) = @_;
7668µs my $anchorName;
77614µs if ($alreadyMade) {
78610µs $anchorName = $text;
79 }
80 else {
81 $anchorName = make($text);
82 }
8368µs my $cnt = 1;
8468µs my $suffix = '';
85
86622µs while ( exists $this->{names}->{ $anchorName . $suffix } ) {
87
88 # $anchorName.$suffix must _always_ be 'compatible', or things
89 # would get complicated (whatever that means)
90 $suffix = '_AN' . $cnt++;
91
92 # limit resulting name to 32 chars
93 $anchorName = substr( $anchorName, 0, 32 - length($suffix) );
94
95 # this is only needed because '__' would not be 'compatible'
96 $anchorName =~ s/_+$//g;
97 }
9868µs $anchorName .= $suffix;
99625µs $this->{names}->{$anchorName} = 1;
100640µs return $anchorName;
101}
102
103=begin TML
104
105---++ StaticMethod make( $text ) -> $name
106
107Make an anchor name from some text, subject to:
108 1 Given the same text, this function must always return the same
109 anchor name
110 2 NAME tokens must begin with a letter ([A-Za-z]) and may be
111 followed by any number of letters, digits ([0-9]), hyphens ("-"),
112 underscores ("_"), colons (":"), and periods (".").
113 (from http://www.w3.org/TR/html401/struct/links.html#h-12.2.1)
114
115The making process tranforms an arbitrary text string to a string that
116can legally be used for an HTML anchor.
117
118=cut
119
120
# spent 1.14ms (686µs+457µs) within Foswiki::Render::Anchors::make which was called 7 times, avg 163µs/call: # 6 times (620µs+397µs) by Foswiki::Render::Anchors::makeHTMLTarget at line 187, avg 170µs/call # once (66µs+61µs) by Foswiki::Render::Anchors::add at line 57
sub make {
121714µs my ($text) = @_;
122
1237174µs2184µs $text =~ s/^\s*(.*?)\s*$/$1/;
# spent 50µs making 7 calls to Foswiki::Render::Anchors::CORE:subst, avg 7µs/call # spent 33µs making 14 calls to Foswiki::Render::Anchors::CORE:substcont, avg 2µs/call
1247108µs1450µs $text =~ s/$Foswiki::regex{headerPatternNoTOC}//go;
# spent 31µs making 7 calls to Foswiki::Render::Anchors::CORE:regcomp, avg 4µs/call # spent 19µs making 7 calls to Foswiki::Render::Anchors::CORE:subst, avg 3µs/call
125
1267105µs1448µs if ( $text =~ /^$Foswiki::regex{anchorRegex}$/ ) {
# spent 33µs making 7 calls to Foswiki::Render::Anchors::CORE:regcomp, avg 5µs/call # spent 15µs making 7 calls to Foswiki::Render::Anchors::CORE:match, avg 2µs/call
127
128 # accept, already valid -- just remove leading #
129112µs return substr( $text, 1 );
130 }
131
132 # $anchorName is a *byte* string. If it contains any wide characters
133 # the encoding algorithm will not work.
134 #ASSERT($text !~ /[^\x00-\xFF]/) if DEBUG;
135640µs614µs $text =~ s/[^\x00-\xFF]//g;
# spent 14µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 2µs/call
136632µs626µs ASSERT( $text !~ /[^\x00-\xFF]/ ) if DEBUG;
# spent 26µs making 6 calls to Assert::ASSERTS_OFF, avg 4µs/call
137
138 # SMELL: This corrects for anchors containing < and >
139 # which for some reason are encoded when building the anchor, but
140 # un-encoded when building the link.
141 #
142 # Convert &, < and > back from entity
143641µs615µs $text =~ s/&lt;/</g;
# spent 15µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 2µs/call
144633µs69µs $text =~ s/&gt;/>/g;
# spent 9µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 1µs/call
145632µs68µs $text =~ s/&amp;/&/g;
# spent 8µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 1µs/call
146
147 # strip out potential links so they don't get rendered.
148 # remove double bracket link
149634µs611µs $text =~ s/\[(?:\[.*?\])?\[(.*?)\]\s*\]/$1/g;
# spent 11µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 2µs/call
150
151 # remove HTML tags and entities
152642µs618µs $text =~ s/<\/?[a-zA-Z][^>]*>//gi;
# spent 18µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 3µs/call
153633µs69µs $text =~ s/&#?[a-zA-Z0-9]+;//g;
# spent 9µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 1µs/call
154
155 # remove escape from escaped wikiWords
156696µs1249µs $text =~
# spent 39µs making 6 calls to Foswiki::Render::Anchors::CORE:regcomp, avg 6µs/call # spent 10µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 2µs/call
157 s/!($Foswiki::regex{wikiWordRegex}|$Foswiki::regex{abbrevRegex})/$1/go;
158
159 # remove spaces
160657µs629µs $text =~ s/\s+/_/g;
# spent 29µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 5µs/call
161
162 # use _ as an escape character to escape any byte outside the
163 # range specified by http://www.w3.org/TR/html401/struct/links.html
164637µs613µs $text =~ s/([^A-Za-z0-9:._])/'_'.sprintf('%02d', ord($1))/ge;
# spent 13µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 2µs/call
165
166 # clean up a bit
167633µs610µs $text =~ s/__/_/g;
# spent 10µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 2µs/call
168640µs615µs $text =~ s/^_*//;
# spent 15µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 3µs/call
169666µs639µs $text =~ s/_*$//;
# spent 39µs making 6 calls to Foswiki::Render::Anchors::CORE:subst, avg 6µs/call
170
171 # Ensure the anchor always starts with an [A-Za-z]
172638µs612µs $text = 'A_' . $text unless $text =~ /^[A-Za-z]/;
# spent 12µs making 6 calls to Foswiki::Render::Anchors::CORE:match, avg 2µs/call
173
174665µs return $text;
175}
176
177=begin TML
178
179---++ ObjectMethod makeHTMLTarget($name) -> $html
180Make an HTML anchor that can be used as the target of links.
181
182=cut
183
184
# spent 2.20ms (207µs+1.99) within Foswiki::Render::Anchors::makeHTMLTarget which was called 6 times, avg 366µs/call: # 6 times (207µs+1.99ms) by Foswiki::Render::_makeAnchorHeading at line 452 of /usr/local/src/github.com/foswiki/core/lib/Foswiki/Render.pm, avg 366µs/call
sub makeHTMLTarget {
185614µs my ( $this, $text ) = @_;
186
187635µs61.02ms my $goodAnchor = make($text);
# spent 1.02ms making 6 calls to Foswiki::Render::Anchors::make, avg 170µs/call
188687µs12973µs my $html = CGI::a( { name => $this->addUnique( $goodAnchor, 1 ) }, '' );
# spent 827µs making 6 calls to CGI::a, avg 138µs/call # spent 146µs making 6 calls to Foswiki::Render::Anchors::addUnique, avg 24µs/call
189
190611µs if ( $Foswiki::cfg{RequireCompatibleAnchors} ) {
191
192 # Add in extra anchors compatible with old formats, as required
193 require Foswiki::Compatibility;
194 my @extras = Foswiki::Compatibility::makeCompatibleAnchors($text);
195 foreach my $extra (@extras) {
196 next if ( $extra eq $goodAnchor );
197 $html .= CGI::a( { name => $this->addUnique( $extra, 1 ), }, '' );
198 }
199 }
200638µs return $html;
201}
202
20314µs1;
204__END__
 
# spent 27µs within Foswiki::Render::Anchors::CORE:match which was called 13 times, avg 2µs/call: # 7 times (15µs+0s) by Foswiki::Render::Anchors::make at line 126, avg 2µs/call # 6 times (12µs+0s) by Foswiki::Render::Anchors::make at line 172, avg 2µs/call
sub Foswiki::Render::Anchors::CORE:match; # opcode
# spent 103µs within Foswiki::Render::Anchors::CORE:regcomp which was called 20 times, avg 5µs/call: # 7 times (33µs+0s) by Foswiki::Render::Anchors::make at line 126, avg 5µs/call # 7 times (31µs+0s) by Foswiki::Render::Anchors::make at line 124, avg 4µs/call # 6 times (39µs+0s) by Foswiki::Render::Anchors::make at line 156, avg 6µs/call
sub Foswiki::Render::Anchors::CORE:regcomp; # opcode
# spent 268µs within Foswiki::Render::Anchors::CORE:subst which was called 92 times, avg 3µs/call: # 7 times (50µs+0s) by Foswiki::Render::Anchors::make at line 123, avg 7µs/call # 7 times (19µs+0s) by Foswiki::Render::Anchors::make at line 124, avg 3µs/call # 6 times (39µs+0s) by Foswiki::Render::Anchors::make at line 169, avg 6µs/call # 6 times (29µs+0s) by Foswiki::Render::Anchors::make at line 160, avg 5µs/call # 6 times (18µs+0s) by Foswiki::Render::Anchors::make at line 152, avg 3µs/call # 6 times (15µs+0s) by Foswiki::Render::Anchors::make at line 168, avg 3µs/call # 6 times (15µs+0s) by Foswiki::Render::Anchors::make at line 143, avg 2µs/call # 6 times (14µs+0s) by Foswiki::Render::Anchors::make at line 135, avg 2µs/call # 6 times (13µs+0s) by Foswiki::Render::Anchors::make at line 164, avg 2µs/call # 6 times (11µs+0s) by Foswiki::Render::Anchors::make at line 149, avg 2µs/call # 6 times (10µs+0s) by Foswiki::Render::Anchors::make at line 156, avg 2µs/call # 6 times (10µs+0s) by Foswiki::Render::Anchors::make at line 167, avg 2µs/call # 6 times (9µs+0s) by Foswiki::Render::Anchors::make at line 153, avg 1µs/call # 6 times (9µs+0s) by Foswiki::Render::Anchors::make at line 144, avg 1µs/call # 6 times (8µs+0s) by Foswiki::Render::Anchors::make at line 145, avg 1µs/call
sub Foswiki::Render::Anchors::CORE:subst; # opcode
# spent 33µs within Foswiki::Render::Anchors::CORE:substcont which was called 14 times, avg 2µs/call: # 14 times (33µs+0s) by Foswiki::Render::Anchors::make at line 123, avg 2µs/call
sub Foswiki::Render::Anchors::CORE:substcont; # opcode