Filename | /usr/local/lib/perl/5.14.2/Moose/Util/TypeConstraints.pm |
Statements | Executed 1934 statements in 7.79ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.10ms | 2.98ms | BEGIN@33 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 857µs | 1.75ms | BEGIN@36 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 839µs | 1.74ms | BEGIN@37 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 806µs | 1.88ms | BEGIN@39 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 765µs | 1.99ms | BEGIN@38 | Moose::Util::TypeConstraints::
104 | 9 | 4 | 665µs | 1.65ms | find_type_constraint | Moose::Util::TypeConstraints::
1 | 1 | 1 | 653µs | 4.47ms | BEGIN@34 | Moose::Util::TypeConstraints::
75 | 3 | 2 | 586µs | 2.77ms | find_or_parse_type_constraint (recurses: max depth 1, inclusive time 57µs) | Moose::Util::TypeConstraints::
1 | 1 | 1 | 508µs | 2.08ms | BEGIN@42 | Moose::Util::TypeConstraints::
17 | 12 | 1 | 454µs | 454µs | CORE:regcomp (opcode) | Moose::Util::TypeConstraints::
20 | 2 | 1 | 444µs | 6.95ms | _create_type_constraint | Moose::Util::TypeConstraints::
71 | 2 | 2 | 360µs | 4.23ms | find_or_create_isa_type_constraint | Moose::Util::TypeConstraints::
75 | 1 | 1 | 222µs | 287µs | normalize_type_constraint_name | Moose::Util::TypeConstraints::
19 | 4 | 3 | 195µs | 6.80ms | subtype | Moose::Util::TypeConstraints::
11 | 2 | 1 | 182µs | 7.22ms | create_class_type_constraint | Moose::Util::TypeConstraints::
28 | 5 | 1 | 69µs | 69µs | CORE:match (opcode) | Moose::Util::TypeConstraints::
75 | 1 | 1 | 65µs | 65µs | CORE:subst (opcode) | Moose::Util::TypeConstraints::
1 | 1 | 1 | 37µs | 446µs | _create_type_constraint_union | Moose::Util::TypeConstraints::
18 | 3 | 2 | 34µs | 34µs | as | Moose::Util::TypeConstraints::
17 | 2 | 2 | 31µs | 31µs | where | Moose::Util::TypeConstraints::
4 | 2 | 1 | 29µs | 140µs | _detect_type_constraint_union | Moose::Util::TypeConstraints::
2 | 1 | 1 | 28µs | 618µs | _install_type_coercions | Moose::Util::TypeConstraints::
1 | 1 | 1 | 26µs | 87µs | _parse_type_constraint_union | Moose::Util::TypeConstraints::
9 | 1 | 1 | 26µs | 6.14ms | class_type | Moose::Util::TypeConstraints::
17 | 1 | 1 | 20µs | 20µs | inline_as | Moose::Util::TypeConstraints::
10 | 10 | 1 | 15µs | 15µs | CORE:qr (opcode) | Moose::Util::TypeConstraints::
2 | 1 | 1 | 14µs | 58µs | _detect_parameterized_type_constraint | Moose::Util::TypeConstraints::
1 | 1 | 1 | 13µs | 34µs | BEGIN@614 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 12µs | 352µs | type | Moose::Util::TypeConstraints::
1 | 1 | 1 | 11µs | 11µs | BEGIN@40 | Moose::Util::TypeConstraints::
2 | 2 | 1 | 10µs | 628µs | coerce | Moose::Util::TypeConstraints::
1 | 1 | 1 | 9µs | 9µs | BEGIN@3 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 9µs | 21µs | BEGIN@667 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 8µs | 17µs | BEGIN@694 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 8µs | 18µs | BEGIN@68 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 8µs | 17µs | BEGIN@673 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 7µs | 12µs | BEGIN@31 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 7µs | 16µs | BEGIN@678 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 7µs | 37µs | BEGIN@12 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 6µs | 36µs | BEGIN@11 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 6µs | 18µs | BEGIN@13 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 6µs | 7µs | list_all_type_constraints | Moose::Util::TypeConstraints::
1 | 1 | 1 | 6µs | 452µs | create_type_constraint_union | Moose::Util::TypeConstraints::
1 | 1 | 1 | 5µs | 5µs | BEGIN@35 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 4µs | 4µs | BEGIN@41 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 4µs | 4µs | BEGIN@10 | Moose::Util::TypeConstraints::
1 | 1 | 1 | 4µs | 4µs | BEGIN@32 | Moose::Util::TypeConstraints::
2 | 2 | 1 | 4µs | 4µs | from | Moose::Util::TypeConstraints::
2 | 2 | 1 | 3µs | 3µs | via | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | __ANON__[:73] | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | _confess | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | _create_parameterized_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | _parse_parameterized_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | _throw_error | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | add_parameterizable_type | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | create_duck_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | create_enum_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | create_named_type_constraint_union | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | create_parameterized_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | create_role_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | duck_type | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | enum | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | export_type_constraints_as_functions | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | find_or_create_does_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | find_or_create_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | get_all_parameterizable_types | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | get_type_constraint_registry | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | list_all_builtin_type_constraints | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | match_on_type | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | maybe_type | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | message | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | optimize_as | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | register_type_constraint | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | role_type | Moose::Util::TypeConstraints::
0 | 0 | 0 | 0s | 0s | union | Moose::Util::TypeConstraints::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | |||||
2 | package Moose::Util::TypeConstraints; | ||||
3 | # spent 9µs within Moose::Util::TypeConstraints::BEGIN@3 which was called:
# once (9µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 5 | ||||
4 | 1 | 5µs | $Moose::Util::TypeConstraints::AUTHORITY = 'cpan:STEVAN'; | ||
5 | 1 | 28µs | 1 | 9µs | } # spent 9µs making 1 call to Moose::Util::TypeConstraints::BEGIN@3 |
6 | { | ||||
7 | 2 | 1µs | $Moose::Util::TypeConstraints::VERSION = '2.1005'; | ||
8 | } | ||||
9 | |||||
10 | 2 | 27µs | 1 | 4µs | # spent 4µs within Moose::Util::TypeConstraints::BEGIN@10 which was called:
# once (4µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 10 # spent 4µs making 1 call to Moose::Util::TypeConstraints::BEGIN@10 |
11 | 2 | 25µs | 2 | 66µs | # spent 36µs (6+30) within Moose::Util::TypeConstraints::BEGIN@11 which was called:
# once (6µs+30µs) by Moose::Meta::Attribute::BEGIN@24 at line 11 # spent 36µs making 1 call to Moose::Util::TypeConstraints::BEGIN@11
# spent 30µs making 1 call to Exporter::import |
12 | 2 | 23µs | 2 | 68µs | # spent 37µs (7+31) within Moose::Util::TypeConstraints::BEGIN@12 which was called:
# once (7µs+31µs) by Moose::Meta::Attribute::BEGIN@24 at line 12 # spent 37µs making 1 call to Moose::Util::TypeConstraints::BEGIN@12
# spent 31µs making 1 call to Exporter::import |
13 | 2 | 48µs | 2 | 31µs | # spent 18µs (6+12) within Moose::Util::TypeConstraints::BEGIN@13 which was called:
# once (6µs+12µs) by Moose::Meta::Attribute::BEGIN@24 at line 13 # spent 18µs making 1 call to Moose::Util::TypeConstraints::BEGIN@13
# spent 12µs making 1 call to Moose::Exporter::import |
14 | |||||
15 | ## -------------------------------------------------------- | ||||
16 | # Prototyped subs must be predeclared because we have a | ||||
17 | # circular dependency with Moose::Meta::Attribute et. al. | ||||
18 | # so in case of us being use'd first the predeclaration | ||||
19 | # ensures the prototypes are in scope when consumers are | ||||
20 | # compiled. | ||||
21 | |||||
22 | # dah sugah! | ||||
23 | sub where (&); | ||||
24 | sub via (&); | ||||
25 | sub message (&); | ||||
26 | sub optimize_as (&); | ||||
27 | sub inline_as (&); | ||||
28 | |||||
29 | ## -------------------------------------------------------- | ||||
30 | |||||
31 | 2 | 20µs | 2 | 17µs | # spent 12µs (7+5) within Moose::Util::TypeConstraints::BEGIN@31 which was called:
# once (7µs+5µs) by Moose::Meta::Attribute::BEGIN@24 at line 31 # spent 12µs making 1 call to Moose::Util::TypeConstraints::BEGIN@31
# spent 5µs making 1 call to Package::DeprecationManager::__ANON__[Package/DeprecationManager.pm:61] |
32 | 2 | 23µs | 1 | 4µs | # spent 4µs within Moose::Util::TypeConstraints::BEGIN@32 which was called:
# once (4µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 32 # spent 4µs making 1 call to Moose::Util::TypeConstraints::BEGIN@32 |
33 | 2 | 117µs | 1 | 2.98ms | # spent 2.98ms (1.10+1.88) within Moose::Util::TypeConstraints::BEGIN@33 which was called:
# once (1.10ms+1.88ms) by Moose::Meta::Attribute::BEGIN@24 at line 33 # spent 2.98ms making 1 call to Moose::Util::TypeConstraints::BEGIN@33 |
34 | 2 | 116µs | 1 | 4.47ms | # spent 4.47ms (653µs+3.81) within Moose::Util::TypeConstraints::BEGIN@34 which was called:
# once (653µs+3.81ms) by Moose::Meta::Attribute::BEGIN@24 at line 34 # spent 4.47ms making 1 call to Moose::Util::TypeConstraints::BEGIN@34 |
35 | 2 | 28µs | 1 | 5µs | # spent 5µs within Moose::Util::TypeConstraints::BEGIN@35 which was called:
# once (5µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 35 # spent 5µs making 1 call to Moose::Util::TypeConstraints::BEGIN@35 |
36 | 2 | 135µs | 1 | 1.75ms | # spent 1.75ms (857µs+898µs) within Moose::Util::TypeConstraints::BEGIN@36 which was called:
# once (857µs+898µs) by Moose::Meta::Attribute::BEGIN@24 at line 36 # spent 1.75ms making 1 call to Moose::Util::TypeConstraints::BEGIN@36 |
37 | 2 | 114µs | 1 | 1.74ms | # spent 1.74ms (839µs+901µs) within Moose::Util::TypeConstraints::BEGIN@37 which was called:
# once (839µs+901µs) by Moose::Meta::Attribute::BEGIN@24 at line 37 # spent 1.74ms making 1 call to Moose::Util::TypeConstraints::BEGIN@37 |
38 | 2 | 110µs | 1 | 1.99ms | # spent 1.99ms (765µs+1.23) within Moose::Util::TypeConstraints::BEGIN@38 which was called:
# once (765µs+1.23ms) by Moose::Meta::Attribute::BEGIN@24 at line 38 # spent 1.99ms making 1 call to Moose::Util::TypeConstraints::BEGIN@38 |
39 | 2 | 135µs | 1 | 1.88ms | # spent 1.88ms (806µs+1.08) within Moose::Util::TypeConstraints::BEGIN@39 which was called:
# once (806µs+1.08ms) by Moose::Meta::Attribute::BEGIN@24 at line 39 # spent 1.88ms making 1 call to Moose::Util::TypeConstraints::BEGIN@39 |
40 | 2 | 29µs | 1 | 11µs | # spent 11µs within Moose::Util::TypeConstraints::BEGIN@40 which was called:
# once (11µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 40 # spent 11µs making 1 call to Moose::Util::TypeConstraints::BEGIN@40 |
41 | 2 | 22µs | 1 | 4µs | # spent 4µs within Moose::Util::TypeConstraints::BEGIN@41 which was called:
# once (4µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 41 # spent 4µs making 1 call to Moose::Util::TypeConstraints::BEGIN@41 |
42 | 2 | 201µs | 1 | 2.08ms | # spent 2.08ms (508µs+1.57) within Moose::Util::TypeConstraints::BEGIN@42 which was called:
# once (508µs+1.57ms) by Moose::Meta::Attribute::BEGIN@24 at line 42 # spent 2.08ms making 1 call to Moose::Util::TypeConstraints::BEGIN@42 |
43 | |||||
44 | 1 | 6µs | 1 | 987µs | Moose::Exporter->setup_import_methods( # spent 987µs making 1 call to Moose::Exporter::setup_import_methods |
45 | as_is => [ | ||||
46 | qw( | ||||
47 | type subtype class_type role_type maybe_type duck_type | ||||
48 | as where message optimize_as inline_as | ||||
49 | coerce from via | ||||
50 | enum union | ||||
51 | find_type_constraint | ||||
52 | register_type_constraint | ||||
53 | match_on_type ) | ||||
54 | ], | ||||
55 | ); | ||||
56 | |||||
57 | ## -------------------------------------------------------- | ||||
58 | ## type registry and some useful functions for it | ||||
59 | ## -------------------------------------------------------- | ||||
60 | |||||
61 | 1 | 2µs | 1 | 239µs | my $REGISTRY = Moose::Meta::TypeConstraint::Registry->new; # spent 239µs making 1 call to Moose::Meta::TypeConstraint::Registry::new |
62 | |||||
63 | sub get_type_constraint_registry {$REGISTRY} | ||||
64 | 2 | 6µs | 1 | 900ns | # spent 7µs (6+900ns) within Moose::Util::TypeConstraints::list_all_type_constraints which was called:
# once (6µs+900ns) by Moose::Meta::Attribute::BEGIN@24 at line 749 # spent 900ns making 1 call to Moose::Meta::TypeConstraint::Registry::type_constraints |
65 | |||||
66 | sub export_type_constraints_as_functions { | ||||
67 | my $pkg = caller(); | ||||
68 | 2 | 2.23ms | 2 | 28µs | # spent 18µs (8+10) within Moose::Util::TypeConstraints::BEGIN@68 which was called:
# once (8µs+10µs) by Moose::Meta::Attribute::BEGIN@24 at line 68 # spent 18µs making 1 call to Moose::Util::TypeConstraints::BEGIN@68
# spent 10µs making 1 call to strict::unimport |
69 | foreach my $constraint ( keys %{ $REGISTRY->type_constraints } ) { | ||||
70 | my $tc = $REGISTRY->get_type_constraint($constraint) | ||||
71 | ->_compiled_type_constraint; | ||||
72 | *{"${pkg}::${constraint}"} | ||||
73 | = sub { $tc->( $_[0] ) ? 1 : undef }; # the undef is for compat | ||||
74 | } | ||||
75 | } | ||||
76 | |||||
77 | # spent 452µs (6+446) within Moose::Util::TypeConstraints::create_type_constraint_union which was called:
# once (6µs+446µs) by Moose::Util::TypeConstraints::find_or_parse_type_constraint at line 278 | ||||
78 | 1 | 6µs | 1 | 446µs | _create_type_constraint_union(\@_); # spent 446µs making 1 call to Moose::Util::TypeConstraints::_create_type_constraint_union |
79 | } | ||||
80 | |||||
81 | sub create_named_type_constraint_union { | ||||
82 | my $name = shift; | ||||
83 | _create_type_constraint_union($name, \@_); | ||||
84 | } | ||||
85 | |||||
86 | # spent 446µs (37+409) within Moose::Util::TypeConstraints::_create_type_constraint_union which was called:
# once (37µs+409µs) by Moose::Util::TypeConstraints::create_type_constraint_union at line 78 | ||||
87 | 1 | 400ns | my $name; | ||
88 | 1 | 600ns | $name = shift if @_ > 1; | ||
89 | 2 | 2µs | my @tcs = @{ shift() }; | ||
90 | |||||
91 | 1 | 400ns | my @type_constraint_names; | ||
92 | |||||
93 | 1 | 2µs | 1 | 8µs | if ( scalar @tcs == 1 && _detect_type_constraint_union( $tcs[0] ) ) { # spent 8µs making 1 call to Moose::Util::TypeConstraints::_detect_type_constraint_union |
94 | 1 | 4µs | 1 | 87µs | @type_constraint_names = _parse_type_constraint_union( $tcs[0] ); # spent 87µs making 1 call to Moose::Util::TypeConstraints::_parse_type_constraint_union |
95 | } | ||||
96 | else { | ||||
97 | @type_constraint_names = @tcs; | ||||
98 | } | ||||
99 | |||||
100 | 1 | 1µs | ( scalar @type_constraint_names >= 2 ) | ||
101 | || __PACKAGE__->_throw_error( | ||||
102 | "You must pass in at least 2 type names to make a union"); | ||||
103 | |||||
104 | 2 | 8µs | 4 | 1µs | my @type_constraints = map { # spent 1µs making 2 calls to Moose::Meta::TypeConstraint::__ANON__[Moose/Meta/TypeConstraint.pm:16], avg 700ns/call
# spent 57µs making 2 calls to Moose::Util::TypeConstraints::find_or_parse_type_constraint, avg 28µs/call, recursion: max depth 1, sum of overlapping time 57µs |
105 | 1 | 3µs | find_or_parse_type_constraint($_) | ||
106 | || __PACKAGE__->_throw_error( | ||||
107 | "Could not locate type constraint ($_) for the union"); | ||||
108 | } @type_constraint_names; | ||||
109 | |||||
110 | 1 | 2µs | my %options = ( | ||
111 | type_constraints => \@type_constraints | ||||
112 | ); | ||||
113 | 1 | 400ns | $options{name} = $name if defined $name; | ||
114 | |||||
115 | 1 | 8µs | 1 | 256µs | return Moose::Meta::TypeConstraint::Union->new(%options); # spent 256µs making 1 call to Moose::Meta::TypeConstraint::Union::new |
116 | } | ||||
117 | |||||
118 | |||||
119 | sub create_parameterized_type_constraint { | ||||
120 | my $type_constraint_name = shift; | ||||
121 | my ( $base_type, $type_parameter ) | ||||
122 | = _parse_parameterized_type_constraint($type_constraint_name); | ||||
123 | |||||
124 | ( defined $base_type && defined $type_parameter ) | ||||
125 | || __PACKAGE__->_throw_error( | ||||
126 | "Could not parse type name ($type_constraint_name) correctly"); | ||||
127 | |||||
128 | if ( $REGISTRY->has_type_constraint($base_type) ) { | ||||
129 | my $base_type_tc = $REGISTRY->get_type_constraint($base_type); | ||||
130 | return _create_parameterized_type_constraint( | ||||
131 | $base_type_tc, | ||||
132 | $type_parameter | ||||
133 | ); | ||||
134 | } | ||||
135 | else { | ||||
136 | __PACKAGE__->_throw_error( | ||||
137 | "Could not locate the base type ($base_type)"); | ||||
138 | } | ||||
139 | } | ||||
140 | |||||
141 | sub _create_parameterized_type_constraint { | ||||
142 | my ( $base_type_tc, $type_parameter ) = @_; | ||||
143 | if ( $base_type_tc->can('parameterize') ) { | ||||
144 | return $base_type_tc->parameterize($type_parameter); | ||||
145 | } | ||||
146 | else { | ||||
147 | return Moose::Meta::TypeConstraint::Parameterized->new( | ||||
148 | name => $base_type_tc->name . '[' . $type_parameter . ']', | ||||
149 | parent => $base_type_tc, | ||||
150 | type_parameter => | ||||
151 | find_or_create_isa_type_constraint($type_parameter), | ||||
152 | ); | ||||
153 | } | ||||
154 | } | ||||
155 | |||||
156 | #should we also support optimized checks? | ||||
157 | # spent 7.22ms (182µs+7.03) within Moose::Util::TypeConstraints::create_class_type_constraint which was called 11 times, avg 656µs/call:
# 9 times (153µs+5.96ms) by Moose::Util::TypeConstraints::class_type at line 368, avg 679µs/call
# 2 times (29µs+1.07ms) by Moose::Util::TypeConstraints::find_or_create_isa_type_constraint at line 260, avg 551µs/call | ||||
158 | 11 | 6µs | my ( $class, $options ) = @_; | ||
159 | |||||
160 | # too early for this check | ||||
161 | #find_type_constraint("ClassName")->check($class) | ||||
162 | # || __PACKAGE__->_throw_error("Can't create a class type constraint because '$class' is not a class name"); | ||||
163 | |||||
164 | 11 | 9µs | my $pkg_defined_in = $options->{package_defined_in} || scalar( caller(1) ); | ||
165 | |||||
166 | 11 | 16µs | 11 | 50µs | if (my $type = $REGISTRY->get_type_constraint($class)) { # spent 50µs making 11 calls to Moose::Meta::TypeConstraint::Registry::get_type_constraint, avg 4µs/call |
167 | if (!($type->isa('Moose::Meta::TypeConstraint::Class') && $type->class eq $class)) { | ||||
168 | _confess( | ||||
169 | "The type constraint '$class' has already been created in " | ||||
170 | . $type->_package_defined_in | ||||
171 | . " and cannot be created again in " | ||||
172 | . $pkg_defined_in ) | ||||
173 | } | ||||
174 | else { | ||||
175 | return $type; | ||||
176 | } | ||||
177 | } | ||||
178 | |||||
179 | 11 | 4µs | my %options = ( | ||
180 | class => $class, | ||||
181 | name => $class, | ||||
182 | package_defined_in => $pkg_defined_in, | ||||
183 | 11 | 34µs | %{ $options || {} }, | ||
184 | ); | ||||
185 | |||||
186 | 11 | 3µs | $options{name} ||= "__ANON__"; | ||
187 | |||||
188 | 11 | 34µs | 11 | 6.78ms | my $tc = Moose::Meta::TypeConstraint::Class->new(%options); # spent 6.78ms making 11 calls to Moose::Meta::TypeConstraint::Class::new, avg 617µs/call |
189 | 11 | 19µs | 11 | 203µs | $REGISTRY->add_type_constraint($tc); # spent 203µs making 11 calls to Moose::Meta::TypeConstraint::Registry::add_type_constraint, avg 18µs/call |
190 | 11 | 33µs | return $tc; | ||
191 | } | ||||
192 | |||||
193 | sub create_role_type_constraint { | ||||
194 | my ( $role, $options ) = @_; | ||||
195 | |||||
196 | # too early for this check | ||||
197 | #find_type_constraint("ClassName")->check($class) | ||||
198 | # || __PACKAGE__->_throw_error("Can't create a class type constraint because '$class' is not a class name"); | ||||
199 | |||||
200 | my $pkg_defined_in = $options->{package_defined_in} || scalar( caller(1) ); | ||||
201 | |||||
202 | if (my $type = $REGISTRY->get_type_constraint($role)) { | ||||
203 | if (!($type->isa('Moose::Meta::TypeConstraint::Role') && $type->role eq $role)) { | ||||
204 | _confess( | ||||
205 | "The type constraint '$role' has already been created in " | ||||
206 | . $type->_package_defined_in | ||||
207 | . " and cannot be created again in " | ||||
208 | . $pkg_defined_in ) | ||||
209 | } | ||||
210 | else { | ||||
211 | return $type; | ||||
212 | } | ||||
213 | } | ||||
214 | |||||
215 | my %options = ( | ||||
216 | role => $role, | ||||
217 | name => $role, | ||||
218 | package_defined_in => $pkg_defined_in, | ||||
219 | %{ $options || {} }, | ||||
220 | ); | ||||
221 | |||||
222 | $options{name} ||= "__ANON__"; | ||||
223 | |||||
224 | my $tc = Moose::Meta::TypeConstraint::Role->new(%options); | ||||
225 | $REGISTRY->add_type_constraint($tc); | ||||
226 | return $tc; | ||||
227 | } | ||||
228 | |||||
229 | sub find_or_create_type_constraint { | ||||
230 | my ( $type_constraint_name, $options_for_anon_type ) = @_; | ||||
231 | |||||
232 | if ( my $constraint | ||||
233 | = find_or_parse_type_constraint($type_constraint_name) ) { | ||||
234 | return $constraint; | ||||
235 | } | ||||
236 | elsif ( defined $options_for_anon_type ) { | ||||
237 | |||||
238 | # NOTE: | ||||
239 | # if there is no $options_for_anon_type | ||||
240 | # specified, then we assume they don't | ||||
241 | # want to create one, and return nothing. | ||||
242 | |||||
243 | # otherwise assume that we should create | ||||
244 | # an ANON type with the $options_for_anon_type | ||||
245 | # options which can be passed in. It should | ||||
246 | # be noted that these don't get registered | ||||
247 | # so we need to return it. | ||||
248 | # - SL | ||||
249 | return Moose::Meta::TypeConstraint->new( | ||||
250 | name => '__ANON__', | ||||
251 | %{$options_for_anon_type} | ||||
252 | ); | ||||
253 | } | ||||
254 | |||||
255 | return; | ||||
256 | } | ||||
257 | |||||
258 | # spent 4.23ms (360µs+3.87) within Moose::Util::TypeConstraints::find_or_create_isa_type_constraint which was called 71 times, avg 60µs/call:
# 52 times (267µs+3.34ms) by Moose::Meta::Attribute::_process_isa_option at line 374 of Moose/Meta/Attribute.pm, avg 69µs/call
# 19 times (93µs+530µs) by Moose::Util::TypeConstraints::_create_type_constraint at line 565, avg 33µs/call | ||||
259 | 71 | 35µs | my ($type_constraint_name, $options) = @_; | ||
260 | 71 | 244µs | 142 | 3.87ms | find_or_parse_type_constraint($type_constraint_name) # spent 2.72ms making 71 calls to Moose::Util::TypeConstraints::find_or_parse_type_constraint, avg 38µs/call
# spent 1.10ms making 2 calls to Moose::Util::TypeConstraints::create_class_type_constraint, avg 551µs/call
# spent 49µs making 69 calls to Moose::Meta::TypeConstraint::__ANON__[Moose/Meta/TypeConstraint.pm:16], avg 706ns/call |
261 | || create_class_type_constraint($type_constraint_name, $options); | ||||
262 | } | ||||
263 | |||||
264 | sub find_or_create_does_type_constraint { | ||||
265 | my ($type_constraint_name, $options) = @_; | ||||
266 | find_or_parse_type_constraint($type_constraint_name) | ||||
267 | || create_role_type_constraint($type_constraint_name, $options); | ||||
268 | } | ||||
269 | |||||
270 | # spent 2.77ms (586µs+2.19) within Moose::Util::TypeConstraints::find_or_parse_type_constraint which was called 75 times, avg 37µs/call:
# 71 times (554µs+2.16ms) by Moose::Util::TypeConstraints::find_or_create_isa_type_constraint at line 260, avg 38µs/call
# 2 times (15µs+42µs) by Moose::Meta::TypeCoercion::compile_type_coercion at line 50 of Moose/Meta/TypeCoercion.pm, avg 28µs/call
# 2 times (17µs+-17µs) by Moose::Util::TypeConstraints::_create_type_constraint_union at line 104, avg 0s/call | ||||
271 | 75 | 83µs | 75 | 287µs | my $type_constraint_name = normalize_type_constraint_name(shift); # spent 287µs making 75 calls to Moose::Util::TypeConstraints::normalize_type_constraint_name, avg 4µs/call |
272 | 75 | 8µs | my $constraint; | ||
273 | |||||
274 | 75 | 205µs | 152 | 1.49ms | if ( $constraint = find_type_constraint($type_constraint_name) ) { # spent 1.22ms making 75 calls to Moose::Util::TypeConstraints::find_type_constraint, avg 16µs/call
# spent 132µs making 3 calls to Moose::Util::TypeConstraints::_detect_type_constraint_union, avg 44µs/call
# spent 77µs making 72 calls to Moose::Meta::TypeConstraint::__ANON__[Moose/Meta/TypeConstraint.pm:16], avg 1µs/call
# spent 58µs making 2 calls to Moose::Util::TypeConstraints::_detect_parameterized_type_constraint, avg 29µs/call |
275 | 72 | 138µs | return $constraint; | ||
276 | } | ||||
277 | elsif ( _detect_type_constraint_union($type_constraint_name) ) { | ||||
278 | 1 | 3µs | 1 | 452µs | $constraint = create_type_constraint_union($type_constraint_name); # spent 452µs making 1 call to Moose::Util::TypeConstraints::create_type_constraint_union |
279 | } | ||||
280 | elsif ( _detect_parameterized_type_constraint($type_constraint_name) ) { | ||||
281 | $constraint | ||||
282 | = create_parameterized_type_constraint($type_constraint_name); | ||||
283 | } | ||||
284 | else { | ||||
285 | 2 | 4µs | return; | ||
286 | } | ||||
287 | |||||
288 | 1 | 2µs | 1 | 20µs | $REGISTRY->add_type_constraint($constraint); # spent 20µs making 1 call to Moose::Meta::TypeConstraint::Registry::add_type_constraint |
289 | 1 | 2µs | return $constraint; | ||
290 | } | ||||
291 | |||||
292 | # spent 287µs (222+65) within Moose::Util::TypeConstraints::normalize_type_constraint_name which was called 75 times, avg 4µs/call:
# 75 times (222µs+65µs) by Moose::Util::TypeConstraints::find_or_parse_type_constraint at line 271, avg 4µs/call | ||||
293 | 75 | 20µs | my $type_constraint_name = shift; | ||
294 | 75 | 185µs | 75 | 65µs | $type_constraint_name =~ s/\s//g; # spent 65µs making 75 calls to Moose::Util::TypeConstraints::CORE:subst, avg 868ns/call |
295 | 75 | 165µs | return $type_constraint_name; | ||
296 | } | ||||
297 | |||||
298 | sub _confess { | ||||
299 | my $error = shift; | ||||
300 | |||||
301 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; | ||||
302 | Carp::confess($error); | ||||
303 | } | ||||
304 | |||||
305 | ## -------------------------------------------------------- | ||||
306 | ## exported functions ... | ||||
307 | ## -------------------------------------------------------- | ||||
308 | |||||
309 | # spent 1.65ms (665µs+982µs) within Moose::Util::TypeConstraints::find_type_constraint which was called 104 times, avg 16µs/call:
# 75 times (485µs+734µs) by Moose::Util::TypeConstraints::find_or_parse_type_constraint at line 274, avg 16µs/call
# 11 times (71µs+98µs) by Moose::Meta::TypeConstraint::Class::new at line 35 of Moose/Meta/TypeConstraint/Class.pm, avg 15µs/call
# 11 times (64µs+87µs) by Moose::init_meta at line 165 of Moose.pm, avg 14µs/call
# 2 times (12µs+18µs) by Moose::Util::TypeConstraints::_install_type_coercions at line 586, avg 15µs/call
# once (6µs+9µs) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 85 of Moose/Util/TypeConstraints/Builtins.pm
# once (7µs+9µs) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 239 of Moose/Util/TypeConstraints/Builtins.pm
# once (6µs+9µs) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 204 of Moose/Util/TypeConstraints/Builtins.pm
# once (6µs+9µs) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 300 of Moose/Util/TypeConstraints/Builtins.pm
# once (6µs+8µs) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 274 of Moose/Util/TypeConstraints/Builtins.pm | ||||
310 | 104 | 26µs | my $type = shift; | ||
311 | |||||
312 | 104 | 216µs | 104 | 46µs | if ( blessed $type and $type->isa("Moose::Meta::TypeConstraint") ) { # spent 46µs making 104 calls to Scalar::Util::blessed, avg 444ns/call |
313 | return $type; | ||||
314 | } | ||||
315 | else { | ||||
316 | 104 | 142µs | 104 | 542µs | return unless $REGISTRY->has_type_constraint($type); # spent 542µs making 104 calls to Moose::Meta::TypeConstraint::Registry::has_type_constraint, avg 5µs/call |
317 | 92 | 259µs | 92 | 394µs | return $REGISTRY->get_type_constraint($type); # spent 394µs making 92 calls to Moose::Meta::TypeConstraint::Registry::get_type_constraint, avg 4µs/call |
318 | } | ||||
319 | } | ||||
320 | |||||
321 | sub register_type_constraint { | ||||
322 | my $constraint = shift; | ||||
323 | __PACKAGE__->_throw_error("can't register an unnamed type constraint") | ||||
324 | unless defined $constraint->name; | ||||
325 | $REGISTRY->add_type_constraint($constraint); | ||||
326 | return $constraint; | ||||
327 | } | ||||
328 | |||||
329 | # type constructors | ||||
330 | |||||
331 | # spent 352µs (12+340) within Moose::Util::TypeConstraints::type which was called:
# once (12µs+340µs) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 16 of Moose/Util/TypeConstraints/Builtins.pm | ||||
332 | 1 | 500ns | my $name = shift; | ||
333 | |||||
334 | 5 | 6µs | my %p = map { %{$_} } @_; | ||
335 | |||||
336 | 1 | 6µs | 1 | 340µs | return _create_type_constraint( # spent 340µs making 1 call to Moose::Util::TypeConstraints::_create_type_constraint |
337 | $name, undef, $p{where}, $p{message}, | ||||
338 | $p{optimize_as}, $p{inline_as}, | ||||
339 | ); | ||||
340 | } | ||||
341 | |||||
342 | # spent 6.80ms (195µs+6.61) within Moose::Util::TypeConstraints::subtype which was called 19 times, avg 358µs/call:
# 16 times (161µs+5.40ms) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 17 of Moose/Util/TypeConstraints/Builtins.pm, avg 347µs/call
# once (11µs+476µs) by MongoDB::BEGIN@32 at line 30 of MongoDB/DBRef.pm
# once (9µs+434µs) by MongoDB::BEGIN@32 at line 31 of MongoDB/DBRef.pm
# once (15µs+299µs) by MongoDB::Connection::BEGIN@28 at line 148 of MongoDB/MongoClient.pm | ||||
343 | 19 | 8µs | if ( @_ == 1 && !ref $_[0] ) { | ||
344 | __PACKAGE__->_throw_error( | ||||
345 | 'A subtype cannot consist solely of a name, it must have a parent' | ||||
346 | ); | ||||
347 | } | ||||
348 | |||||
349 | # The blessed check is mostly to accommodate MooseX::Types, which | ||||
350 | # uses an object which overloads stringification as a type name. | ||||
351 | 19 | 10µs | my $name = ref $_[0] && !blessed $_[0] ? undef : shift; | ||
352 | |||||
353 | 119 | 94µs | my %p = map { %{$_} } @_; | ||
354 | |||||
355 | # subtype Str => where { ... }; | ||||
356 | 19 | 7µs | if ( !exists $p{as} ) { | ||
357 | 1 | 700ns | $p{as} = $name; | ||
358 | 1 | 200ns | $name = undef; | ||
359 | } | ||||
360 | |||||
361 | 19 | 81µs | 19 | 6.61ms | return _create_type_constraint( # spent 6.61ms making 19 calls to Moose::Util::TypeConstraints::_create_type_constraint, avg 348µs/call |
362 | $name, $p{as}, $p{where}, $p{message}, | ||||
363 | $p{optimize_as}, $p{inline_as}, | ||||
364 | ); | ||||
365 | } | ||||
366 | |||||
367 | # spent 6.14ms (26µs+6.11) within Moose::Util::TypeConstraints::class_type which was called 9 times, avg 682µs/call:
# 9 times (26µs+6.11ms) by Moose::init_meta at line 165 of Moose.pm, avg 682µs/call | ||||
368 | 9 | 25µs | 9 | 6.11ms | create_class_type_constraint(@_); # spent 6.11ms making 9 calls to Moose::Util::TypeConstraints::create_class_type_constraint, avg 679µs/call |
369 | } | ||||
370 | |||||
371 | sub role_type ($;$) { | ||||
372 | create_role_type_constraint(@_); | ||||
373 | } | ||||
374 | |||||
375 | sub maybe_type { | ||||
376 | my ($type_parameter) = @_; | ||||
377 | |||||
378 | register_type_constraint( | ||||
379 | $REGISTRY->get_type_constraint('Maybe')->parameterize($type_parameter) | ||||
380 | ); | ||||
381 | } | ||||
382 | |||||
383 | sub duck_type { | ||||
384 | my ( $type_name, @methods ) = @_; | ||||
385 | if ( ref $type_name eq 'ARRAY' && !@methods ) { | ||||
386 | @methods = @$type_name; | ||||
387 | $type_name = undef; | ||||
388 | } | ||||
389 | if ( @methods == 1 && ref $methods[0] eq 'ARRAY' ) { | ||||
390 | @methods = @{ $methods[0] }; | ||||
391 | } | ||||
392 | |||||
393 | register_type_constraint( | ||||
394 | create_duck_type_constraint( | ||||
395 | $type_name, | ||||
396 | \@methods, | ||||
397 | ) | ||||
398 | ); | ||||
399 | } | ||||
400 | |||||
401 | # spent 628µs (10+618) within Moose::Util::TypeConstraints::coerce which was called 2 times, avg 314µs/call:
# once (6µs+385µs) by MongoDB::BEGIN@32 at line 35 of MongoDB/DBRef.pm
# once (3µs+233µs) by MongoDB::BEGIN@32 at line 39 of MongoDB/DBRef.pm | ||||
402 | 2 | 3µs | my ( $type_name, @coercion_map ) = @_; | ||
403 | 2 | 7µs | 2 | 618µs | _install_type_coercions( $type_name, \@coercion_map ); # spent 618µs making 2 calls to Moose::Util::TypeConstraints::_install_type_coercions, avg 309µs/call |
404 | } | ||||
405 | |||||
406 | # The trick of returning @_ lets us avoid having to specify a | ||||
407 | # prototype. Perl will parse this: | ||||
408 | # | ||||
409 | # subtype 'Foo' | ||||
410 | # => as 'Str' | ||||
411 | # => where { ... } | ||||
412 | # | ||||
413 | # as this: | ||||
414 | # | ||||
415 | # subtype( 'Foo', as( 'Str', where { ... } ) ); | ||||
416 | # | ||||
417 | # If as() returns all its extra arguments, this just works, and | ||||
418 | # preserves backwards compatibility. | ||||
419 | 18 | 52µs | # spent 34µs within Moose::Util::TypeConstraints::as which was called 18 times, avg 2µs/call:
# 16 times (27µs+0s) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 18 of Moose/Util/TypeConstraints/Builtins.pm, avg 2µs/call
# once (5µs+0s) by MongoDB::BEGIN@32 at line 30 of MongoDB/DBRef.pm
# once (2µs+0s) by MongoDB::BEGIN@32 at line 31 of MongoDB/DBRef.pm | ||
420 | 17 | 41µs | # spent 31µs within Moose::Util::TypeConstraints::where which was called 17 times, avg 2µs/call:
# 16 times (23µs+0s) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 19 of Moose/Util/TypeConstraints/Builtins.pm, avg 1µs/call
# once (7µs+0s) by MongoDB::Connection::BEGIN@28 at line 148 of MongoDB/MongoClient.pm | ||
421 | sub message (&) { { message => $_[0] } } | ||||
422 | sub optimize_as (&) { { optimize_as => $_[0] } } | ||||
423 | 17 | 35µs | # spent 20µs within Moose::Util::TypeConstraints::inline_as which was called 17 times, avg 1µs/call:
# 17 times (20µs+0s) by Moose::Util::TypeConstraints::Builtins::define_builtins at line 21 of Moose/Util/TypeConstraints/Builtins.pm, avg 1µs/call | ||
424 | |||||
425 | 2 | 5µs | # spent 4µs within Moose::Util::TypeConstraints::from which was called 2 times, avg 2µs/call:
# once (2µs+0s) by MongoDB::BEGIN@32 at line 35 of MongoDB/DBRef.pm
# once (1µs+0s) by MongoDB::BEGIN@32 at line 39 of MongoDB/DBRef.pm | ||
426 | 2 | 6µs | # spent 3µs within Moose::Util::TypeConstraints::via which was called 2 times, avg 2µs/call:
# once (2µs+0s) by MongoDB::BEGIN@32 at line 35 of MongoDB/DBRef.pm
# once (1µs+0s) by MongoDB::BEGIN@32 at line 39 of MongoDB/DBRef.pm | ||
427 | |||||
428 | sub enum { | ||||
429 | my ( $type_name, @values ) = @_; | ||||
430 | |||||
431 | # NOTE: | ||||
432 | # if only an array-ref is passed then | ||||
433 | # you get an anon-enum | ||||
434 | # - SL | ||||
435 | if ( ref $type_name eq 'ARRAY' ) { | ||||
436 | @values == 0 | ||||
437 | || __PACKAGE__->_throw_error("enum called with an array reference and additional arguments. Did you mean to parenthesize the enum call's parameters?"); | ||||
438 | |||||
439 | @values = @$type_name; | ||||
440 | $type_name = undef; | ||||
441 | } | ||||
442 | if ( @values == 1 && ref $values[0] eq 'ARRAY' ) { | ||||
443 | @values = @{ $values[0] }; | ||||
444 | } | ||||
445 | |||||
446 | register_type_constraint( | ||||
447 | create_enum_type_constraint( | ||||
448 | $type_name, | ||||
449 | \@values, | ||||
450 | ) | ||||
451 | ); | ||||
452 | } | ||||
453 | |||||
454 | sub union { | ||||
455 | my ( $type_name, @constraints ) = @_; | ||||
456 | if ( ref $type_name eq 'ARRAY' ) { | ||||
457 | @constraints == 0 | ||||
458 | || __PACKAGE__->_throw_error("union called with an array reference and additional arguments."); | ||||
459 | @constraints = @$type_name; | ||||
460 | $type_name = undef; | ||||
461 | } | ||||
462 | if ( @constraints == 1 && ref $constraints[0] eq 'ARRAY' ) { | ||||
463 | @constraints = @{ $constraints[0] }; | ||||
464 | } | ||||
465 | if ( defined $type_name ) { | ||||
466 | return register_type_constraint( | ||||
467 | create_named_type_constraint_union( $type_name, @constraints ) | ||||
468 | ); | ||||
469 | } | ||||
470 | return create_type_constraint_union( @constraints ); | ||||
471 | } | ||||
472 | |||||
473 | sub create_enum_type_constraint { | ||||
474 | my ( $type_name, $values ) = @_; | ||||
475 | |||||
476 | Moose::Meta::TypeConstraint::Enum->new( | ||||
477 | name => $type_name || '__ANON__', | ||||
478 | values => $values, | ||||
479 | ); | ||||
480 | } | ||||
481 | |||||
482 | sub create_duck_type_constraint { | ||||
483 | my ( $type_name, $methods ) = @_; | ||||
484 | |||||
485 | Moose::Meta::TypeConstraint::DuckType->new( | ||||
486 | name => $type_name || '__ANON__', | ||||
487 | methods => $methods, | ||||
488 | ); | ||||
489 | } | ||||
490 | |||||
491 | sub match_on_type { | ||||
492 | my ($to_match, @cases) = @_; | ||||
493 | my $default; | ||||
494 | if (@cases % 2 != 0) { | ||||
495 | $default = pop @cases; | ||||
496 | (ref $default eq 'CODE') | ||||
497 | || __PACKAGE__->_throw_error("Default case must be a CODE ref, not $default"); | ||||
498 | } | ||||
499 | while (@cases) { | ||||
500 | my ($type, $action) = splice @cases, 0, 2; | ||||
501 | |||||
502 | unless (blessed $type && $type->isa('Moose::Meta::TypeConstraint')) { | ||||
503 | $type = find_or_parse_type_constraint($type) | ||||
504 | || __PACKAGE__->_throw_error("Cannot find or parse the type '$type'") | ||||
505 | } | ||||
506 | |||||
507 | (ref $action eq 'CODE') | ||||
508 | || __PACKAGE__->_throw_error("Match action must be a CODE ref, not $action"); | ||||
509 | |||||
510 | if ($type->check($to_match)) { | ||||
511 | local $_ = $to_match; | ||||
512 | return $action->($to_match); | ||||
513 | } | ||||
514 | } | ||||
515 | (defined $default) | ||||
516 | || __PACKAGE__->_throw_error("No cases matched for $to_match"); | ||||
517 | { | ||||
518 | local $_ = $to_match; | ||||
519 | return $default->($to_match); | ||||
520 | } | ||||
521 | } | ||||
522 | |||||
523 | |||||
524 | ## -------------------------------------------------------- | ||||
525 | ## desugaring functions ... | ||||
526 | ## -------------------------------------------------------- | ||||
527 | |||||
528 | sub _create_type_constraint ($$$;$$) { | ||||
529 | 20 | 6µs | my $name = shift; | ||
530 | 20 | 4µs | my $parent = shift; | ||
531 | 20 | 4µs | my $check = shift; | ||
532 | 20 | 15µs | my $message = shift; | ||
533 | 20 | 5µs | my $optimized = shift; | ||
534 | 20 | 4µs | my $inlined = shift; | ||
535 | |||||
536 | 20 | 10µs | my $pkg_defined_in = scalar( caller(1) ); | ||
537 | |||||
538 | 20 | 7µs | if ( defined $name ) { | ||
539 | 19 | 23µs | 19 | 100µs | my $type = $REGISTRY->get_type_constraint($name); # spent 100µs making 19 calls to Moose::Meta::TypeConstraint::Registry::get_type_constraint, avg 5µs/call |
540 | |||||
541 | 19 | 4µs | ( $type->_package_defined_in eq $pkg_defined_in ) | ||
542 | || _confess( | ||||
543 | "The type constraint '$name' has already been created in " | ||||
544 | . $type->_package_defined_in | ||||
545 | . " and cannot be created again in " | ||||
546 | . $pkg_defined_in ) | ||||
547 | if defined $type; | ||||
548 | |||||
549 | 19 | 87µs | 19 | 50µs | $name =~ /^[\w:\.]+$/ # spent 50µs making 19 calls to Moose::Util::TypeConstraints::CORE:match, avg 3µs/call |
550 | or die qq{$name contains invalid characters for a type name.} | ||||
551 | . qq{ Names can contain alphanumeric character, ":", and "."\n}; | ||||
552 | } | ||||
553 | |||||
554 | 20 | 44µs | my %opts = ( | ||
555 | name => $name, | ||||
556 | package_defined_in => $pkg_defined_in, | ||||
557 | |||||
558 | ( $check ? ( constraint => $check ) : () ), | ||||
559 | ( $message ? ( message => $message ) : () ), | ||||
560 | ( $optimized ? ( optimized => $optimized ) : () ), | ||||
561 | ( $inlined ? ( inlined => $inlined ) : () ), | ||||
562 | ); | ||||
563 | |||||
564 | 20 | 3µs | my $constraint; | ||
565 | 20 | 104µs | 57 | 648µs | if ( # spent 623µs making 19 calls to Moose::Util::TypeConstraints::find_or_create_isa_type_constraint, avg 33µs/call
# spent 13µs making 19 calls to Scalar::Util::blessed, avg 684ns/call
# spent 12µs making 19 calls to Moose::Meta::TypeConstraint::__ANON__[Moose/Meta/TypeConstraint.pm:16], avg 611ns/call |
566 | defined $parent | ||||
567 | and $parent | ||||
568 | = blessed $parent | ||||
569 | ? $parent | ||||
570 | : find_or_create_isa_type_constraint($parent) | ||||
571 | ) { | ||||
572 | 19 | 42µs | 19 | 5.10ms | $constraint = $parent->create_child_type(%opts); # spent 5.10ms making 19 calls to Moose::Meta::TypeConstraint::create_child_type, avg 268µs/call |
573 | } | ||||
574 | else { | ||||
575 | 1 | 3µs | 1 | 274µs | $constraint = Moose::Meta::TypeConstraint->new(%opts); # spent 274µs making 1 call to Moose::Meta::TypeConstraint::new |
576 | } | ||||
577 | |||||
578 | 20 | 27µs | 19 | 331µs | $REGISTRY->add_type_constraint($constraint) # spent 331µs making 19 calls to Moose::Meta::TypeConstraint::Registry::add_type_constraint, avg 17µs/call |
579 | if defined $name; | ||||
580 | |||||
581 | 20 | 49µs | return $constraint; | ||
582 | } | ||||
583 | |||||
584 | # spent 618µs (28+590) within Moose::Util::TypeConstraints::_install_type_coercions which was called 2 times, avg 309µs/call:
# 2 times (28µs+590µs) by Moose::Util::TypeConstraints::coerce at line 403, avg 309µs/call | ||||
585 | 2 | 2µs | my ( $type_name, $coercion_map ) = @_; | ||
586 | 2 | 3µs | 2 | 30µs | my $type = find_type_constraint($type_name); # spent 30µs making 2 calls to Moose::Util::TypeConstraints::find_type_constraint, avg 15µs/call |
587 | 2 | 400ns | ( defined $type ) | ||
588 | || __PACKAGE__->_throw_error( | ||||
589 | "Cannot find type '$type_name', perhaps you forgot to load it"); | ||||
590 | 2 | 8µs | 2 | 3µs | if ( $type->has_coercion ) { # spent 3µs making 2 calls to Moose::Meta::TypeConstraint::has_coercion, avg 1µs/call |
591 | $type->coercion->add_type_coercions(@$coercion_map); | ||||
592 | } | ||||
593 | else { | ||||
594 | 2 | 5µs | 2 | 548µs | my $type_coercion = Moose::Meta::TypeCoercion->new( # spent 548µs making 2 calls to Moose::Meta::TypeCoercion::new, avg 274µs/call |
595 | type_coercion_map => $coercion_map, | ||||
596 | type_constraint => $type | ||||
597 | ); | ||||
598 | 2 | 4µs | 2 | 9µs | $type->coercion($type_coercion); # spent 9µs making 2 calls to Moose::Meta::TypeConstraint::coercion, avg 4µs/call |
599 | } | ||||
600 | } | ||||
601 | |||||
602 | ## -------------------------------------------------------- | ||||
603 | ## type notation parsing ... | ||||
604 | ## -------------------------------------------------------- | ||||
605 | |||||
606 | { | ||||
607 | |||||
608 | # All I have to say is mugwump++ cause I know | ||||
609 | # do not even have enough regexp-fu to be able | ||||
610 | # to have written this (I can only barely | ||||
611 | # understand it as it is) | ||||
612 | # - SL | ||||
613 | |||||
614 | 3 | 253µs | 2 | 56µs | # spent 34µs (13+21) within Moose::Util::TypeConstraints::BEGIN@614 which was called:
# once (13µs+21µs) by Moose::Meta::Attribute::BEGIN@24 at line 614 # spent 34µs making 1 call to Moose::Util::TypeConstraints::BEGIN@614
# spent 21µs making 1 call to re::import |
615 | |||||
616 | 1 | 13µs | 1 | 6µs | my $valid_chars = qr{[\w:\.]}; # spent 6µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
617 | 1 | 19µs | 2 | 13µs | my $type_atom = qr{ (?>$valid_chars+) }x; # spent 12µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
618 | 1 | 3µs | 1 | 900ns | my $ws = qr{ (?>\s*) }x; # spent 900ns making 1 call to Moose::Util::TypeConstraints::CORE:qr |
619 | 1 | 16µs | 2 | 12µs | my $op_union = qr{ $ws \| $ws }x; # spent 11µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
620 | |||||
621 | 1 | 300ns | my ($type, $type_capture_parts, $type_with_parameter, $union, $any); | ||
622 | 1 | 1µs | if (Class::MOP::IS_RUNNING_ON_5_10) { | ||
623 | 1 | 400ns | my $type_pattern | ||
624 | = q{ (?&type_atom) (?: \[ (?&ws) (?&any) (?&ws) \] )? }; | ||||
625 | 1 | 200ns | my $type_capture_parts_pattern | ||
626 | = q{ ((?&type_atom)) (?: \[ (?&ws) ((?&any)) (?&ws) \] )? }; | ||||
627 | 1 | 100ns | my $type_with_parameter_pattern | ||
628 | = q{ (?&type_atom) \[ (?&ws) (?&any) (?&ws) \] }; | ||||
629 | 1 | 200ns | my $union_pattern | ||
630 | = q{ (?&type) (?> (?: (?&op_union) (?&type) )+ ) }; | ||||
631 | 1 | 200ns | my $any_pattern | ||
632 | = q{ (?&type) | (?&union) }; | ||||
633 | |||||
634 | 1 | 62µs | 2 | 54µs | my $defines = qr{(?(DEFINE) # spent 53µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
635 | (?<valid_chars> $valid_chars) | ||||
636 | (?<type_atom> $type_atom) | ||||
637 | (?<ws> $ws) | ||||
638 | (?<op_union> $op_union) | ||||
639 | (?<type> $type_pattern) | ||||
640 | (?<type_capture_parts> $type_capture_parts_pattern) | ||||
641 | (?<type_with_parameter> $type_with_parameter_pattern) | ||||
642 | (?<union> $union_pattern) | ||||
643 | (?<any> $any_pattern) | ||||
644 | )}x; | ||||
645 | |||||
646 | 1 | 47µs | 2 | 40µs | $type = qr{ $type_pattern $defines }x; # spent 39µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
647 | 1 | 49µs | 2 | 38µs | $type_capture_parts = qr{ $type_capture_parts_pattern $defines }x; # spent 37µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
648 | 1 | 46µs | 2 | 40µs | $type_with_parameter = qr{ $type_with_parameter_pattern $defines }x; # spent 39µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
649 | 1 | 43µs | 2 | 34µs | $union = qr{ $union_pattern $defines }x; # spent 33µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
650 | 1 | 43µs | 2 | 34µs | $any = qr{ $any_pattern $defines }x; # spent 33µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 1µs making 1 call to Moose::Util::TypeConstraints::CORE:qr |
651 | } | ||||
652 | else { | ||||
653 | $type | ||||
654 | = qr{ $type_atom (?: \[ $ws (??{$any}) $ws \] )? }x; | ||||
655 | $type_capture_parts | ||||
656 | = qr{ ($type_atom) (?: \[ $ws ((??{$any})) $ws \] )? }x; | ||||
657 | $type_with_parameter | ||||
658 | = qr{ $type_atom \[ $ws (??{$any}) $ws \] }x; | ||||
659 | $union | ||||
660 | = qr{ $type (?> (?: $op_union $type )+ ) }x; | ||||
661 | $any | ||||
662 | = qr{ $type | $union }x; | ||||
663 | } | ||||
664 | |||||
665 | |||||
666 | sub _parse_parameterized_type_constraint { | ||||
667 | 2 | 63µs | 2 | 33µs | # spent 21µs (9+12) within Moose::Util::TypeConstraints::BEGIN@667 which was called:
# once (9µs+12µs) by Moose::Meta::Attribute::BEGIN@24 at line 667 # spent 21µs making 1 call to Moose::Util::TypeConstraints::BEGIN@667
# spent 12µs making 1 call to warnings::unimport |
668 | $_[0] =~ m{ $type_capture_parts }x; | ||||
669 | return ( $1, $2 ); | ||||
670 | } | ||||
671 | |||||
672 | # spent 58µs (14+44) within Moose::Util::TypeConstraints::_detect_parameterized_type_constraint which was called 2 times, avg 29µs/call:
# 2 times (14µs+44µs) by Moose::Util::TypeConstraints::find_or_parse_type_constraint at line 274, avg 29µs/call | ||||
673 | 6 | 54µs | 2 | 26µs | # spent 17µs (8+9) within Moose::Util::TypeConstraints::BEGIN@673 which was called:
# once (8µs+9µs) by Moose::Meta::Attribute::BEGIN@24 at line 673 # spent 17µs making 1 call to Moose::Util::TypeConstraints::BEGIN@673
# spent 9µs making 1 call to warnings::unimport |
674 | 2 | 57µs | 4 | 44µs | $_[0] =~ m{ ^ $type_with_parameter $ }x; # spent 43µs making 2 calls to Moose::Util::TypeConstraints::CORE:regcomp, avg 21µs/call
# spent 1µs making 2 calls to Moose::Util::TypeConstraints::CORE:match, avg 550ns/call |
675 | } | ||||
676 | |||||
677 | # spent 87µs (26+62) within Moose::Util::TypeConstraints::_parse_type_constraint_union which was called:
# once (26µs+62µs) by Moose::Util::TypeConstraints::_create_type_constraint_union at line 94 | ||||
678 | 4 | 103µs | 2 | 24µs | # spent 16µs (7+9) within Moose::Util::TypeConstraints::BEGIN@678 which was called:
# once (7µs+9µs) by Moose::Meta::Attribute::BEGIN@24 at line 678 # spent 16µs making 1 call to Moose::Util::TypeConstraints::BEGIN@678
# spent 9µs making 1 call to warnings::unimport |
679 | 1 | 600ns | my $given = shift; | ||
680 | 1 | 300ns | my @rv; | ||
681 | 1 | 66µs | 2 | 58µs | while ( $given =~ m{ \G (?: $op_union )? ($type) }gcx ) { # spent 54µs making 1 call to Moose::Util::TypeConstraints::CORE:regcomp
# spent 4µs making 1 call to Moose::Util::TypeConstraints::CORE:match |
682 | 2 | 16µs | 4 | 4µs | push @rv => $1; # spent 2µs making 2 calls to Moose::Util::TypeConstraints::CORE:match, avg 1µs/call
# spent 2µs making 2 calls to Moose::Util::TypeConstraints::CORE:regcomp, avg 800ns/call |
683 | } | ||||
684 | 1 | 2µs | ( pos($given) eq length($given) ) | ||
685 | || __PACKAGE__->_throw_error( "'$given' didn't parse (parse-pos=" | ||||
686 | . pos($given) | ||||
687 | . " and str-length=" | ||||
688 | . length($given) | ||||
689 | . ")" ); | ||||
690 | 1 | 4µs | @rv; | ||
691 | } | ||||
692 | |||||
693 | # spent 140µs (29+111) within Moose::Util::TypeConstraints::_detect_type_constraint_union which was called 4 times, avg 35µs/call:
# 3 times (25µs+107µs) by Moose::Util::TypeConstraints::find_or_parse_type_constraint at line 274, avg 44µs/call
# once (4µs+4µs) by Moose::Util::TypeConstraints::_create_type_constraint_union at line 93 | ||||
694 | 10 | 272µs | 2 | 26µs | # spent 17µs (8+9) within Moose::Util::TypeConstraints::BEGIN@694 which was called:
# once (8µs+9µs) by Moose::Meta::Attribute::BEGIN@24 at line 694 # spent 17µs making 1 call to Moose::Util::TypeConstraints::BEGIN@694
# spent 9µs making 1 call to warnings::unimport |
695 | 4 | 144µs | 8 | 111µs | $_[0] =~ m{^ $type $op_union $type ( $op_union .* )? $}x; # spent 98µs making 4 calls to Moose::Util::TypeConstraints::CORE:regcomp, avg 25µs/call
# spent 12µs making 4 calls to Moose::Util::TypeConstraints::CORE:match, avg 3µs/call |
696 | } | ||||
697 | } | ||||
698 | |||||
699 | ## -------------------------------------------------------- | ||||
700 | # define some basic built-in types | ||||
701 | ## -------------------------------------------------------- | ||||
702 | |||||
703 | # By making these classes immutable before creating all the types in | ||||
704 | # Moose::Util::TypeConstraints::Builtin , we avoid repeatedly calling the slow | ||||
705 | # MOP-based accessors. | ||||
706 | $_->make_immutable( | ||||
707 | 9 | 7µs | 9 | 5µs | inline_constructor => 1, # spent 5µs making 9 calls to Class::MOP::Class::is_mutable, avg 589ns/call |
708 | constructor_name => "_new", | ||||
709 | |||||
710 | # these are Class::MOP accessors, so they need inlining | ||||
711 | inline_accessors => 1 | ||||
712 | 10 | 27µs | 18 | 33.0ms | ) for grep { $_->is_mutable } # spent 32.9ms making 9 calls to Class::MOP::Class::make_immutable, avg 3.66ms/call
# spent 30µs making 9 calls to Class::MOP::class_of, avg 3µs/call |
713 | map { Class::MOP::class_of($_) } | ||||
714 | qw( | ||||
715 | Moose::Meta::TypeConstraint | ||||
716 | Moose::Meta::TypeConstraint::Union | ||||
717 | Moose::Meta::TypeConstraint::Parameterized | ||||
718 | Moose::Meta::TypeConstraint::Parameterizable | ||||
719 | Moose::Meta::TypeConstraint::Class | ||||
720 | Moose::Meta::TypeConstraint::Role | ||||
721 | Moose::Meta::TypeConstraint::Enum | ||||
722 | Moose::Meta::TypeConstraint::DuckType | ||||
723 | Moose::Meta::TypeConstraint::Registry | ||||
724 | ); | ||||
725 | |||||
726 | 1 | 102µs | require Moose::Util::TypeConstraints::Builtins; | ||
727 | 1 | 2µs | 1 | 7.64ms | Moose::Util::TypeConstraints::Builtins::define_builtins($REGISTRY); # spent 7.64ms making 1 call to Moose::Util::TypeConstraints::Builtins::define_builtins |
728 | |||||
729 | my @PARAMETERIZABLE_TYPES | ||||
730 | 5 | 6µs | 4 | 16µs | = map { $REGISTRY->get_type_constraint($_) } qw[ScalarRef ArrayRef HashRef Maybe]; # spent 16µs making 4 calls to Moose::Meta::TypeConstraint::Registry::get_type_constraint, avg 4µs/call |
731 | |||||
732 | sub get_all_parameterizable_types {@PARAMETERIZABLE_TYPES} | ||||
733 | |||||
734 | sub add_parameterizable_type { | ||||
735 | my $type = shift; | ||||
736 | ( blessed $type | ||||
737 | && $type->isa('Moose::Meta::TypeConstraint::Parameterizable') ) | ||||
738 | || __PACKAGE__->_throw_error( | ||||
739 | "Type must be a Moose::Meta::TypeConstraint::Parameterizable not $type" | ||||
740 | ); | ||||
741 | push @PARAMETERIZABLE_TYPES => $type; | ||||
742 | } | ||||
743 | |||||
744 | ## -------------------------------------------------------- | ||||
745 | # end of built-in types ... | ||||
746 | ## -------------------------------------------------------- | ||||
747 | |||||
748 | { | ||||
749 | 2 | 4µs | 1 | 7µs | my @BUILTINS = list_all_type_constraints(); # spent 7µs making 1 call to Moose::Util::TypeConstraints::list_all_type_constraints |
750 | sub list_all_builtin_type_constraints {@BUILTINS} | ||||
751 | } | ||||
752 | |||||
753 | sub _throw_error { | ||||
754 | shift; | ||||
755 | require Moose; | ||||
756 | unshift @_, 'Moose'; | ||||
757 | goto &Moose::throw_error; | ||||
758 | } | ||||
759 | |||||
760 | 1 | 27µs | 1; | ||
761 | |||||
762 | # ABSTRACT: Type constraint system for Moose | ||||
763 | |||||
764 | __END__ | ||||
# spent 69µs within Moose::Util::TypeConstraints::CORE:match which was called 28 times, avg 2µs/call:
# 19 times (50µs+0s) by Moose::Util::TypeConstraints::_create_type_constraint at line 549, avg 3µs/call
# 4 times (12µs+0s) by Moose::Util::TypeConstraints::_detect_type_constraint_union at line 695, avg 3µs/call
# 2 times (2µs+0s) by Moose::Util::TypeConstraints::_parse_type_constraint_union at line 682, avg 1µs/call
# 2 times (1µs+0s) by Moose::Util::TypeConstraints::_detect_parameterized_type_constraint at line 674, avg 550ns/call
# once (4µs+0s) by Moose::Util::TypeConstraints::_parse_type_constraint_union at line 681 | |||||
# spent 15µs within Moose::Util::TypeConstraints::CORE:qr which was called 10 times, avg 2µs/call:
# once (6µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 616
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 634
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 646
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 649
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 648
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 650
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 617
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 619
# once (1µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 647
# once (900ns+0s) by Moose::Meta::Attribute::BEGIN@24 at line 618 | |||||
# spent 454µs within Moose::Util::TypeConstraints::CORE:regcomp which was called 17 times, avg 27µs/call:
# 4 times (98µs+0s) by Moose::Util::TypeConstraints::_detect_type_constraint_union at line 695, avg 25µs/call
# 2 times (43µs+0s) by Moose::Util::TypeConstraints::_detect_parameterized_type_constraint at line 674, avg 21µs/call
# 2 times (2µs+0s) by Moose::Util::TypeConstraints::_parse_type_constraint_union at line 682, avg 800ns/call
# once (54µs+0s) by Moose::Util::TypeConstraints::_parse_type_constraint_union at line 681
# once (53µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 634
# once (39µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 648
# once (39µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 646
# once (37µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 647
# once (33µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 650
# once (33µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 649
# once (12µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 617
# once (11µs+0s) by Moose::Meta::Attribute::BEGIN@24 at line 619 | |||||
# spent 65µs within Moose::Util::TypeConstraints::CORE:subst which was called 75 times, avg 868ns/call:
# 75 times (65µs+0s) by Moose::Util::TypeConstraints::normalize_type_constraint_name at line 294, avg 868ns/call |