Profile of utf8_heavy.pl

← Index
NYTProf Performance Profile «line view»
For t/optimization.t
Run on Thu Jan 8 22:47:42 2015
Reported on Thu Jan 8 22:48:05 2015

Filename | /home/ss5/perl5/perlbrew/perls/tapper-perl/lib/5.16.3/utf8_heavy.pl
Statements | Executed 3300 statements in 14.8ms

Subroutines
Calls | P | F | Exclusive Time | Inclusive Time | Subroutine
--- | --- | --- | --- | --- | ---
2 | 2 | 2 | 13.3ms | 14.3ms | utf8::::SWASHNEWutf8::SWASHNEW
634 | 4 | 1 | 1.02ms | 1.02ms | utf8::::CORE:matchutf8::CORE:match (opcode)
1 | 1 | 1 | 14µs | 27µs | utf8::::BEGIN@542utf8::BEGIN@542
1 | 1 | 1 | 13µs | 30µs | utf8::::BEGIN@147utf8::BEGIN@147
11 | 7 | 1 | 13µs | 13µs | utf8::::CORE:substutf8::CORE:subst (opcode)
1 | 1 | 1 | 13µs | 27µs | utf8::::BEGIN@654utf8::BEGIN@654
1 | 1 | 1 | 12µs | 25µs | utf8::::BEGIN@153utf8::BEGIN@153
1 | 1 | 1 | 11µs | 26µs | utf8::::BEGIN@2utf8::BEGIN@2
1 | 1 | 1 | 10µs | 11µs | utf8::::_loose_nameutf8::_loose_name
1 | 1 | 1 | 10µs | 14µs | utf8::::BEGIN@3utf8::BEGIN@3
1 | 1 | 1 | 5µs | 5µs | utf8::::BEGIN@166utf8::BEGIN@166
1 | 1 | 1 | 5µs | 5µs | utf8::::BEGIN@178utf8::BEGIN@178
1 | 1 | 1 | 2µs | 2µs | utf8::::upgradeutf8::upgrade (xsub)
0 | 0 | 0 | 0s | 0s | utf8::::DESTROYutf8::DESTROY
0 | 0 | 0 | 0s | 0s | utf8::::croakutf8::croak

Call graph for these subroutines as a Graphviz dot language file.

Line | State ments | Time on line | Calls | Time in subs | Code
--- | --- | --- | --- | --- | ---
1 |  |  |  |  | package utf8;
2 | 2 | 25µs | 2 | 40µs | # spent 26µs (11+15) within utf8::BEGIN@2 which was called: # once (11µs+15µs) by utf8::AUTOLOAD at line 2 use strict; # spent 26µs making 1 call to utf8::BEGIN@2 # spent 15µs making 1 call to strict::import
3 | 2 | 498µs | 2 | 19µs | # spent 14µs (10+5) within utf8::BEGIN@3 which was called: # once (10µs+5µs) by utf8::AUTOLOAD at line 3 use warnings; # spent 14µs making 1 call to utf8::BEGIN@3 # spent 5µs making 1 call to warnings::import
4 |  |  |  |  |
5 |  |  |  |  | sub DEBUG () { 0 }
6 |  |  |  |  | $|=1 if DEBUG;
7 |  |  |  |  |
8 |  |  |  |  | sub DESTROY {}
9 |  |  |  |  |
10 | 1 | 300ns |  |  | my %Cache;
11 |  |  |  |  |
12 |  |  |  |  | sub croak { require Carp; Carp::croak(@_) }
13 |  |  |  |  |
14 |  |  |  |  | # spent 11µs (10+1) within utf8::_loose_name which was called: # once (10µs+1µs) by utf8::SWASHNEW at line 399 sub _loose_name ($) {
15 |  |  |  |  | # Given a lowercase property or property-value name, return its
16 |  |  |  |  | # standardized version that is expected for look-up in the 'loose' hashes
17 |  |  |  |  | # in Heavy.pl (hence, this depends on what mktables does). This squeezes
18 |  |  |  |  | # out blanks, underscores and dashes. The complication stems from the
19 |  |  |  |  | # grandfathered-in 'L_', which retains a single trailing underscore.
20 |  |  |  |  |
21 | 1 | 5µs | 1 | 700ns | my $loose = $_[0] =~ s/[-\s_]//rg; # spent 700ns making 1 call to utf8::CORE:subst
22 |  |  |  |  |
23 | 1 | 8µs | 1 | 600ns | return $loose if $loose !~ / ^ (?: is | to )? l $/x; # spent 600ns making 1 call to utf8::CORE:match
24 |  |  |  |  | return 'l_' if $_[0] =~ / l .* _ /x; # If original had a trailing '_'
25 |  |  |  |  | return $loose;
26 |  |  |  |  | }
27 |  |  |  |  |
28 |  |  |  |  | ##
29 |  |  |  |  | ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape.
30 |  |  |  |  | ## It's a data structure that encodes a set of Unicode characters.
31 |  |  |  |  | ##
32 |  |  |  |  |
33 |  |  |  |  | {
34 |  |  |  |  | # If a floating point number is within this distance from the value of a
35 |  |  |  |  | # fraction, it is considered to be that fraction, even if many more digits
36 |  |  |  |  | # are specified that don't exactly match.
37 | 2 | 600ns |  |  | my $min_floating_slop;
38 |  |  |  |  |
39 |  |  |  |  | # To guard against this program calling something that in turn ends up
40 |  |  |  |  | # calling this program with the same inputs, and hence infinitely
41 |  |  |  |  | # recursing, we keep a stack of the properties that are currently in
42 |  |  |  |  | # progress, pushed upon entry, popped upon return.
43 | 1 | 800ns |  |  | my @recursed;
44 |  |  |  |  |
45 |  |  |  |  | # spent 14.3ms (13.3+1.04) within utf8::SWASHNEW which was called 2 times, avg 7.15ms/call: # once (9.62ms+7µs) by constant::BEGIN@25 at line 18 of utf8.pm # once (3.63ms+1.03ms) by Safe::CORE:regcomp at line 70 of Safe.pm sub SWASHNEW {
46 | 2 | 3µs |  |  | my ($class, $type, $list, $minbits, $none) = @_;
47 | 2 | 600ns |  |  | my $user_defined = 0;
48 | 2 | 2µs |  |  | local $^D = 0 if $^D;
49 |  |  |  |  |
50 | 2 | 800ns |  |  | $class = "" unless defined $class;
51 |  |  |  |  | print STDERR __LINE__, ": class=$class, type=$type, list=",
52 |  |  |  |  | (defined $list) ? $list : ':undef:',
53 |  |  |  |  | ", minbits=$minbits, none=$none\n" if DEBUG;
54 |  |  |  |  |
55 |  |  |  |  | ##
56 |  |  |  |  | ## Get the list of codepoints for the type.
57 |  |  |  |  | ## Called from swash_init (see utf8.c) or SWASHNEW itself.
58 |  |  |  |  | ##
59 |  |  |  |  | ## Callers of swash_init:
60 |  |  |  |  | ## op.c:pmtrans -- for tr/// and y///
61 |  |  |  |  | ## regexec.c:regclass_swash -- for /[]/, \p, and \P
62 |  |  |  |  | ## utf8.c:is_utf8_common -- for common Unicode properties
63 |  |  |  |  | ## utf8.c:to_utf8_case -- for lc, uc, ucfirst, etc. and //i
64 |  |  |  |  | ## Unicode::UCD::prop_invlist
65 |  |  |  |  | ## Unicode::UCD::prop_invmap
66 |  |  |  |  | ##
67 |  |  |  |  | ## Given a $type, our goal is to fill $list with the set of codepoint
68 |  |  |  |  | ## ranges. If $type is false, $list passed is used.
69 |  |  |  |  | ##
70 |  |  |  |  | ## $minbits:
71 |  |  |  |  | ## For binary properties, $minbits must be 1.
72 |  |  |  |  | ## For character mappings (case and transliteration), $minbits must
73 |  |  |  |  | ## be a number except 1.
74 |  |  |  |  | ##
75 |  |  |  |  | ## $list (or that filled according to $type):
76 |  |  |  |  | ## Refer to perlunicode.pod, "User-Defined Character Properties."
77 |  |  |  |  | ##
78 |  |  |  |  | ## For binary properties, only characters with the property value
79 |  |  |  |  | ## of True should be listed. The 3rd column, if any, will be ignored
80 |  |  |  |  | ##
81 |  |  |  |  | ## $none is undocumented, so I'm (khw) trying to do some documentation
82 |  |  |  |  | ## of it now. It appears to be if there is a mapping in an input file
83 |  |  |  |  | ## that maps to 'XXXX', then that is replaced by $none+1, expressed in
84 |  |  |  |  | ## hexadecimal. It is used somehow in tr///.
85 |  |  |  |  | ##
86 |  |  |  |  | ## To make the parsing of $type clear, this code takes the a rather
87 |  |  |  |  | ## unorthodox approach of last'ing out of the block once we have the
88 |  |  |  |  | ## info we need. Were this to be a subroutine, the 'last' would just
89 |  |  |  |  | ## be a 'return'.
90 |  |  |  |  | ##
91 |  |  |  |  | # If a problem is found $type is returned;
92 |  |  |  |  | # Upon success, a new (or cached) blessed object is returned with
93 |  |  |  |  | # keys TYPE, BITS, EXTRAS, LIST, and NONE with values having the
94 |  |  |  |  | # same meanings as the input parameters.
95 |  |  |  |  | # SPECIALS contains a reference to any special-treatment hash in the
96 |  |  |  |  | # INVERT_IT is non-zero if the result should be inverted before use
97 |  |  |  |  | # USER_DEFINED is non-zero if the result came from a user-defined
98 |  |  |  |  | # property.
99 | 2 | 400ns |  |  | my $file; ## file to load data from, and also part of the %Cache key.
100 |  |  |  |  |
101 |  |  |  |  | # Change this to get a different set of Unicode tables
102 | 2 | 800ns |  |  | my $unicore_dir = 'unicore';
103 | 2 | 500ns |  |  | my $invert_it = 0;
104 | 2 | 400ns |  |  | my $list_is_from_mktables = 0; # Is $list returned from a mktables
105 |  |  |  |  | # generated file? If so, we know it's
106 |  |  |  |  | # well behaved.
107 |  |  |  |  |
108 | 2 | 1µs |  |  | if ($type)
109 |  |  |  |  | {
110 |  |  |  |  | # Verify that this isn't a recursive call for this property.
111 |  |  |  |  | # Can't use croak, as it may try to recurse to here itself.
112 | 2 | 2µs |  |  | my $class_type = $class . "::$type";
113 | 2 | 2µs |  |  | if (grep { $_ eq $class_type } @recursed) {
114 |  |  |  |  | CORE::die "panic: Infinite recursion in SWASHNEW for '$type'\n";
115 |  |  |  |  | }
116 | 2 | 2µs |  |  | push @recursed, $class_type;
117 |  |  |  |  |
118 | 2 | 13µs | 2 | 4µs | $type =~ s/^\s+//; # spent 4µs making 2 calls to utf8::CORE:subst, avg 2µs/call
119 | 2 | 7µs | 2 | 2µs | $type =~ s/\s+$//; # spent 2µs making 2 calls to utf8::CORE:subst, avg 1µs/call
120 |  |  |  |  |
121 |  |  |  |  | # regcomp.c surrounds the property name with '__" and '_i' if this
122 |  |  |  |  | # is to be caseless matching.
123 | 2 | 5µs | 2 | 1µs | my $caseless = $type =~ s/^(.*)__(.*)_i$/$1$2/; # spent 1µs making 2 calls to utf8::CORE:subst, avg 550ns/call
124 |  |  |  |  |
125 |  |  |  |  | print STDERR __LINE__, ": type=$type, caseless=$caseless\n" if DEBUG;
126 |  |  |  |  |
127 | 2 | 2µs |  |  | GETFILE:
128 |  |  |  |  | {
129 |  |  |  |  | ##
130 |  |  |  |  | ## It could be a user-defined property. Look in current
131 |  |  |  |  | ## package if no package given
132 |  |  |  |  | ##
133 |  |  |  |  |
134 |  |  |  |  |
135 | 2 | 600ns |  |  | my $caller0 = caller(0);
136 | 2 | 8µs | 2 | 1µs | my $caller1 = $type =~ s/(.+)::// ? $1 : $caller0 eq 'main' ? # spent 1µs making 2 calls to utf8::CORE:subst, avg 500ns/call
137 |  |  |  |  | 'main' : caller(1);
138 |  |  |  |  |
139 | 2 | 10µs | 2 | 1µs | if (defined $caller1 && $type =~ /^I[ns]\w+$/) { # spent 1µs making 2 calls to utf8::CORE:match, avg 650ns/call
140 |  |  |  |  | my $prop = "${caller1}::$type";
141 |  |  |  |  | if (exists &{$prop}) {
142 |  |  |  |  | # stolen from Scalar::Util::PP::tainted()
143 |  |  |  |  | my $tainted;
144 |  |  |  |  | {
145 |  |  |  |  | local($@, $SIG{__DIE__}, $SIG{__WARN__});
146 |  |  |  |  | local $^W = 0;
147 | 2 | 72µs | 2 | 46µs | # spent 30µs (13+16) within utf8::BEGIN@147 which was called: # once (13µs+16µs) by utf8::AUTOLOAD at line 147 no warnings; # spent 30µs making 1 call to utf8::BEGIN@147 # spent 16µs making 1 call to warnings::unimport
148 |  |  |  |  | eval { kill 0 * $prop };
149 |  |  |  |  | $tainted = 1 if $@ =~ /^Insecure/;
150 |  |  |  |  | }
151 |  |  |  |  | die "Insecure user-defined property \\p{$prop}\n"
152 |  |  |  |  | if $tainted;
153 | 2 | 63µs | 2 | 38µs | # spent 25µs (12+13) within utf8::BEGIN@153 which was called: # once (12µs+13µs) by utf8::AUTOLOAD at line 153 no strict 'refs'; # spent 25µs making 1 call to utf8::BEGIN@153 # spent 13µs making 1 call to strict::unimport
154 |  |  |  |  | $list = &{$prop}($caseless);
155 |  |  |  |  | $user_defined = 1;
156 |  |  |  |  | last GETFILE;
157 |  |  |  |  | }
158 |  |  |  |  | }
159 |  |  |  |  |
160 |  |  |  |  | # During Perl's compilation, this routine may be called before
161 |  |  |  |  | # the tables are constructed. If so, we have a chicken/egg
162 |  |  |  |  | # problem. If we die, the tables never get constructed, so
163 |  |  |  |  | # keep going, but return an empty table so only what the code
164 |  |  |  |  | # has compiled in internally (currently ASCII/Latin1 range
165 |  |  |  |  | # matching) will work.
166 |  |  |  |  | # spent 5µs within utf8::BEGIN@166 which was called: # once (5µs+0s) by utf8::AUTOLOAD at line 170 BEGIN {
167 |  |  |  |  | # Poor man's constant, to avoid a run-time check.
168 | 1 | 6µs |  |  | $utf8::{miniperl}
169 |  |  |  |  | = \! defined &DynaLoader::boot_DynaLoader;
170 | 1 | 58µs | 1 | 5µs | } # spent 5µs making 1 call to utf8::BEGIN@166
171 | 2 | 3µs |  |  | if (miniperl) {
172 |  |  |  |  | eval "require '$unicore_dir/Heavy.pl'";
173 |  |  |  |  | last GETFILE if $@;
174 |  |  |  |  | }
175 |  |  |  |  | else {
176 | 2 | 6.66ms |  |  | require "$unicore_dir/Heavy.pl";
177 |  |  |  |  | }
178 | 1 | 1.35ms | 1 | 5µs | # spent 5µs within utf8::BEGIN@178 which was called: # once (5µs+0s) by utf8::AUTOLOAD at line 178 BEGIN { delete $utf8::{miniperl} } # spent 5µs making 1 call to utf8::BEGIN@178
179 |  |  |  |  |
180 |  |  |  |  | # All property names are matched caselessly
181 | 2 | 3µs |  |  | my $property_and_table = CORE::lc $type;
182 |  |  |  |  | print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
183 |  |  |  |  |
184 |  |  |  |  | # See if is of the compound form 'property=value', where the
185 |  |  |  |  | # value indicates the table we should use.
186 | 2 | 9µs |  |  | my ($property, $table, @remainder) =
187 |  |  |  |  | split /\s*[:=]\s*/, $property_and_table, -1;
188 | 2 | 1µs |  |  | if (@remainder) {
189 |  |  |  |  | pop @recursed if @recursed;
190 |  |  |  |  | return $type;
191 |  |  |  |  | }
192 |  |  |  |  |
193 | 2 | 600ns |  |  | my $prefix;
194 | 2 | 2µs |  |  | if (! defined $table) {
195 |  |  |  |  |
196 |  |  |  |  | # Here, is the single form. The property becomes empty, and
197 |  |  |  |  | # the whole value is the table.
198 | 2 | 900ns |  |  | $table = $property;
199 | 2 | 2µs |  |  | $prefix = $property = "";
200 |  |  |  |  | } else {
201 |  |  |  |  | print STDERR __LINE__, ": $property\n" if DEBUG;
202 |  |  |  |  |
203 |  |  |  |  | # Here it is the compound property=table form. The property
204 |  |  |  |  | # name is always loosely matched, and always can have an
205 |  |  |  |  | # optional 'is' prefix (which isn't true in the single
206 |  |  |  |  | # form).
207 |  |  |  |  | $property = _loose_name($property) =~ s/^is//r;
208 |  |  |  |  |
209 |  |  |  |  | # And convert to canonical form. Quit if not valid.
210 |  |  |  |  | $property = $utf8::loose_property_name_of{$property};
211 |  |  |  |  | if (! defined $property) {
212 |  |  |  |  | pop @recursed if @recursed;
213 |  |  |  |  | return $type;
214 |  |  |  |  | }
215 |  |  |  |  |
216 |  |  |  |  | $prefix = "$property=";
217 |  |  |  |  |
218 |  |  |  |  | # If the rhs looks like it is a number...
219 |  |  |  |  | print STDERR __LINE__, ": table=$table\n" if DEBUG;
220 |  |  |  |  | if ($table =~ qr{ ^ [ \s 0-9 _ + / . -]+ $ }x) {
221 |  |  |  |  | print STDERR __LINE__, ": table=$table\n" if DEBUG;
222 |  |  |  |  |
223 |  |  |  |  | # Don't allow leading nor trailing slashes
224 |  |  |  |  | if ($table =~ / ^ \/ | \/ $ /x) {
225 |  |  |  |  | pop @recursed if @recursed;
226 |  |  |  |  | return $type;
227 |  |  |  |  | }
228 |  |  |  |  |
229 |  |  |  |  | # Split on slash, in case it is a rational, like \p{1/5}
230 |  |  |  |  | my @parts = split qr{ \s* / \s* }x, $table, -1;
231 |  |  |  |  | print __LINE__, ": $type\n" if @parts > 2 && DEBUG;
232 |  |  |  |  |
233 |  |  |  |  | # Can have maximum of one slash
234 |  |  |  |  | if (@parts > 2) {
235 |  |  |  |  | pop @recursed if @recursed;
236 |  |  |  |  | return $type;
237 |  |  |  |  | }
238 |  |  |  |  |
239 |  |  |  |  | foreach my $part (@parts) {
240 |  |  |  |  | print __LINE__, ": part=$part\n" if DEBUG;
241 |  |  |  |  |
242 |  |  |  |  | $part =~ s/^\+\s*//; # Remove leading plus
243 |  |  |  |  | $part =~ s/^-\s*/-/; # Remove blanks after unary
244 |  |  |  |  | # minus
245 |  |  |  |  |
246 |  |  |  |  | # Remove underscores between digits.
247 |  |  |  |  | $part =~ s/( ?<= [0-9] ) _ (?= [0-9] ) //xg;
248 |  |  |  |  |
249 |  |  |  |  | # No leading zeros (but don't make a single '0'
250 |  |  |  |  | # into a null string)
251 |  |  |  |  | $part =~ s/ ^ ( -? ) 0+ /$1/x;
252 |  |  |  |  | $part .= '0' if $part eq '-' || $part eq "";
253 |  |  |  |  |
254 |  |  |  |  | # No trailing zeros after a decimal point
255 |  |  |  |  | $part =~ s/ ( \. .*? ) 0+ $ /$1/x;
256 |  |  |  |  |
257 |  |  |  |  | # Begin with a 0 if a leading decimal point
258 |  |  |  |  | $part =~ s/ ^ ( -? ) \. /${1}0./x;
259 |  |  |  |  |
260 |  |  |  |  | # Ensure not a trailing decimal point: turn into an
261 |  |  |  |  | # integer
262 |  |  |  |  | $part =~ s/ \. $ //x;
263 |  |  |  |  |
264 |  |  |  |  | print STDERR __LINE__, ": part=$part\n" if DEBUG;
265 |  |  |  |  | #return $type if $part eq "";
266 |  |  |  |  |
267 |  |  |  |  | # Result better look like a number. (This test is
268 |  |  |  |  | # needed because, for example could have a plus in
269 |  |  |  |  | # the middle.)
270 |  |  |  |  | if ($part !~ / ^ -? [0-9]+ ( \. [0-9]+)? $ /x) {
271 |  |  |  |  | pop @recursed if @recursed;
272 |  |  |  |  | return $type;
273 |  |  |  |  | }
274 |  |  |  |  | }
275 |  |  |  |  |
276 |  |  |  |  | # If a rational...
277 |  |  |  |  | if (@parts == 2) {
278 |  |  |  |  |
279 |  |  |  |  | # If denominator is negative, get rid of it, and ...
280 |  |  |  |  | if ($parts[1] =~ s/^-//) {
281 |  |  |  |  |
282 |  |  |  |  | # If numerator is also negative, convert the
283 |  |  |  |  | # whole thing to positive, or move the minus to
284 |  |  |  |  | # the numerator
285 |  |  |  |  | if ($parts[0] !~ s/^-//) {
286 |  |  |  |  | $parts[0] = '-' . $parts[0];
287 |  |  |  |  | }
288 |  |  |  |  | }
289 |  |  |  |  | $table = join '/', @parts;
290 |  |  |  |  | }
291 |  |  |  |  | elsif ($property ne 'nv' || $parts[0] !~ /\./) {
292 |  |  |  |  |
293 |  |  |  |  | # Here is not numeric value, or doesn't have a
294 |  |  |  |  | # decimal point. No further manipulation is
295 |  |  |  |  | # necessary. (Note the hard-coded property name.
296 |  |  |  |  | # This could fail if other properties eventually
297 |  |  |  |  | # had fractions as well; perhaps the cjk ones
298 |  |  |  |  | # could evolve to do that. This hard-coding could
299 |  |  |  |  | # be fixed by mktables generating a list of
300 |  |  |  |  | # properties that could have fractions.)
301 |  |  |  |  | $table = $parts[0];
302 |  |  |  |  | } else {
303 |  |  |  |  |
304 |  |  |  |  | # Here is a floating point numeric_value. Try to
305 |  |  |  |  | # convert to rational. First see if is in the list
306 |  |  |  |  | # of known ones.
307 |  |  |  |  | if (exists $utf8::nv_floating_to_rational{$parts[0]}) {
308 |  |  |  |  | $table = $utf8::nv_floating_to_rational{$parts[0]};
309 |  |  |  |  | } else {
310 |  |  |  |  |
311 |  |  |  |  | # Here not in the list. See if is close
312 |  |  |  |  | # enough to something in the list. First
313 |  |  |  |  | # determine what 'close enough' means. It has
314 |  |  |  |  | # to be as tight as what mktables says is the
315 |  |  |  |  | # maximum slop, and as tight as how many
316 |  |  |  |  | # digits we were passed. That is, if the user
317 |  |  |  |  | # said .667, .6667, .66667, etc. we match as
318 |  |  |  |  | # many digits as they passed until get to
319 |  |  |  |  | # where it doesn't matter any more due to the
320 |  |  |  |  | # machine's precision. If they said .6666668,
321 |  |  |  |  | # we fail.
322 |  |  |  |  | (my $fraction = $parts[0]) =~ s/^.*\.//;
323 |  |  |  |  | my $epsilon = 10 ** - (length($fraction));
324 |  |  |  |  | if ($epsilon > $utf8::max_floating_slop) {
325 |  |  |  |  | $epsilon = $utf8::max_floating_slop;
326 |  |  |  |  | }
327 |  |  |  |  |
328 |  |  |  |  | # But it can't be tighter than the minimum
329 |  |  |  |  | # precision for this machine. If haven't
330 |  |  |  |  | # already calculated that minimum, do so now.
331 |  |  |  |  | if (! defined $min_floating_slop) {
332 |  |  |  |  |
333 |  |  |  |  | # Keep going down an order of magnitude
334 |  |  |  |  | # until find that adding this quantity to
335 |  |  |  |  | # 1 remains 1; but put an upper limit on
336 |  |  |  |  | # this so in case this algorithm doesn't
337 |  |  |  |  | # work properly on some platform, that we
338 |  |  |  |  | # won't loop forever.
339 |  |  |  |  | my $count = 0;
340 |  |  |  |  | $min_floating_slop = 1;
341 |  |  |  |  | while (1+ $min_floating_slop != 1
342 |  |  |  |  | && $count++ < 50)
343 |  |  |  |  | {
344 |  |  |  |  | my $next = $min_floating_slop / 10;
345 |  |  |  |  | last if $next == 0; # If underflows,
346 |  |  |  |  | # use previous one
347 |  |  |  |  | $min_floating_slop = $next;
348 |  |  |  |  | print STDERR __LINE__, ": min_float_slop=$min_floating_slop\n" if DEBUG;
349 |  |  |  |  | }
350 |  |  |  |  |
351 |  |  |  |  | # Back off a couple orders of magnitude,
352 |  |  |  |  | # just to be safe.
353 |  |  |  |  | $min_floating_slop *= 100;
354 |  |  |  |  | }
355 |  |  |  |  |
356 |  |  |  |  | if ($epsilon < $min_floating_slop) {
357 |  |  |  |  | $epsilon = $min_floating_slop;
358 |  |  |  |  | }
359 |  |  |  |  | print STDERR __LINE__, ": fraction=.$fraction; epsilon=$epsilon\n" if DEBUG;
360 |  |  |  |  |
361 |  |  |  |  | undef $table;
362 |  |  |  |  |
363 |  |  |  |  | # And for each possible rational in the table,
364 |  |  |  |  | # see if it is within epsilon of the input.
365 |  |  |  |  | foreach my $official
366 |  |  |  |  | (keys %utf8::nv_floating_to_rational)
367 |  |  |  |  | {
368 |  |  |  |  | print STDERR __LINE__, ": epsilon=$epsilon, official=$official, diff=", abs($parts[0] - $official), "\n" if DEBUG;
369 |  |  |  |  | if (abs($parts[0] - $official) < $epsilon) {
370 |  |  |  |  | $table =
371 |  |  |  |  | $utf8::nv_floating_to_rational{$official};
372 |  |  |  |  | last;
373 |  |  |  |  | }
374 |  |  |  |  | }
375 |  |  |  |  |
376 |  |  |  |  | # Quit if didn't find one.
377 |  |  |  |  | if (! defined $table) {
378 |  |  |  |  | pop @recursed if @recursed;
379 |  |  |  |  | return $type;
380 |  |  |  |  | }
381 |  |  |  |  | }
382 |  |  |  |  | }
383 |  |  |  |  | print STDERR __LINE__, ": $property=$table\n" if DEBUG;
384 |  |  |  |  | }
385 |  |  |  |  | }
386 |  |  |  |  |
387 |  |  |  |  | # Combine lhs (if any) and rhs to get something that matches
388 |  |  |  |  | # the syntax of the lookups.
389 | 2 | 1µs |  |  | $property_and_table = "$prefix$table";
390 |  |  |  |  | print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
391 |  |  |  |  |
392 |  |  |  |  | # First try stricter matching.
393 | 2 | 2µs |  |  | $file = $utf8::stricter_to_file_of{$property_and_table};
394 |  |  |  |  |
395 |  |  |  |  | # If didn't find it, try again with looser matching by editing
396 |  |  |  |  | # out the applicable characters on the rhs and looking up
397 |  |  |  |  | # again.
398 | 2 | 1µs |  |  | if (! defined $file) {
399 | 1 | 2µs | 1 | 11µs | $table = _loose_name($table); # spent 11µs making 1 call to utf8::_loose_name
400 | 1 | 500ns |  |  | $property_and_table = "$prefix$table";
401 |  |  |  |  | print STDERR __LINE__, ": $property_and_table\n" if DEBUG;
402 | 1 | 1µs |  |  | $file = $utf8::loose_to_file_of{$property_and_table};
403 |  |  |  |  | }
404 |  |  |  |  |
405 |  |  |  |  | # Add the constant and go fetch it in.
406 | 2 | 900ns |  |  | if (defined $file) {
407 |  |  |  |  |
408 |  |  |  |  | # A beginning ! means to invert. The 0+ makes sure is
409 |  |  |  |  | # numeric
410 | 1 | 11µs | 1 | 2µs | $invert_it = 0 + $file =~ s/^!//; # spent 2µs making 1 call to utf8::CORE:subst
411 |  |  |  |  |
412 | 1 | 600ns |  |  | if ($utf8::why_deprecated{$file}) {
413 |  |  |  |  | warnings::warnif('deprecated', "Use of '$type' in \\p{} or \\P{} is deprecated because: $utf8::why_deprecated{$file};");
414 |  |  |  |  | }
415 |  |  |  |  |
416 | 1 | 300ns |  |  | if ($caseless
417 |  |  |  |  | && exists $utf8::caseless_equivalent{$property_and_table})
418 |  |  |  |  | {
419 |  |  |  |  | $file = $utf8::caseless_equivalent{$property_and_table};
420 |  |  |  |  | }
421 | 1 | 2µs |  |  | $file= "$unicore_dir/lib/$file.pl";
422 | 1 | 3µs |  |  | last GETFILE;
423 |  |  |  |  | }
424 |  |  |  |  | print STDERR __LINE__, ": didn't find $property_and_table\n" if DEBUG;
425 |  |  |  |  |
426 |  |  |  |  | ##
427 |  |  |  |  | ## Last attempt -- see if it's a standard "To" name
428 |  |  |  |  | ## (e.g. "ToLower") ToTitle is used by ucfirst().
429 |  |  |  |  | ## The user-level way to access ToDigit() and ToFold()
430 |  |  |  |  | ## is to use Unicode::UCD.
431 |  |  |  |  | ##
432 |  |  |  |  | # Only check if caller wants non-binary
433 | 1 | 600ns |  |  | my $retried = 0;
434 | 1 | 6µs | 1 | 2µs | if ($minbits != 1 && $property_and_table =~ s/^to//) {{ # spent 2µs making 1 call to utf8::CORE:subst
435 |  |  |  |  | # Look input up in list of properties for which we have
436 |  |  |  |  | # mapping files.
437 | 2 | 2µs |  |  | if (defined ($file =
438 |  |  |  |  | $utf8::loose_property_to_file_of{$property_and_table}))
439 |  |  |  |  | {
440 | 1 | 1µs |  |  | $type = $utf8::file_to_swash_name{$file};
441 |  |  |  |  | print STDERR __LINE__, ": type set to $type\n" if DEBUG;
442 | 1 | 2µs |  |  | $file = "$unicore_dir/$file.pl";
443 | 1 | 3µs |  |  | last GETFILE;
444 |  |  |  |  | } # If that fails see if there is a corresponding binary
445 |  |  |  |  | # property file
446 |  |  |  |  | elsif (defined ($file =
447 |  |  |  |  | $utf8::loose_to_file_of{$property_and_table}))
448 |  |  |  |  | {
449 |  |  |  |  |
450 |  |  |  |  | # Here, there is no map file for the property we are
451 |  |  |  |  | # trying to get the map of, but this is a binary
452 |  |  |  |  | # property, and there is a file for it that can easily
453 |  |  |  |  | # be translated to a mapping.
454 |  |  |  |  |
455 |  |  |  |  | # In the case of properties that are forced to binary,
456 |  |  |  |  | # they are a combination. We return the actual
457 |  |  |  |  | # mapping instead of the binary. If the input is
458 |  |  |  |  | # something like 'Tocjkkiicore', it will be found in
459 |  |  |  |  | # %loose_property_to_file_of above as => 'To/kIICore'.
460 |  |  |  |  | # But the form like ToIskiicore won't be. To fix
461 |  |  |  |  | # this, it was easiest to do it here. These
462 |  |  |  |  | # properties are the complements of the default
463 |  |  |  |  | # property, so there is an entry in %loose_to_file_of
464 |  |  |  |  | # that is 'iskiicore' => '!kIICore/N', If we find such
465 |  |  |  |  | # an entry, strip off things and try again, which
466 |  |  |  |  | # should find the entry in %loose_property_to_file_of.
467 |  |  |  |  | # Actual binary properties that are of this form, such
468 |  |  |  |  | # as this entry: 'ishrkt' => '!Perl/Any' will also be
469 |  |  |  |  | # retried, but won't be in %loose_property_to_file_of,
470 |  |  |  |  | # and instead the next time through, it will find
471 |  |  |  |  | # 'hrkt' => '!Perl/Any' and proceed.
472 |  |  |  |  | redo if ! $retried
473 |  |  |  |  | && $file =~ /^!/
474 |  |  |  |  | && $property_and_table =~ s/^is//;
475 |  |  |  |  |
476 |  |  |  |  | # This is a binary property. Setting this here causes
477 |  |  |  |  | # it to be stored as such in the cache, so if someone
478 |  |  |  |  | # comes along later looking for just a binary, they
479 |  |  |  |  | # get it.
480 |  |  |  |  | $minbits = 1;
481 |  |  |  |  |
482 |  |  |  |  | # The 0+ makes sure is numeric
483 |  |  |  |  | $invert_it = 0 + $file =~ s/^!//;
484 |  |  |  |  | $file = "$unicore_dir/lib/$file.pl";
485 |  |  |  |  | last GETFILE;
486 |  |  |  |  | }
487 |  |  |  |  | } }
488 |  |  |  |  |
489 |  |  |  |  | ##
490 |  |  |  |  | ## If we reach this line, it's because we couldn't figure
491 |  |  |  |  | ## out what to do with $type. Ouch.
492 |  |  |  |  | ##
493 |  |  |  |  |
494 |  |  |  |  | pop @recursed if @recursed;
495 |  |  |  |  | return $type;
496 |  |  |  |  | } # end of GETFILE block
497 |  |  |  |  |
498 | 2 | 2µs |  |  | if (defined $file) {
499 |  |  |  |  | print STDERR __LINE__, ": found it (file='$file')\n" if DEBUG;
500 |  |  |  |  |
501 |  |  |  |  | ##
502 |  |  |  |  | ## If we reach here, it was due to a 'last GETFILE' above
503 |  |  |  |  | ## (exception: user-defined properties and mappings), so we
504 |  |  |  |  | ## have a filename, so now we load it if we haven't already.
505 |  |  |  |  | ## If we have, return the cached results. The cache key is the
506 |  |  |  |  | ## class and file to load, and whether the results need to be
507 |  |  |  |  | ## inverted.
508 |  |  |  |  | ##
509 | 2 | 4µs |  |  | my $found = $Cache{$class, $file, $invert_it};
510 | 2 | 700ns |  |  | if ($found and ref($found) eq $class) {
511 |  |  |  |  | print STDERR __LINE__, ": Returning cached swash for '$class,$file,$invert_it' for \\p{$type}\n" if DEBUG;
512 |  |  |  |  | pop @recursed if @recursed;
513 |  |  |  |  | return $found;
514 |  |  |  |  | }
515 |  |  |  |  |
516 | 2 | 600ns |  |  | local $@;
517 | 2 | 5µs |  |  | local $!;
518 | 4 | 1.46ms |  |  | $list = do $file; die $@ if $@;
519 | 2 | 6µs |  |  | $list_is_from_mktables = 1;
520 |  |  |  |  | }
521 |  |  |  |  | } # End of $type is non-null
522 |  |  |  |  |
523 |  |  |  |  | # Here, either $type was null, or we found the requested property and
524 |  |  |  |  | # read it into $list
525 |  |  |  |  |
526 | 2 | 1µs |  |  | my $extras = "";
527 |  |  |  |  |
528 | 2 | 900ns |  |  | my $bits = $minbits;
529 |  |  |  |  |
530 |  |  |  |  | # mktables lists don't have extras, like '&utf8::prop', so don't need
531 |  |  |  |  | # to separate them; also lists are already sorted, so don't need to do
532 |  |  |  |  | # that.
533 | 2 | 900ns |  |  | if ($list && ! $list_is_from_mktables) {
534 |  |  |  |  | my $taint = substr($list,0,0); # maintain taint
535 |  |  |  |  |
536 |  |  |  |  | # Separate the extras from the code point list, and make sure
537 |  |  |  |  | # user-defined properties and tr/// are well-behaved for
538 |  |  |  |  | # downstream code.
539 |  |  |  |  | if ($user_defined || $none) {
540 |  |  |  |  | my @tmp = split(/^/m, $list);
541 |  |  |  |  | my %seen;
542 | 2 | 659µs | 2 | 40µs | # spent 27µs (14+13) within utf8::BEGIN@542 which was called: # once (14µs+13µs) by utf8::AUTOLOAD at line 542 no warnings; # spent 27µs making 1 call to utf8::BEGIN@542 # spent 13µs making 1 call to warnings::unimport
543 |  |  |  |  |
544 |  |  |  |  | # The extras are anything that doesn't begin with a hex digit.
545 |  |  |  |  | $extras = join '', $taint, grep /^[^0-9a-fA-F]/, @tmp;
546 |  |  |  |  |
547 |  |  |  |  | # Remove the extras, and sort the remaining entries by the
548 |  |  |  |  | # numeric value of their beginning hex digits, removing any
549 |  |  |  |  | # duplicates.
550 |  |  |  |  | $list = join '', $taint,
551 |  |  |  |  | map { $_->[1] }
552 |  |  |  |  | sort { $a->[0] <=> $b->[0] }
553 |  |  |  |  | map { /^([0-9a-fA-F]+)/; [ CORE::hex($1), $_ ] }
554 |  |  |  |  | grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right
555 |  |  |  |  | }
556 |  |  |  |  | else {
557 |  |  |  |  | # mktables has gone to some trouble to make non-user defined
558 |  |  |  |  | # properties well-behaved, so we can skip the effort we do for
559 |  |  |  |  | # user-defined ones. Any extras are at the very beginning of
560 |  |  |  |  | # the string.
561 |  |  |  |  |
562 |  |  |  |  | # This regex splits out the first lines of $list into $1 and
563 |  |  |  |  | # strips them off from $list, until we get one that begins
564 |  |  |  |  | # with a hex number, alone on the line, or followed by a tab.
565 |  |  |  |  | # Either portion may be empty.
566 |  |  |  |  | $list =~ s/ \A ( .*? )
567 |  |  |  |  | (?: \z | (?= ^ [0-9a-fA-F]+ (?: \t | $) ) )
568 |  |  |  |  | //msx;
569 |  |  |  |  |
570 |  |  |  |  | $extras = "$taint$1";
571 |  |  |  |  | }
572 |  |  |  |  | }
573 |  |  |  |  |
574 | 2 | 600ns |  |  | if ($none) {
575 |  |  |  |  | my $hextra = sprintf "%04x", $none + 1;
576 |  |  |  |  | $list =~ s/\tXXXX$/\t$hextra/mg;
577 |  |  |  |  | }
578 |  |  |  |  |
579 | 2 | 2µs |  |  | if ($minbits != 1 && $minbits < 32) { # not binary property
580 | 1 | 400ns |  |  | my $top = 0;
581 | 1 | 12µs | 1 | 6µs | while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) { # spent 6µs making 1 call to utf8::CORE:match
582 | 630 | 461µs |  |  | my $min = CORE::hex $1;
583 | 630 | 192µs |  |  | my $max = defined $2 ? CORE::hex $2 : $min;
584 | 630 | 296µs |  |  | my $val = defined $3 ? CORE::hex $3 : 0;
585 | 630 | 229µs |  |  | $val += $max - $min if defined $3;
586 | 630 | 2.34ms | 630 | 1.01ms | $top = $val if $val > $top; # spent 1.01ms making 630 calls to utf8::CORE:match, avg 2µs/call
587 |  |  |  |  | }
588 | 1 | 600ns |  |  | my $topbits =
589 |  |  |  |  | $top > 0xffff ? 32 :
590 |  |  |  |  | $top > 0xff ? 16 : 8;
591 | 1 | 500ns |  |  | $bits = $topbits if $bits < $topbits;
592 |  |  |  |  | }
593 |  |  |  |  |
594 | 2 | 700ns |  |  | my @extras;
595 | 2 | 500ns |  |  | if ($extras) {
596 |  |  |  |  | for my $x ($extras) {
597 |  |  |  |  | my $taint = substr($x,0,0); # maintain taint
598 |  |  |  |  | pos $x = 0;
599 |  |  |  |  | while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
600 |  |  |  |  | my $char = "$1$taint";
601 |  |  |  |  | my $name = "$2$taint";
602 |  |  |  |  | print STDERR __LINE__, ": char [$char] => name [$name]\n"
603 |  |  |  |  | if DEBUG;
604 |  |  |  |  | if ($char =~ /[-+!&]/) {
605 |  |  |  |  | my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
606 |  |  |  |  | my $subobj;
607 |  |  |  |  | if ($c eq 'utf8') {
608 |  |  |  |  | $subobj = utf8->SWASHNEW($t, "", $minbits, 0);
609 |  |  |  |  | }
610 |  |  |  |  | elsif (exists &$name) {
611 |  |  |  |  | $subobj = utf8->SWASHNEW($name, "", $minbits, 0);
612 |  |  |  |  | }
613 |  |  |  |  | elsif ($c =~ /^([0-9a-fA-F]+)/) {
614 |  |  |  |  | $subobj = utf8->SWASHNEW("", $c, $minbits, 0);
615 |  |  |  |  | }
616 |  |  |  |  | print STDERR __LINE__, ": returned from getting sub object for $name\n" if DEBUG;
617 |  |  |  |  | if (! ref $subobj) {
618 |  |  |  |  | pop @recursed if @recursed && $type;
619 |  |  |  |  | return $subobj;
620 |  |  |  |  | }
621 |  |  |  |  | push @extras, $name => $subobj;
622 |  |  |  |  | $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
623 |  |  |  |  | $user_defined = $subobj->{USER_DEFINED}
624 |  |  |  |  | if $subobj->{USER_DEFINED};
625 |  |  |  |  | }
626 |  |  |  |  | }
627 |  |  |  |  | }
628 |  |  |  |  | }
629 |  |  |  |  |
630 |  |  |  |  | if (DEBUG) {
631 |  |  |  |  | print STDERR __LINE__, ": CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none, INVERT_IT => $invert_it, USER_DEFINED => $user_defined";
632 |  |  |  |  | print STDERR "\nLIST =>\n$list" if defined $list;
633 |  |  |  |  | print STDERR "\nEXTRAS =>\n$extras" if defined $extras;
634 |  |  |  |  | print STDERR "\n";
635 |  |  |  |  | }
636 |  |  |  |  |
637 | 2 | 32µs |  |  | my $SWASH = bless {
638 |  |  |  |  | TYPE => $type,
639 |  |  |  |  | BITS => $bits,
640 |  |  |  |  | EXTRAS => $extras,
641 |  |  |  |  | LIST => $list,
642 |  |  |  |  | NONE => $none,
643 |  |  |  |  | USER_DEFINED => $user_defined,
644 |  |  |  |  | @extras,
645 |  |  |  |  | } => $class;
646 |  |  |  |  |
647 | 2 | 1µs |  |  | if ($file) {
648 | 2 | 5µs |  |  | $Cache{$class, $file, $invert_it} = $SWASH;
649 | 2 | 3µs |  |  | if ($type
650 |  |  |  |  | && exists $utf8::SwashInfo{$type}
651 |  |  |  |  | && exists $utf8::SwashInfo{$type}{'specials_name'})
652 |  |  |  |  | {
653 | 1 | 1µs |  |  | my $specials_name = $utf8::SwashInfo{$type}{'specials_name'};
654 | 2 | 185µs | 2 | 42µs | # spent 27µs (13+14) within utf8::BEGIN@654 which was called: # once (13µs+14µs) by utf8::AUTOLOAD at line 654 no strict "refs"; # spent 27µs making 1 call to utf8::BEGIN@654 # spent 14µs making 1 call to strict::unimport
655 |  |  |  |  | print STDERR "\nspecials_name => $specials_name\n" if DEBUG;
656 | 1 | 2µs |  |  | $SWASH->{'SPECIALS'} = \%$specials_name;
657 |  |  |  |  | }
658 | 2 | 3µs |  |  | $SWASH->{'INVERT_IT'} = $invert_it;
659 |  |  |  |  | }
660 |  |  |  |  |
661 | 2 | 2µs |  |  | pop @recursed if @recursed && $type;
662 |  |  |  |  |
663 | 2 | 13µs |  |  | return $SWASH;
664 |  |  |  |  | }
665 |  |  |  |  | }
666 |  |  |  |  |
667 |  |  |  |  | # Now SWASHGET is recasted into a C function S_swatch_get (see utf8.c).
668 |  |  |  |  |
669 | 1 | 3µs |  |  | 1;
|  |  |  |  | # spent 1.02ms within utf8::CORE:match which was called 634 times, avg 2µs/call: # 630 times (1.01ms+0s) by utf8::SWASHNEW at line 586, avg 2µs/call # 2 times (1µs+0s) by utf8::SWASHNEW at line 139, avg 650ns/call # once (6µs+0s) by utf8::SWASHNEW at line 581 # once (600ns+0s) by utf8::_loose_name at line 23 sub utf8::CORE:match; # opcode
|  |  |  |  | # spent 13µs within utf8::CORE:subst which was called 11 times, avg 1µs/call: # 2 times (4µs+0s) by utf8::SWASHNEW at line 118, avg 2µs/call # 2 times (2µs+0s) by utf8::SWASHNEW at line 119, avg 1µs/call # 2 times (1µs+0s) by utf8::SWASHNEW at line 123, avg 550ns/call # 2 times (1µs+0s) by utf8::SWASHNEW at line 136, avg 500ns/call # once (2µs+0s) by utf8::SWASHNEW at line 434 # once (2µs+0s) by utf8::SWASHNEW at line 410 # once (700ns+0s) by utf8::_loose_name at line 21 sub utf8::CORE:subst; # opcode
|  |  |  |  | # spent 2µs within utf8::upgrade which was called: # once (2µs+0s) by Data::DPath::Context::BEGIN@17 at line 70 of Safe.pm sub utf8::upgrade; # xsub