← Index
NYTProf Performance Profile   « line view »
For /home/ss5/perl5/perlbrew/perls/perl-5.22.0/bin/benchmarkanything-storage
  Run on Mon Jan 29 16:55:34 2018
Reported on Mon Jan 29 16:57:07 2018

Filename/home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/x86_64-linux/Moose/Meta/TypeConstraint/Union.pm
StatementsExecuted 16 statements in 772µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
111311µs1.01msMoose::Meta::TypeConstraint::Union::::BEGIN@8Moose::Meta::TypeConstraint::Union::BEGIN@8
11116µs21µsMoose::Meta::TypeConstraint::Union::::BEGIN@4Moose::Meta::TypeConstraint::Union::BEGIN@4
11114µs60µsMoose::Meta::TypeConstraint::Union::::BEGIN@10Moose::Meta::TypeConstraint::Union::BEGIN@10
1118µs17µsMoose::Meta::TypeConstraint::Union::::BEGIN@5Moose::Meta::TypeConstraint::Union::BEGIN@5
1117µs567µsMoose::Meta::TypeConstraint::Union::::BEGIN@6Moose::Meta::TypeConstraint::Union::BEGIN@6
1116µs23µsMoose::Meta::TypeConstraint::Union::::BEGIN@12Moose::Meta::TypeConstraint::Union::BEGIN@12
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:136]Moose::Meta::TypeConstraint::Union::__ANON__[:136]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:157]Moose::Meta::TypeConstraint::Union::__ANON__[:157]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:163]Moose::Meta::TypeConstraint::Union::__ANON__[:163]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:169]Moose::Meta::TypeConstraint::Union::__ANON__[:169]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:16]Moose::Meta::TypeConstraint::Union::__ANON__[:16]
0000s0sMoose::Meta::TypeConstraint::Union::::__ANON__[:71]Moose::Meta::TypeConstraint::Union::__ANON__[:71]
0000s0sMoose::Meta::TypeConstraint::Union::::_actually_compile_type_constraintMoose::Meta::TypeConstraint::Union::_actually_compile_type_constraint
0000s0sMoose::Meta::TypeConstraint::Union::::_inline_checkMoose::Meta::TypeConstraint::Union::_inline_check
0000s0sMoose::Meta::TypeConstraint::Union::::can_be_inlinedMoose::Meta::TypeConstraint::Union::can_be_inlined
0000s0sMoose::Meta::TypeConstraint::Union::::coercionMoose::Meta::TypeConstraint::Union::coercion
0000s0sMoose::Meta::TypeConstraint::Union::::create_child_typeMoose::Meta::TypeConstraint::Union::create_child_type
0000s0sMoose::Meta::TypeConstraint::Union::::equalsMoose::Meta::TypeConstraint::Union::equals
0000s0sMoose::Meta::TypeConstraint::Union::::find_type_forMoose::Meta::TypeConstraint::Union::find_type_for
0000s0sMoose::Meta::TypeConstraint::Union::::has_coercionMoose::Meta::TypeConstraint::Union::has_coercion
0000s0sMoose::Meta::TypeConstraint::Union::::inline_environmentMoose::Meta::TypeConstraint::Union::inline_environment
0000s0sMoose::Meta::TypeConstraint::Union::::is_a_type_ofMoose::Meta::TypeConstraint::Union::is_a_type_of
0000s0sMoose::Meta::TypeConstraint::Union::::is_subtype_ofMoose::Meta::TypeConstraint::Union::is_subtype_of
0000s0sMoose::Meta::TypeConstraint::Union::::newMoose::Meta::TypeConstraint::Union::new
0000s0sMoose::Meta::TypeConstraint::Union::::parentMoose::Meta::TypeConstraint::Union::parent
0000s0sMoose::Meta::TypeConstraint::Union::::validateMoose::Meta::TypeConstraint::Union::validate
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Moose::Meta::TypeConstraint::Union;
21400nsour $VERSION = '2.1605';
3
4217µs226µs
# spent 21µs (16+5) within Moose::Meta::TypeConstraint::Union::BEGIN@4 which was called: # once (16µs+5µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 4
use strict;
# spent 21µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@4 # spent 5µs making 1 call to strict::import
5218µs226µs
# spent 17µs (8+9) within Moose::Meta::TypeConstraint::Union::BEGIN@5 which was called: # once (8µs+9µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 5
use warnings;
# spent 17µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@5 # spent 9µs making 1 call to warnings::import
6235µs21.13ms
# spent 567µs (7+560) within Moose::Meta::TypeConstraint::Union::BEGIN@6 which was called: # once (7µs+560µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 6
use metaclass;
# spent 567µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@6 # spent 560µs making 1 call to metaclass::import
7
82106µs11.01ms
# spent 1.01ms (311µs+697µs) within Moose::Meta::TypeConstraint::Union::BEGIN@8 which was called: # once (311µs+697µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 8
use Moose::Meta::TypeCoercion::Union;
# spent 1.01ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@8
9
10337µs383µs
# spent 60µs (14+46) within Moose::Meta::TypeConstraint::Union::BEGIN@10 which was called: # once (14µs+46µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 10
use List::Util 1.33 qw(first all);
# spent 60µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@10 # spent 12µs making 1 call to UNIVERSAL::VERSION # spent 10µs making 1 call to List::Util::import
11
122547µs241µs
# spent 23µs (6+18) within Moose::Meta::TypeConstraint::Union::BEGIN@12 which was called: # once (6µs+18µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 12
use parent 'Moose::Meta::TypeConstraint';
# spent 23µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@12 # spent 18µs making 1 call to parent::import
13
14__PACKAGE__->meta->add_attribute('type_constraints' => (
15 accessor => 'type_constraints',
16 default => sub { [] },
1718µs3402µs Class::MOP::_definition_context(),
# spent 372µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute # spent 20µs making 1 call to Moose::Meta::TypeConstraint::Union::meta # spent 9µs making 1 call to Class::MOP::_definition_context
18));
19
20sub new {
21 my ($class, %options) = @_;
22
23 my $name = join '|' => sort { $a cmp $b }
24 map { $_->name } @{ $options{type_constraints} };
25
26 my $self = $class->SUPER::new(
27 name => $name,
28 %options,
29 );
30
31 $self->_set_constraint( $self->_compiled_type_constraint );
32
33 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.
40sub coercion {
41 my $self = shift;
42
43 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 if ( grep { $_->has_coercion } @{ $self->type_constraints } ) {
48 return $self->{coercion} = Moose::Meta::TypeCoercion::Union->new(
49 type_constraint => $self );
50 }
51 else {
52 return $self->{coercion} = undef;
53 }
54}
55
56sub has_coercion {
57 return defined $_[0]->coercion;
58}
59
60sub _actually_compile_type_constraint {
61 my $self = shift;
62
63 my @constraints = @{ $self->type_constraints };
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 };
72}
73
74sub can_be_inlined {
75 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 for my $tc ( @{ $self->type_constraints } ) {
80 return 0 unless $tc->can_be_inlined;
81 }
82
83 return 1;
84}
85
86sub _inline_check {
87 my $self = shift;
88 my $val = shift;
89
90 return '('
91 . (
92 join ' || ', map { '(' . $_->_inline_check($val) . ')' }
93 @{ $self->type_constraints }
94 )
95 . ')';
96}
97
98sub inline_environment {
99 my $self = shift;
100
101 return { map { %{ $_->inline_environment } }
102 @{ $self->type_constraints } };
103}
104
105sub 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
130sub 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
142sub 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
154sub find_type_for {
155 my ($self, $value) = @_;
156
157 return first { $_->check($value) } @{ $self->type_constraints };
158}
159
160sub is_a_type_of {
161 my ($self, $type_name) = @_;
162
163 return all { $_->is_a_type_of($type_name) } @{ $self->type_constraints };
164}
165
166sub is_subtype_of {
167 my ($self, $type_name) = @_;
168
169 return all { $_->is_subtype_of($type_name) } @{ $self->type_constraints };
170}
171
172sub 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
19514µs1;
196
197# ABSTRACT: A union of Moose type constraints
198
199__END__