← Index
NYTProf Performance Profile   « line view »
For fastest.pl
  Run on Fri Jan 31 20:48:16 2014
Reported on Fri Jan 31 20:49:40 2014

Filename/opt/perl-5.18.1/lib/site_perl/5.18.1/darwin-thread-multi-2level/Mouse/Util/TypeConstraints.pm
StatementsExecuted 63 statements in 3.16ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11131µs70µsMouse::Util::TypeConstraints::::_define_typeMouse::Util::TypeConstraints::_define_type
21120µs22µsMouse::Util::TypeConstraints::::find_or_parse_type_constraintMouse::Util::TypeConstraints::find_or_parse_type_constraint
11115µs46µsMouse::Util::TypeConstraints::::BEGIN@2Mouse::Util::TypeConstraints::BEGIN@2
11113µs15µsMouse::Util::TypeConstraints::::find_type_constraintMouse::Util::TypeConstraints::find_type_constraint
22212µs34µsMouse::Util::TypeConstraints::::find_or_create_isa_type_constraintMouse::Util::TypeConstraints::find_or_create_isa_type_constraint
11111µs97µsMouse::Util::TypeConstraints::::class_typeMouse::Util::TypeConstraints::class_type
1119µs13µsMouse::Util::TypeConstraints::::BEGIN@5Mouse::Util::TypeConstraints::BEGIN@5
1115µs5µsMouse::Util::TypeConstraints::::BEGIN@4Mouse::Util::TypeConstraints::BEGIN@4
1115µs75µsMouse::Util::TypeConstraints::::subtypeMouse::Util::TypeConstraints::subtype
1114µs4µsMouse::Util::TypeConstraints::::BEGIN@7Mouse::Util::TypeConstraints::BEGIN@7
1114µs4µsMouse::Util::TypeConstraints::::BEGIN@8Mouse::Util::TypeConstraints::BEGIN@8
1114µs4µsMouse::Util::TypeConstraints::::CORE:matchMouse::Util::TypeConstraints::CORE:match (opcode)
1112µs2µsMouse::Util::TypeConstraints::::CORE:substMouse::Util::TypeConstraints::CORE:subst (opcode)
0000s0sMouse::Util::TypeConstraints::::__ANON__[:217]Mouse::Util::TypeConstraints::__ANON__[:217]
0000s0sMouse::Util::TypeConstraints::::__ANON__[:246]Mouse::Util::TypeConstraints::__ANON__[:246]
0000s0sMouse::Util::TypeConstraints::::__ANON__[:266]Mouse::Util::TypeConstraints::__ANON__[:266]
0000s0sMouse::Util::TypeConstraints::::_find_or_create_parameterized_typeMouse::Util::TypeConstraints::_find_or_create_parameterized_type
0000s0sMouse::Util::TypeConstraints::::_find_or_create_regular_typeMouse::Util::TypeConstraints::_find_or_create_regular_type
0000s0sMouse::Util::TypeConstraints::::_find_or_create_union_typeMouse::Util::TypeConstraints::_find_or_create_union_type
0000s0sMouse::Util::TypeConstraints::::_parse_nameMouse::Util::TypeConstraints::_parse_name
0000s0sMouse::Util::TypeConstraints::::_parse_paramMouse::Util::TypeConstraints::_parse_param
0000s0sMouse::Util::TypeConstraints::::_parse_single_typeMouse::Util::TypeConstraints::_parse_single_type
0000s0sMouse::Util::TypeConstraints::::_parse_typeMouse::Util::TypeConstraints::_parse_type
0000s0sMouse::Util::TypeConstraints::::asMouse::Util::TypeConstraints::as
0000s0sMouse::Util::TypeConstraints::::coerceMouse::Util::TypeConstraints::coerce
0000s0sMouse::Util::TypeConstraints::::duck_typeMouse::Util::TypeConstraints::duck_type
0000s0sMouse::Util::TypeConstraints::::enumMouse::Util::TypeConstraints::enum
0000s0sMouse::Util::TypeConstraints::::find_or_create_does_type_constraintMouse::Util::TypeConstraints::find_or_create_does_type_constraint
0000s0sMouse::Util::TypeConstraints::::fromMouse::Util::TypeConstraints::from
0000s0sMouse::Util::TypeConstraints::::list_all_builtin_type_constraintsMouse::Util::TypeConstraints::list_all_builtin_type_constraints
0000s0sMouse::Util::TypeConstraints::::list_all_type_constraintsMouse::Util::TypeConstraints::list_all_type_constraints
0000s0sMouse::Util::TypeConstraints::::maybe_typeMouse::Util::TypeConstraints::maybe_type
0000s0sMouse::Util::TypeConstraints::::messageMouse::Util::TypeConstraints::message
0000s0sMouse::Util::TypeConstraints::::optimize_asMouse::Util::TypeConstraints::optimize_as
0000s0sMouse::Util::TypeConstraints::::optimized_constraintsMouse::Util::TypeConstraints::optimized_constraints
0000s0sMouse::Util::TypeConstraints::::register_type_constraintMouse::Util::TypeConstraints::register_type_constraint
0000s0sMouse::Util::TypeConstraints::::role_typeMouse::Util::TypeConstraints::role_type
0000s0sMouse::Util::TypeConstraints::::typeMouse::Util::TypeConstraints::type
0000s0sMouse::Util::TypeConstraints::::viaMouse::Util::TypeConstraints::via
0000s0sMouse::Util::TypeConstraints::::whereMouse::Util::TypeConstraints::where
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Mouse::Util::TypeConstraints;
2245µs277µs
# spent 46µs (15+31) within Mouse::Util::TypeConstraints::BEGIN@2 which was called: # once (15µs+31µs) by Mouse::BEGIN@18 at line 2
use Mouse::Util; # enables strict and warnings
# spent 46µs making 1 call to Mouse::Util::TypeConstraints::BEGIN@2 # spent 31µs making 1 call to Mouse::Exporter::do_import
3
4231µs15µs
# spent 5µs within Mouse::Util::TypeConstraints::BEGIN@4 which was called: # once (5µs+0s) by Mouse::BEGIN@18 at line 4
use Mouse::Meta::TypeConstraint;
# spent 5µs making 1 call to Mouse::Util::TypeConstraints::BEGIN@4
5229µs218µs
# spent 13µs (9+5) within Mouse::Util::TypeConstraints::BEGIN@5 which was called: # once (9µs+5µs) by Mouse::BEGIN@18 at line 5
use Mouse::Exporter;
# spent 13µs making 1 call to Mouse::Util::TypeConstraints::BEGIN@5 # spent 4µs making 1 call to Mouse::Exporter::import
6
7226µs14µs
# spent 4µs within Mouse::Util::TypeConstraints::BEGIN@7 which was called: # once (4µs+0s) by Mouse::BEGIN@18 at line 7
use Carp ();
# spent 4µs making 1 call to Mouse::Util::TypeConstraints::BEGIN@7
822.80ms14µs
# spent 4µs within Mouse::Util::TypeConstraints::BEGIN@8 which was called: # once (4µs+0s) by Mouse::BEGIN@18 at line 8
use Scalar::Util ();
# spent 4µs making 1 call to Mouse::Util::TypeConstraints::BEGIN@8
9
1017µs1142µsMouse::Exporter->setup_import_methods(
# spent 142µs making 1 call to Mouse::Exporter::setup_import_methods
11 as_is => [qw(
12 as where message optimize_as
13 from via
14
15 type subtype class_type role_type maybe_type duck_type
16 enum
17 coerce
18
19 find_type_constraint
20 register_type_constraint
21 )],
22);
23
2411µsour @CARP_NOT = qw(Mouse::Meta::Attribute);
25
261100nsmy %TYPE;
27
28# The root type
2912µs119µs$TYPE{Any} = Mouse::Meta::TypeConstraint->new(
# spent 19µs making 1 call to Mouse::Meta::TypeConstraint::new
30 name => 'Any',
31);
32
33111µsmy @builtins = (
34 # $name => $parent, $code,
35
36 # the base type
37 Item => 'Any', undef,
38
39 # the maybe[] type
40 Maybe => 'Item', undef,
41
42 # value types
43 Undef => 'Item', \&Undef,
44 Defined => 'Item', \&Defined,
45 Bool => 'Item', \&Bool,
46 Value => 'Defined', \&Value,
47 Str => 'Value', \&Str,
48 Num => 'Str', \&Num,
49 Int => 'Num', \&Int,
50
51 # ref types
52 Ref => 'Defined', \&Ref,
53 ScalarRef => 'Ref', \&ScalarRef,
54 ArrayRef => 'Ref', \&ArrayRef,
55 HashRef => 'Ref', \&HashRef,
56 CodeRef => 'Ref', \&CodeRef,
57 RegexpRef => 'Ref', \&RegexpRef,
58 GlobRef => 'Ref', \&GlobRef,
59
60 # object types
61 FileHandle => 'GlobRef', \&FileHandle,
62 Object => 'Ref', \&Object,
63
64 # special string types
65 ClassName => 'Str', \&ClassName,
66 RoleName => 'ClassName', \&RoleName,
67);
68
69163µs20318µswhile (my ($name, $parent, $code) = splice @builtins, 0, 3) {
# spent 318µs making 20 calls to Mouse::Meta::TypeConstraint::new, avg 16µs/call
70 $TYPE{$name} = Mouse::Meta::TypeConstraint->new(
71 name => $name,
72 parent => $TYPE{$parent},
73 optimized => $code,
74 );
75}
76
77# parametarizable types
781800ns$TYPE{Maybe} {constraint_generator} = \&_parameterize_Maybe_for;
791500ns$TYPE{ArrayRef}{constraint_generator} = \&_parameterize_ArrayRef_for;
801400ns$TYPE{HashRef} {constraint_generator} = \&_parameterize_HashRef_for;
81
82# sugars
83sub as ($) { (as => $_[0]) } ## no critic
84sub where (&) { (where => $_[0]) } ## no critic
85sub message (&) { (message => $_[0]) } ## no critic
86sub optimize_as (&) { (optimize_as => $_[0]) } ## no critic
87
88sub from { @_ }
89sub via (&) { $_[0] } ## no critic
90
91# type utilities
92
93sub optimized_constraints { # DEPRECATED
94 Carp::cluck('optimized_constraints() has been deprecated');
95 return \%TYPE;
96}
97
981500nsundef @builtins; # free the allocated memory
9915µs@builtins = keys %TYPE; # reuse it
100sub list_all_builtin_type_constraints { @builtins }
101sub list_all_type_constraints { keys %TYPE }
102
103
# spent 70µs (31+39) within Mouse::Util::TypeConstraints::_define_type which was called: # once (31µs+39µs) by Mouse::Util::TypeConstraints::subtype at line 183
sub _define_type {
1041300ns my $is_subtype = shift;
1051100ns my $name;
1061200ns my %args;
107
10815µs if(@_ == 1 && ref $_[0] ){ # @_ : { name => $name, where => ... }
109 %args = %{$_[0]};
110 }
111 elsif(@_ == 2 && ref $_[1]) { # @_ : $name => { where => ... }
112 $name = $_[0];
113 %args = %{$_[1]};
114 }
115 elsif(@_ % 2) { # @_ : $name => ( where => ... )
116 ($name, %args) = @_;
117 }
118 else{ # @_ : (name => $name, where => ...)
119 %args = @_;
120 }
121
1221100ns if(!defined $name){
123 $name = $args{name};
124 }
125
1261400ns $args{name} = $name;
127
1281700ns my $parent = delete $args{as};
1291200ns if($is_subtype && !$parent){
130 $parent = delete $args{name};
131 $name = undef;
132 }
133
13412µs117µs if(defined $parent) {
135 $args{parent} = find_or_create_isa_type_constraint($parent);
136 }
137
1381300ns if(defined $name){
139 # set 'package_defined_in' only if it is not a core package
1401300ns my $this = $args{package_defined_in};
1411200ns if(!$this){
1421600ns $this = caller(1);
14319µs14µs if($this !~ /\A Mouse \b/xms){
# spent 4µs making 1 call to Mouse::Util::TypeConstraints::CORE:match
144 $args{package_defined_in} = $this;
145 }
146 }
147
1481500ns if(defined $TYPE{$name}){
149 my $that = $TYPE{$name}->{package_defined_in} || __PACKAGE__;
150 if($this ne $that) {
151 my $note = '';
152 if($that eq __PACKAGE__) {
153 $note = sprintf " ('%s' is %s type constraint)",
154 $name,
155 scalar(grep { $name eq $_ } list_all_builtin_type_constraints())
156 ? 'a builtin'
157 : 'an implicitly created';
158 }
159 Carp::croak("The type constraint '$name' has already been created in $that"
160 . " and cannot be created again in $this" . $note);
161 }
162 }
163 }
164
1651300ns $args{constraint} = delete $args{where} if exists $args{where};
16611µs $args{optimized} = delete $args{optimized_as} if exists $args{optimized_as};
167
16814µs118µs my $constraint = Mouse::Meta::TypeConstraint->new(%args);
# spent 18µs making 1 call to Mouse::Meta::TypeConstraint::new
169
17015µs if(defined $name){
171 return $TYPE{$name} = $constraint;
172 }
173 else{
174 return $constraint;
175 }
176}
177
178sub type {
179 return _define_type 0, @_;
180}
181
182
# spent 75µs (5+70) within Mouse::Util::TypeConstraints::subtype which was called: # once (5µs+70µs) by Mouse::Util::TypeConstraints::class_type at line 200
sub subtype {
18314µs170µs return _define_type 1, @_;
# spent 70µs making 1 call to Mouse::Util::TypeConstraints::_define_type
184}
185
186sub coerce { # coerce $type, from $from, via { ... }, ...
187 my $type_name = shift;
188 my $type = find_type_constraint($type_name)
189 or Carp::croak("Cannot find type '$type_name', perhaps you forgot to load it");
190
191 $type->_add_type_coercions(@_);
192 return;
193}
194
195
# spent 97µs (11+86) within Mouse::Util::TypeConstraints::class_type which was called: # once (11µs+86µs) by Mouse::init_meta at line 143 of Mouse.pm
sub class_type {
1961300ns my($name, $options) = @_;
1971600ns my $class = $options->{class} || $name;
198
199 # ClassType
200121µs286µs return subtype $name => (
# spent 75µs making 1 call to Mouse::Util::TypeConstraints::subtype # spent 11µs making 1 call to Mouse::Util::generate_isa_predicate_for
201 as => 'Object',
202 optimized_as => Mouse::Util::generate_isa_predicate_for($class),
203 class => $class,
204 );
205}
206
207sub role_type {
208 my($name, $options) = @_;
209 my $role = $options->{role} || $name;
210
211 # RoleType
212 return subtype $name => (
213 as => 'Object',
214 optimized_as => sub {
215 return Scalar::Util::blessed($_[0])
216 && Mouse::Util::does_role($_[0], $role);
217 },
218 role => $role,
219 );
220}
221
222sub maybe_type {
223 my $param = shift;
224 return _find_or_create_parameterized_type($TYPE{Maybe}, $param);
225}
226
227sub duck_type {
228 my($name, @methods);
229
230 if(ref($_[0]) ne 'ARRAY'){
231 $name = shift;
232 }
233
234 @methods = (@_ == 1 && ref($_[0]) eq 'ARRAY') ? @{$_[0]} : @_;
235
236 # DuckType
237 return _define_type 1, $name => (
238 as => 'Object',
239 optimized_as => Mouse::Util::generate_can_predicate_for(\@methods),
240 message => sub {
241 my($object) = @_;
242 my @missing = grep { !$object->can($_) } @methods;
243 return ref($object)
244 . ' is missing methods '
245 . Mouse::Util::quoted_english_list(@missing);
246 },
247 methods => \@methods,
248 );
249}
250
251sub enum {
252 my($name, %valid);
253
254 if(!(@_ == 1 && ref($_[0]) eq 'ARRAY')){
255 $name = shift;
256 }
257
258 %valid = map{ $_ => undef }
259 (@_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_);
260
261 # EnumType
262 return _define_type 1, $name => (
263 as => 'Str',
264 optimized_as => sub{
265 return defined($_[0]) && !ref($_[0]) && exists $valid{$_[0]};
266 },
267 );
268}
269
270sub _find_or_create_regular_type{
271 my($spec, $create) = @_;
272
273 return $TYPE{$spec} if exists $TYPE{$spec};
274
275 my $meta = Mouse::Util::get_metaclass_by_name($spec);
276
277 if(!defined $meta){
278 return $create ? class_type($spec) : undef;
279 }
280
281 if(Mouse::Util::is_a_metarole($meta)){
282 return role_type($spec);
283 }
284 else{
285 return class_type($spec);
286 }
287}
288
289sub _find_or_create_parameterized_type{
290 my($base, $param) = @_;
291
292 my $name = sprintf '%s[%s]', $base->name, $param->name;
293
294 $TYPE{$name} ||= $base->parameterize($param, $name);
295}
296
297sub _find_or_create_union_type{
298 return if grep{ not defined } @_; # all things must be defined
299 my @types = sort
300 map{ $_->{type_constraints} ? @{$_->{type_constraints}} : $_ } @_;
301
302 my $name = join '|', @types;
303
304 # UnionType
305 $TYPE{$name} ||= Mouse::Meta::TypeConstraint->new(
306 name => $name,
307 type_constraints => \@types,
308 );
309}
310
311# The type parser
312
313# param : '[' type ']' | NOTHING
314sub _parse_param {
315 my($c) = @_;
316
317 if($c->{spec} =~ s/^\[//){
318 my $type = _parse_type($c, 1);
319
320 if($c->{spec} =~ s/^\]//){
321 return $type;
322 }
323 Carp::croak("Syntax error in type: missing right square bracket in '$c->{orig}'");
324 }
325
326 return undef;
327}
328
329# name : [\w.:]+
330sub _parse_name {
331 my($c, $create) = @_;
332
333 if($c->{spec} =~ s/\A ([\w.:]+) //xms){
334 return _find_or_create_regular_type($1, $create);
335 }
336 Carp::croak("Syntax error in type: expect type name near '$c->{spec}' in '$c->{orig}'");
337}
338
339# single_type : name param
340sub _parse_single_type {
341 my($c, $create) = @_;
342
343 my $type = _parse_name($c, $create);
344 my $param = _parse_param($c);
345
346 if(defined $type){
347 if(defined $param){
348 return _find_or_create_parameterized_type($type, $param);
349 }
350 else {
351 return $type;
352 }
353 }
354 elsif(defined $param){
355 Carp::croak("Undefined type with parameter [$param] in '$c->{orig}'");
356 }
357 else{
358 return undef;
359 }
360}
361
362# type : single_type ('|' single_type)*
363sub _parse_type {
364 my($c, $create) = @_;
365
366 my $type = _parse_single_type($c, $create);
367 if($c->{spec}){ # can be an union type
368 my @types;
369 while($c->{spec} =~ s/^\|//){
370 push @types, _parse_single_type($c, $create);
371 }
372 if(@types){
373 return _find_or_create_union_type($type, @types);
374 }
375 }
376 return $type;
377}
378
379
380
# spent 15µs (13+3) within Mouse::Util::TypeConstraints::find_type_constraint which was called: # once (13µs+3µs) by Mouse::init_meta at line 143 of Mouse.pm
sub find_type_constraint {
3811400ns my($spec) = @_;
38216µs1800ns return $spec if Mouse::Util::is_a_type_constraint($spec) or not defined $spec;
# spent 800ns making 1 call to Mouse::Util::is_a_type_constraint
383
38416µs12µs $spec =~ s/\s+//g;
# spent 2µs making 1 call to Mouse::Util::TypeConstraints::CORE:subst
38514µs return $TYPE{$spec};
386}
387
388sub register_type_constraint {
389 my($constraint) = @_;
390 Carp::croak("No type supplied / type is not a valid type constraint")
391 unless Mouse::Util::is_a_type_constraint($constraint);
392 return $TYPE{$constraint->name} = $constraint;
393}
394
395
# spent 22µs (20+2) within Mouse::Util::TypeConstraints::find_or_parse_type_constraint which was called 2 times, avg 11µs/call: # 2 times (20µs+2µs) by Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint at line 427, avg 11µs/call
sub find_or_parse_type_constraint {
3962900ns my($spec) = @_;
39729µs22µs return $spec if Mouse::Util::is_a_type_constraint($spec) or not defined $spec;
# spent 2µs making 2 calls to Mouse::Util::is_a_type_constraint, avg 800ns/call
398
39926µs $spec =~ tr/ \t\r\n//d;
400
40121µs my $tc = $TYPE{$spec};
40228µs if(defined $tc) {
403 return $tc;
404 }
405
406 my %context = (
407 spec => $spec,
408 orig => $spec,
409 );
410 $tc = _parse_type(\%context);
411
412 if($context{spec}){
413 Carp::croak("Syntax error: extra elements '$context{spec}' in '$context{orig}'");
414 }
415
416 return $TYPE{$spec} = $tc;
417}
418
419sub find_or_create_does_type_constraint{
420 # XXX: Moose does not register a new role_type, but Mouse does.
421 my $tc = find_or_parse_type_constraint(@_);
422 return defined($tc) ? $tc : role_type(@_);
423}
424
425
# spent 34µs (12+22) within Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint which was called 2 times, avg 17µs/call: # once (6µs+11µs) by Mouse::Util::TypeConstraints::_define_type at line 134 # once (6µs+10µs) by Mouse::Meta::Attribute::_process_options at line 56 of Mouse/Meta/Attribute.pm
sub find_or_create_isa_type_constraint {
426 # XXX: Moose does not register a new class_type, but Mouse does.
42723µs222µs my $tc = find_or_parse_type_constraint(@_);
# spent 22µs making 2 calls to Mouse::Util::TypeConstraints::find_or_parse_type_constraint, avg 11µs/call
42828µs return defined($tc) ? $tc : class_type(@_);
429}
430
431128µs1;
432__END__
 
# spent 4µs within Mouse::Util::TypeConstraints::CORE:match which was called: # once (4µs+0s) by Mouse::Util::TypeConstraints::_define_type at line 143
sub Mouse::Util::TypeConstraints::CORE:match; # opcode
# spent 2µs within Mouse::Util::TypeConstraints::CORE:subst which was called: # once (2µs+0s) by Mouse::Util::TypeConstraints::find_type_constraint at line 384
sub Mouse::Util::TypeConstraints::CORE:subst; # opcode