| Filename | /usr/local/lib/perl/5.18.2/Moose/Meta/TypeConstraint/Union.pm |
| Statements | Executed 265 statements in 1.35ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 322µs | 840µs | Moose::Meta::TypeConstraint::Union::BEGIN@8 |
| 22 | 2 | 2 | 155µs | 525µs | Moose::Meta::TypeConstraint::Union::can_be_inlined |
| 12 | 2 | 2 | 94µs | 271µs | Moose::Meta::TypeConstraint::Union::inline_environment |
| 8 | 3 | 2 | 91µs | 510µs | Moose::Meta::TypeConstraint::Union::_inline_check |
| 2 | 1 | 1 | 52µs | 335µs | Moose::Meta::TypeConstraint::Union::new |
| 2 | 1 | 1 | 18µs | 22µs | Moose::Meta::TypeConstraint::Union::_actually_compile_type_constraint |
| 1 | 1 | 1 | 14µs | 56µs | Moose::Meta::TypeConstraint::Union::BEGIN@10 |
| 1 | 1 | 1 | 11µs | 14µs | Moose::Meta::TypeConstraint::Union::coercion |
| 1 | 1 | 1 | 9µs | 18µs | Moose::Meta::TypeConstraint::Union::BEGIN@4 |
| 1 | 1 | 1 | 7µs | 481µs | Moose::Meta::TypeConstraint::Union::BEGIN@6 |
| 1 | 1 | 1 | 6µs | 26µs | Moose::Meta::TypeConstraint::Union::BEGIN@12 |
| 1 | 1 | 1 | 6µs | 9µs | Moose::Meta::TypeConstraint::Union::BEGIN@5 |
| 1 | 1 | 1 | 4µs | 18µs | Moose::Meta::TypeConstraint::Union::has_coercion |
| 2 | 1 | 1 | 2µs | 2µs | Moose::Meta::TypeConstraint::Union::CORE:sort (opcode) |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:136] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:157] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:163] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:169] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:16] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:71] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::create_child_type |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::equals |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::find_type_for |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::is_a_type_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::is_subtype_of |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::parent |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::validate |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Moose::Meta::TypeConstraint::Union; | ||||
| 2 | 1 | 500ns | our $VERSION = '2.1604'; | ||
| 3 | |||||
| 4 | 2 | 19µs | 2 | 27µs | # spent 18µs (9+9) within Moose::Meta::TypeConstraint::Union::BEGIN@4 which was called:
# once (9µs+9µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 4 # spent 18µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@4
# spent 9µs making 1 call to strict::import |
| 5 | 2 | 18µs | 2 | 12µs | # spent 9µs (6+3) within Moose::Meta::TypeConstraint::Union::BEGIN@5 which was called:
# once (6µs+3µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 5 # spent 9µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@5
# spent 3µs making 1 call to warnings::import |
| 6 | 2 | 24µs | 2 | 955µs | # spent 481µs (7+474) within Moose::Meta::TypeConstraint::Union::BEGIN@6 which was called:
# once (7µs+474µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 6 # spent 481µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@6
# spent 474µs making 1 call to metaclass::import |
| 7 | |||||
| 8 | 2 | 104µs | 1 | 840µs | # spent 840µs (322+518) within Moose::Meta::TypeConstraint::Union::BEGIN@8 which was called:
# once (322µs+518µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 8 # spent 840µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@8 |
| 9 | |||||
| 10 | 3 | 39µs | 3 | 75µs | # spent 56µs (14+42) within Moose::Meta::TypeConstraint::Union::BEGIN@10 which was called:
# once (14µs+42µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 10 # spent 56µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@10
# spent 10µs making 1 call to UNIVERSAL::VERSION
# spent 8µs making 1 call to List::Util::import |
| 11 | |||||
| 12 | 2 | 786µs | 2 | 45µs | # spent 26µs (6+20) within Moose::Meta::TypeConstraint::Union::BEGIN@12 which was called:
# once (6µs+20µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 12 # spent 26µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@12
# spent 20µs making 1 call to parent::import |
| 13 | |||||
| 14 | __PACKAGE__->meta->add_attribute('type_constraints' => ( | ||||
| 15 | accessor => 'type_constraints', | ||||
| 16 | default => sub { [] }, | ||||
| 17 | 1 | 7µs | 3 | 404µs | Class::MOP::_definition_context(), # spent 381µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 15µs making 1 call to Moose::Meta::TypeConstraint::Union::meta
# spent 7µs making 1 call to Class::MOP::_definition_context |
| 18 | )); | ||||
| 19 | |||||
| 20 | # spent 335µs (52+282) within Moose::Meta::TypeConstraint::Union::new which was called 2 times, avg 167µs/call:
# 2 times (52µs+282µs) by Moose::Util::TypeConstraints::_create_type_constraint_union at line 107 of Moose/Util/TypeConstraints.pm, avg 167µs/call | ||||
| 21 | 2 | 2µs | my ($class, %options) = @_; | ||
| 22 | |||||
| 23 | 4 | 5µs | 4 | 9µs | my $name = join '|' => sort { $a cmp $b } # spent 9µs making 4 calls to Moose::Meta::TypeConstraint::name, avg 2µs/call |
| 24 | 2 | 17µs | 2 | 2µs | map { $_->name } @{ $options{type_constraints} }; # spent 2µs making 2 calls to Moose::Meta::TypeConstraint::Union::CORE:sort, avg 1µs/call |
| 25 | |||||
| 26 | 2 | 10µs | 2 | 265µs | my $self = $class->SUPER::new( # spent 265µs making 2 calls to Moose::Meta::TypeConstraint::new, avg 132µs/call |
| 27 | name => $name, | ||||
| 28 | %options, | ||||
| 29 | ); | ||||
| 30 | |||||
| 31 | 2 | 7µs | 4 | 6µs | $self->_set_constraint( $self->_compiled_type_constraint ); # spent 4µs making 2 calls to Moose::Meta::TypeConstraint::_set_constraint, avg 2µs/call
# spent 2µs making 2 calls to Moose::Meta::TypeConstraint::_compiled_type_constraint, avg 900ns/call |
| 32 | |||||
| 33 | 2 | 5µs | return $self; | ||
| 34 | } | ||||
| 35 | |||||
| 36 | # XXX - this is a rather gross implementation of laziness for the benefit of | ||||
| 37 | # MX::Types. If we try to call ->has_coercion on the objects during object | ||||
| 38 | # construction, this does not work when defining a recursive constraint with | ||||
| 39 | # MX::Types. | ||||
| 40 | # spent 14µs (11+3) within Moose::Meta::TypeConstraint::Union::coercion which was called:
# once (11µs+3µs) by Moose::Meta::TypeConstraint::Union::has_coercion at line 57 | ||||
| 41 | 1 | 300ns | my $self = shift; | ||
| 42 | |||||
| 43 | 1 | 500ns | return $self->{coercion} if exists $self->{coercion}; | ||
| 44 | |||||
| 45 | # Using any instead of grep here causes a weird error with some corner | ||||
| 46 | # cases when MX::Types is in use. See RT #61001. | ||||
| 47 | 3 | 4µs | 3 | 3µs | if ( grep { $_->has_coercion } @{ $self->type_constraints } ) { # spent 2µs making 2 calls to Moose::Meta::TypeConstraint::has_coercion, avg 950ns/call
# spent 700ns making 1 call to Moose::Meta::TypeConstraint::Union::type_constraints |
| 48 | return $self->{coercion} = Moose::Meta::TypeCoercion::Union->new( | ||||
| 49 | type_constraint => $self ); | ||||
| 50 | } | ||||
| 51 | else { | ||||
| 52 | 1 | 3µs | return $self->{coercion} = undef; | ||
| 53 | } | ||||
| 54 | } | ||||
| 55 | |||||
| 56 | # spent 18µs (4+14) within Moose::Meta::TypeConstraint::Union::has_coercion which was called:
# once (4µs+14µs) by Class::MOP::Class:::around at line 149 of Moose/Meta/Method/Accessor/Native/Collection.pm | ||||
| 57 | 1 | 5µs | 1 | 14µs | return defined $_[0]->coercion; # spent 14µs making 1 call to Moose::Meta::TypeConstraint::Union::coercion |
| 58 | } | ||||
| 59 | |||||
| 60 | # spent 22µs (18+4) within Moose::Meta::TypeConstraint::Union::_actually_compile_type_constraint which was called 2 times, avg 11µs/call:
# 2 times (18µs+4µs) by Moose::Meta::TypeConstraint::compile_type_constraint at line 280 of Moose/Meta/TypeConstraint.pm, avg 11µs/call | ||||
| 61 | 2 | 400ns | my $self = shift; | ||
| 62 | |||||
| 63 | 2 | 5µs | 2 | 4µs | my @constraints = @{ $self->type_constraints }; # spent 4µs making 2 calls to Moose::Meta::TypeConstraint::Union::type_constraints, avg 2µs/call |
| 64 | |||||
| 65 | return sub { | ||||
| 66 | my $value = shift; | ||||
| 67 | foreach my $type (@constraints) { | ||||
| 68 | return 1 if $type->check($value); | ||||
| 69 | } | ||||
| 70 | return undef; | ||||
| 71 | 2 | 12µs | }; | ||
| 72 | } | ||||
| 73 | |||||
| 74 | # spent 525µs (155+370) within Moose::Meta::TypeConstraint::Union::can_be_inlined which was called 22 times, avg 24µs/call:
# 21 times (149µs+355µs) by Moose::Meta::TypeConstraint::Parameterized::can_be_inlined at line 72 of Moose/Meta/TypeConstraint/Parameterized.pm, avg 24µs/call
# once (6µs+15µs) by Moose::Meta::Method::Accessor::Native::Collection::_inline_check_member_constraint at line 103 of Moose/Meta/Method/Accessor/Native/Collection.pm | ||||
| 75 | 22 | 4µs | my $self = shift; | ||
| 76 | |||||
| 77 | # This was originally done with all() from List::MoreUtils, but that | ||||
| 78 | # caused some sort of bizarro parsing failure under 5.10. | ||||
| 79 | 22 | 26µs | 22 | 17µs | for my $tc ( @{ $self->type_constraints } ) { # spent 17µs making 22 calls to Moose::Meta::TypeConstraint::Union::type_constraints, avg 791ns/call |
| 80 | 44 | 52µs | 44 | 352µs | return 0 unless $tc->can_be_inlined; # spent 352µs making 44 calls to Moose::Meta::TypeConstraint::can_be_inlined, avg 8µs/call |
| 81 | } | ||||
| 82 | |||||
| 83 | 22 | 38µs | return 1; | ||
| 84 | } | ||||
| 85 | |||||
| 86 | # spent 510µs (91+419) within Moose::Meta::TypeConstraint::Union::_inline_check which was called 8 times, avg 64µs/call:
# 5 times (62µs+282µs) by Moose::Util::TypeConstraints::Builtins::__ANON__[/usr/local/lib/perl/5.18.2/Moose/Util/TypeConstraints/Builtins.pm:231] at line 223 of Moose/Util/TypeConstraints/Builtins.pm, avg 69µs/call
# 2 times (19µs+88µs) by Moose::Util::TypeConstraints::Builtins::__ANON__[/usr/local/lib/perl/5.18.2/Moose/Util/TypeConstraints/Builtins.pm:292] at line 290 of Moose/Util/TypeConstraints/Builtins.pm, avg 54µs/call
# once (10µs+49µs) by Moose::Meta::Method::Accessor::Native::Collection::_inline_check_member_constraint at line 103 of Moose/Meta/Method/Accessor/Native/Collection.pm | ||||
| 87 | 8 | 2µs | my $self = shift; | ||
| 88 | 8 | 2µs | my $val = shift; | ||
| 89 | |||||
| 90 | 16 | 31µs | 16 | 414µs | return '(' # spent 414µs making 16 calls to Moose::Meta::TypeConstraint::_inline_check, avg 26µs/call |
| 91 | . ( | ||||
| 92 | join ' || ', map { '(' . $_->_inline_check($val) . ')' } | ||||
| 93 | 8 | 47µs | 8 | 5µs | @{ $self->type_constraints } # spent 5µs making 8 calls to Moose::Meta::TypeConstraint::Union::type_constraints, avg 675ns/call |
| 94 | ) | ||||
| 95 | . ')'; | ||||
| 96 | } | ||||
| 97 | |||||
| 98 | # spent 271µs (94+177) within Moose::Meta::TypeConstraint::Union::inline_environment which was called 12 times, avg 23µs/call:
# 11 times (88µs+163µs) by Moose::Meta::TypeConstraint::Parameterized::inline_environment at line 85 of Moose/Meta/TypeConstraint/Parameterized.pm, avg 23µs/call
# once (6µs+14µs) by Class::MOP::Class:::around at line 158 of Moose/Meta/Method/Accessor/Native/Collection.pm | ||||
| 99 | 12 | 4µs | my $self = shift; | ||
| 100 | |||||
| 101 | 48 | 26µs | 24 | 166µs | return { map { %{ $_->inline_environment } } # spent 166µs making 24 calls to Moose::Meta::TypeConstraint::inline_environment, avg 7µs/call |
| 102 | 12 | 44µs | 12 | 11µs | @{ $self->type_constraints } }; # spent 11µs making 12 calls to Moose::Meta::TypeConstraint::Union::type_constraints, avg 900ns/call |
| 103 | } | ||||
| 104 | |||||
| 105 | sub equals { | ||||
| 106 | my ( $self, $type_or_name ) = @_; | ||||
| 107 | |||||
| 108 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); | ||||
| 109 | |||||
| 110 | return unless $other->isa(__PACKAGE__); | ||||
| 111 | |||||
| 112 | my @self_constraints = @{ $self->type_constraints }; | ||||
| 113 | my @other_constraints = @{ $other->type_constraints }; | ||||
| 114 | |||||
| 115 | return unless @self_constraints == @other_constraints; | ||||
| 116 | |||||
| 117 | # FIXME presort type constraints for efficiency? | ||||
| 118 | constraint: foreach my $constraint ( @self_constraints ) { | ||||
| 119 | for ( my $i = 0; $i < @other_constraints; $i++ ) { | ||||
| 120 | if ( $constraint->equals($other_constraints[$i]) ) { | ||||
| 121 | splice @other_constraints, $i, 1; | ||||
| 122 | next constraint; | ||||
| 123 | } | ||||
| 124 | } | ||||
| 125 | } | ||||
| 126 | |||||
| 127 | return @other_constraints == 0; | ||||
| 128 | } | ||||
| 129 | |||||
| 130 | sub parent { | ||||
| 131 | my $self = shift; | ||||
| 132 | |||||
| 133 | my ($first, @rest) = @{ $self->type_constraints }; | ||||
| 134 | |||||
| 135 | for my $parent ( $first->_collect_all_parents ) { | ||||
| 136 | return $parent if all { $_->is_a_type_of($parent) } @rest; | ||||
| 137 | } | ||||
| 138 | |||||
| 139 | return; | ||||
| 140 | } | ||||
| 141 | |||||
| 142 | sub validate { | ||||
| 143 | my ($self, $value) = @_; | ||||
| 144 | my $message; | ||||
| 145 | foreach my $type (@{$self->type_constraints}) { | ||||
| 146 | my $err = $type->validate($value); | ||||
| 147 | return unless defined $err; | ||||
| 148 | $message .= ($message ? ' and ' : '') . $err | ||||
| 149 | if defined $err; | ||||
| 150 | } | ||||
| 151 | return ($message . ' in (' . $self->name . ')') ; | ||||
| 152 | } | ||||
| 153 | |||||
| 154 | sub find_type_for { | ||||
| 155 | my ($self, $value) = @_; | ||||
| 156 | |||||
| 157 | return first { $_->check($value) } @{ $self->type_constraints }; | ||||
| 158 | } | ||||
| 159 | |||||
| 160 | sub is_a_type_of { | ||||
| 161 | my ($self, $type_name) = @_; | ||||
| 162 | |||||
| 163 | return all { $_->is_a_type_of($type_name) } @{ $self->type_constraints }; | ||||
| 164 | } | ||||
| 165 | |||||
| 166 | sub is_subtype_of { | ||||
| 167 | my ($self, $type_name) = @_; | ||||
| 168 | |||||
| 169 | return all { $_->is_subtype_of($type_name) } @{ $self->type_constraints }; | ||||
| 170 | } | ||||
| 171 | |||||
| 172 | sub create_child_type { | ||||
| 173 | my ( $self, %opts ) = @_; | ||||
| 174 | |||||
| 175 | my $constraint | ||||
| 176 | = Moose::Meta::TypeConstraint->new( %opts, parent => $self ); | ||||
| 177 | |||||
| 178 | # if we have a type constraint union, and no | ||||
| 179 | # type check, this means we are just aliasing | ||||
| 180 | # the union constraint, which means we need to | ||||
| 181 | # handle this differently. | ||||
| 182 | # - SL | ||||
| 183 | if ( not( defined $opts{constraint} ) | ||||
| 184 | && $self->has_coercion ) { | ||||
| 185 | $constraint->coercion( | ||||
| 186 | Moose::Meta::TypeCoercion::Union->new( | ||||
| 187 | type_constraint => $self, | ||||
| 188 | ) | ||||
| 189 | ); | ||||
| 190 | } | ||||
| 191 | |||||
| 192 | return $constraint; | ||||
| 193 | } | ||||
| 194 | |||||
| 195 | 1 | 4µs | 1; | ||
| 196 | |||||
| 197 | # ABSTRACT: A union of Moose type constraints | ||||
| 198 | |||||
| 199 | __END__ | ||||
# spent 2µs within Moose::Meta::TypeConstraint::Union::CORE:sort which was called 2 times, avg 1µs/call:
# 2 times (2µs+0s) by Moose::Meta::TypeConstraint::Union::new at line 24, avg 1µs/call |