| Filename | /home/doy/coding/src/Moose/blib/lib//Moose/Meta/TypeConstraint/Union.pm |
| Statements | Executed 86 statements in 4.99ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 1.61ms | 5.16ms | Moose::Meta::TypeConstraint::Union::BEGIN@8 |
| 6 | 1 | 1 | 930µs | 3.58ms | Moose::Meta::TypeConstraint::Union::new |
| 6 | 1 | 1 | 259µs | 341µs | Moose::Meta::TypeConstraint::Union::_actually_compile_type_constraint |
| 1 | 1 | 1 | 125µs | 154µs | Moose::Meta::TypeConstraint::Union::BEGIN@4 |
| 1 | 1 | 1 | 61µs | 157µs | Moose::Meta::TypeConstraint::Union::BEGIN@5 |
| 1 | 1 | 1 | 50µs | 228µs | Moose::Meta::TypeConstraint::Union::BEGIN@10 |
| 1 | 1 | 1 | 49µs | 3.95ms | Moose::Meta::TypeConstraint::Union::BEGIN@6 |
| 6 | 1 | 1 | 42µs | 42µs | Moose::Meta::TypeConstraint::Union::CORE:sort (opcode) |
| 1 | 1 | 1 | 40µs | 283µs | Moose::Meta::TypeConstraint::Union::BEGIN@16 |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:123] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:20] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:34] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::__ANON__[:74] |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::coercion |
| 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::has_coercion |
| 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::parents |
| 0 | 0 | 0 | 0s | 0s | Moose::Meta::TypeConstraint::Union::validate |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | package Moose::Meta::TypeConstraint::Union; | ||||
| 3 | |||||
| 4 | 3 | 167µs | 2 | 182µs | # spent 154µs (125+28) within Moose::Meta::TypeConstraint::Union::BEGIN@4 which was called:
# once (125µs+28µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 4 # spent 154µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@4
# spent 28µs making 1 call to strict::import |
| 5 | 3 | 159µs | 2 | 254µs | # spent 157µs (61+97) within Moose::Meta::TypeConstraint::Union::BEGIN@5 which was called:
# once (61µs+97µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 5 # spent 157µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@5
# spent 96µs making 1 call to warnings::import |
| 6 | 3 | 141µs | 2 | 7.84ms | # spent 3.95ms (49µs+3.90) within Moose::Meta::TypeConstraint::Union::BEGIN@6 which was called:
# once (49µs+3.90ms) by Moose::Util::TypeConstraints::BEGIN@30 at line 6 # spent 3.95ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@6
# spent 3.90ms making 1 call to metaclass::import |
| 7 | |||||
| 8 | 3 | 500µs | 1 | 5.16ms | # spent 5.16ms (1.61+3.55) within Moose::Meta::TypeConstraint::Union::BEGIN@8 which was called:
# once (1.61ms+3.55ms) by Moose::Util::TypeConstraints::BEGIN@30 at line 8 # spent 5.16ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@8 |
| 9 | |||||
| 10 | 3 | 230µs | 2 | 406µs | # spent 228µs (50+178) within Moose::Meta::TypeConstraint::Union::BEGIN@10 which was called:
# once (50µs+178µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 10 # spent 228µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@10
# spent 178µs making 1 call to Exporter::import |
| 11 | |||||
| 12 | 1 | 5µs | our $VERSION = '1.19'; | ||
| 13 | 1 | 60µs | $VERSION = eval $VERSION; # spent 12µs executing statements in string eval | ||
| 14 | 1 | 4µs | our $AUTHORITY = 'cpan:STEVAN'; | ||
| 15 | |||||
| 16 | 3 | 2.53ms | 2 | 526µs | # spent 283µs (40+243) within Moose::Meta::TypeConstraint::Union::BEGIN@16 which was called:
# once (40µs+243µs) by Moose::Util::TypeConstraints::BEGIN@30 at line 16 # spent 283µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@16
# spent 243µs making 1 call to base::import |
| 17 | |||||
| 18 | __PACKAGE__->meta->add_attribute('type_constraints' => ( | ||||
| 19 | accessor => 'type_constraints', | ||||
| 20 | default => sub { [] } | ||||
| 21 | 1 | 33µs | 2 | 2.15ms | )); # spent 2.01ms making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 147µs making 1 call to Moose::Meta::TypeConstraint::Union::meta |
| 22 | |||||
| 23 | # spent 3.58ms (930µs+2.65) within Moose::Meta::TypeConstraint::Union::new which was called 6 times, avg 597µs/call:
# 6 times (930µs+2.65ms) by Moose::Util::TypeConstraints::create_type_constraint_union at line 95 of Moose/Util/TypeConstraints.pm, avg 597µs/call | ||||
| 24 | 6 | 47µs | my ($class, %options) = @_; | ||
| 25 | |||||
| 26 | 15 | 161µs | 15 | 195µs | my $name = join '|' => sort { $a cmp $b } # spent 195µs making 15 calls to Moose::Meta::TypeConstraint::name, avg 13µs/call |
| 27 | 6 | 196µs | 6 | 42µs | map { $_->name } @{ $options{type_constraints} }; # spent 42µs making 6 calls to Moose::Meta::TypeConstraint::Union::CORE:sort, avg 7µs/call |
| 28 | |||||
| 29 | 6 | 284µs | 6 | 2.35ms | my $self = $class->SUPER::new( # spent 2.35ms making 6 calls to Moose::Meta::TypeConstraint::new, avg 391µs/call |
| 30 | name => $name, | ||||
| 31 | %options, | ||||
| 32 | ); | ||||
| 33 | |||||
| 34 | 6 | 115µs | 6 | 70µs | $self->_set_constraint(sub { $self->check($_[0]) }); # spent 70µs making 6 calls to Moose::Meta::TypeConstraint::_set_constraint, avg 12µs/call |
| 35 | |||||
| 36 | 6 | 80µs | return $self; | ||
| 37 | } | ||||
| 38 | |||||
| 39 | # XXX - this is a rather gross implementation of laziness for the benefit of | ||||
| 40 | # MX::Types. If we try to call ->has_coercion on the objects during object | ||||
| 41 | # construction, this does not work when defining a recursive constraint with | ||||
| 42 | # MX::Types. | ||||
| 43 | sub coercion { | ||||
| 44 | my $self = shift; | ||||
| 45 | |||||
| 46 | return $self->{coercion} if exists $self->{coercion}; | ||||
| 47 | |||||
| 48 | # Using any instead of grep here causes a weird error with some corner | ||||
| 49 | # cases when MX::Types is in use. See RT #61001. | ||||
| 50 | if ( grep { $_->has_coercion } @{ $self->type_constraints } ) { | ||||
| 51 | return $self->{coercion} = Moose::Meta::TypeCoercion::Union->new( | ||||
| 52 | type_constraint => $self ); | ||||
| 53 | } | ||||
| 54 | else { | ||||
| 55 | return $self->{coercion} = undef; | ||||
| 56 | } | ||||
| 57 | } | ||||
| 58 | |||||
| 59 | sub has_coercion { | ||||
| 60 | return defined $_[0]->coercion; | ||||
| 61 | } | ||||
| 62 | |||||
| 63 | # spent 341µs (259+82) within Moose::Meta::TypeConstraint::Union::_actually_compile_type_constraint which was called 6 times, avg 57µs/call:
# 6 times (259µs+82µs) by Moose::Meta::TypeConstraint::compile_type_constraint at line 202 of Moose/Meta/TypeConstraint.pm, avg 57µs/call | ||||
| 64 | 6 | 18µs | my $self = shift; | ||
| 65 | |||||
| 66 | 6 | 95µs | 6 | 82µs | my @constraints = @{ $self->type_constraints }; # spent 82µs making 6 calls to Moose::Meta::TypeConstraint::Union::type_constraints, avg 14µs/call |
| 67 | |||||
| 68 | return sub { | ||||
| 69 | my $value = shift; | ||||
| 70 | foreach my $type (@constraints) { | ||||
| 71 | return 1 if $type->check($value); | ||||
| 72 | } | ||||
| 73 | return undef; | ||||
| 74 | 6 | 141µs | }; | ||
| 75 | } | ||||
| 76 | |||||
| 77 | |||||
| 78 | sub equals { | ||||
| 79 | my ( $self, $type_or_name ) = @_; | ||||
| 80 | |||||
| 81 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); | ||||
| 82 | |||||
| 83 | return unless $other->isa(__PACKAGE__); | ||||
| 84 | |||||
| 85 | my @self_constraints = @{ $self->type_constraints }; | ||||
| 86 | my @other_constraints = @{ $other->type_constraints }; | ||||
| 87 | |||||
| 88 | return unless @self_constraints == @other_constraints; | ||||
| 89 | |||||
| 90 | # FIXME presort type constraints for efficiency? | ||||
| 91 | constraint: foreach my $constraint ( @self_constraints ) { | ||||
| 92 | for ( my $i = 0; $i < @other_constraints; $i++ ) { | ||||
| 93 | if ( $constraint->equals($other_constraints[$i]) ) { | ||||
| 94 | splice @other_constraints, $i, 1; | ||||
| 95 | next constraint; | ||||
| 96 | } | ||||
| 97 | } | ||||
| 98 | } | ||||
| 99 | |||||
| 100 | return @other_constraints == 0; | ||||
| 101 | } | ||||
| 102 | |||||
| 103 | sub parents { | ||||
| 104 | my $self = shift; | ||||
| 105 | $self->type_constraints; | ||||
| 106 | } | ||||
| 107 | |||||
| 108 | sub validate { | ||||
| 109 | my ($self, $value) = @_; | ||||
| 110 | my $message; | ||||
| 111 | foreach my $type (@{$self->type_constraints}) { | ||||
| 112 | my $err = $type->validate($value); | ||||
| 113 | return unless defined $err; | ||||
| 114 | $message .= ($message ? ' and ' : '') . $err | ||||
| 115 | if defined $err; | ||||
| 116 | } | ||||
| 117 | return ($message . ' in (' . $self->name . ')') ; | ||||
| 118 | } | ||||
| 119 | |||||
| 120 | sub find_type_for { | ||||
| 121 | my ($self, $value) = @_; | ||||
| 122 | |||||
| 123 | return first { $_->check($value) } @{ $self->type_constraints }; | ||||
| 124 | } | ||||
| 125 | |||||
| 126 | sub is_a_type_of { | ||||
| 127 | my ($self, $type_name) = @_; | ||||
| 128 | foreach my $type (@{$self->type_constraints}) { | ||||
| 129 | return 1 if $type->is_a_type_of($type_name); | ||||
| 130 | } | ||||
| 131 | return 0; | ||||
| 132 | } | ||||
| 133 | |||||
| 134 | sub is_subtype_of { | ||||
| 135 | my ($self, $type_name) = @_; | ||||
| 136 | foreach my $type (@{$self->type_constraints}) { | ||||
| 137 | return 1 if $type->is_subtype_of($type_name); | ||||
| 138 | } | ||||
| 139 | return 0; | ||||
| 140 | } | ||||
| 141 | |||||
| 142 | sub create_child_type { | ||||
| 143 | my ( $self, %opts ) = @_; | ||||
| 144 | |||||
| 145 | my $constraint | ||||
| 146 | = Moose::Meta::TypeConstraint->new( %opts, parent => $self ); | ||||
| 147 | |||||
| 148 | # if we have a type constraint union, and no | ||||
| 149 | # type check, this means we are just aliasing | ||||
| 150 | # the union constraint, which means we need to | ||||
| 151 | # handle this differently. | ||||
| 152 | # - SL | ||||
| 153 | if ( not( defined $opts{constraint} ) | ||||
| 154 | && $self->has_coercion ) { | ||||
| 155 | $constraint->coercion( | ||||
| 156 | Moose::Meta::TypeCoercion::Union->new( | ||||
| 157 | type_constraint => $self, | ||||
| 158 | ) | ||||
| 159 | ); | ||||
| 160 | } | ||||
| 161 | |||||
| 162 | return $constraint; | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | 1 | 24µs | 1; | ||
| 166 | |||||
| 167 | __END__ | ||||
# spent 42µs within Moose::Meta::TypeConstraint::Union::CORE:sort which was called 6 times, avg 7µs/call:
# 6 times (42µs+0s) by Moose::Meta::TypeConstraint::Union::new at line 27, avg 7µs/call |