← Index
NYTProf Performance Profile   « line view »
For ../prof.pl
  Run on Thu Dec 15 15:23:56 2022
Reported on Thu Dec 15 15:27:02 2022

Filename/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Type/Coercion.pm
StatementsExecuted 1368 statements in 3.36ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
5833454µs6.21msType::Coercion::::newType::Coercion::new
5811260µs5.44msType::Coercion::::_preserve_type_constraintType::Coercion::_preserve_type_constraint
6122126µs153µsType::Coercion::::is_anonType::Coercion::is_anon
564375µs75µsType::Coercion::::freezeType::Coercion::freeze
22263µs529µsType::Coercion::::add_type_coercionsType::Coercion::add_type_coercions
11149µs490µsType::Coercion::::_build_compiled_coercionType::Coercion::_build_compiled_coercion
522132µs32µsType::Coercion::::__ANON__[:28]Type::Coercion::__ANON__[:28]
11132µs120µsType::Coercion::::inline_coercionType::Coercion::inline_coercion
565231µs31µsType::Coercion::::type_coercion_mapType::Coercion::type_coercion_map
704230µs30µsType::Coercion::::nameType::Coercion::name
22124µs163µsType::Coercion::::can_be_inlinedType::Coercion::can_be_inlined
11123µs23µsType::Coercion::::BEGIN@3Type::Coercion::BEGIN@3
3119µs12µsType::Coercion::::qualified_nameType::Coercion::qualified_name
6319µs11µsType::Coercion::::type_constraintType::Coercion::type_constraint
5318µs8µsType::Coercion::::CORE:matchType::Coercion::CORE:match (opcode)
3217µs14µsType::Coercion::::has_type_constraintType::Coercion::has_type_constraint
6437µs7µsType::Coercion::::frozenType::Coercion::frozen
1116µs31µsType::Coercion::::BEGIN@15Type::Coercion::BEGIN@15
1116µs496µsType::Coercion::::compiled_coercionType::Coercion::compiled_coercion
3116µs8µsType::Coercion::::is_parameterizableType::Coercion::is_parameterizable
1115µs8µsType::Coercion::::AUTOLOADType::Coercion::AUTOLOAD
1115µs7µsType::Coercion::::BEGIN@4Type::Coercion::BEGIN@4
1115µs7µsType::Coercion::::__ANON__[:27]Type::Coercion::__ANON__[:27]
1114µs27µsType::Coercion::::BEGIN@5Type::Coercion::BEGIN@5
1113µs3µsType::Coercion::::BEGIN@14Type::Coercion::BEGIN@14
2113µs3µsType::Coercion::::_clear_compiled_coercionType::Coercion::_clear_compiled_coercion
1112µs2µsType::Coercion::::BEGIN@7Type::Coercion::BEGIN@7
3112µs2µsType::Coercion::::has_coercion_generatorType::Coercion::has_coercion_generator
3112µs2µsType::Coercion::::has_libraryType::Coercion::has_library
1111µs1µsType::Coercion::::BEGIN@16Type::Coercion::BEGIN@16
1111µs1µsType::Coercion::::display_nameType::Coercion::display_name
0000s0sType::Coercion::::__ANON__[:262]Type::Coercion::__ANON__[:262]
0000s0sType::Coercion::::__ANON__[:33]Type::Coercion::__ANON__[:33]
0000s0sType::Coercion::::__ANON__[:46]Type::Coercion::__ANON__[:46]
0000s0sType::Coercion::::__ANON__[:500]Type::Coercion::__ANON__[:500]
0000s0sType::Coercion::::_build_display_nameType::Coercion::_build_display_name
0000s0sType::Coercion::::_build_moose_coercionType::Coercion::_build_moose_coercion
0000s0sType::Coercion::::_codelike_type_coercion_mapType::Coercion::_codelike_type_coercion_map
0000s0sType::Coercion::::_compiled_type_coercionType::Coercion::_compiled_type_coercion
0000s0sType::Coercion::::_croakType::Coercion::_croak
0000s0sType::Coercion::::_maybe_restore_type_constraintType::Coercion::_maybe_restore_type_constraint
0000s0sType::Coercion::::_overload_coderefType::Coercion::_overload_coderef
0000s0sType::Coercion::::_reparameterizeType::Coercion::_reparameterize
0000s0sType::Coercion::::_source_type_unionType::Coercion::_source_type_union
0000s0sType::Coercion::::_stringify_no_magicType::Coercion::_stringify_no_magic
0000s0sType::Coercion::::addType::Coercion::add
0000s0sType::Coercion::::assert_coerceType::Coercion::assert_coerce
0000s0sType::Coercion::::canType::Coercion::can
0000s0sType::Coercion::::coerceType::Coercion::coerce
0000s0sType::Coercion::::coercion_generatorType::Coercion::coercion_generator
0000s0sType::Coercion::::has_coercion_for_typeType::Coercion::has_coercion_for_type
0000s0sType::Coercion::::has_coercion_for_valueType::Coercion::has_coercion_for_value
0000s0sType::Coercion::::has_parametersType::Coercion::has_parameters
0000s0sType::Coercion::::i_really_want_to_unfreezeType::Coercion::i_really_want_to_unfreeze
0000s0sType::Coercion::::is_parameterizedType::Coercion::is_parameterized
0000s0sType::Coercion::::isaType::Coercion::isa
0000s0sType::Coercion::::libraryType::Coercion::library
0000s0sType::Coercion::::metaType::Coercion::meta
0000s0sType::Coercion::::moose_coercionType::Coercion::moose_coercion
0000s0sType::Coercion::::parameterizeType::Coercion::parameterize
0000s0sType::Coercion::::parameterized_fromType::Coercion::parameterized_from
0000s0sType::Coercion::::parametersType::Coercion::parameters
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Type::Coercion;
2
3234µs123µs
# spent 23µs within Type::Coercion::BEGIN@3 which was called: # once (23µs+0s) by Type::Tiny::_build_coercion at line 3
use 5.008001;
# spent 23µs making 1 call to Type::Coercion::BEGIN@3
4217µs29µs
# spent 7µs (5+2) within Type::Coercion::BEGIN@4 which was called: # once (5µs+2µs) by Type::Tiny::_build_coercion at line 4
use strict;
# spent 7µs making 1 call to Type::Coercion::BEGIN@4 # spent 2µs making 1 call to strict::import
5225µs250µs
# spent 27µs (4+23) within Type::Coercion::BEGIN@5 which was called: # once (4µs+23µs) by Type::Tiny::_build_coercion at line 5
use warnings;
# spent 27µs making 1 call to Type::Coercion::BEGIN@5 # spent 23µs making 1 call to warnings::import
6
7
# spent 2µs within Type::Coercion::BEGIN@7 which was called: # once (2µs+0s) by Type::Tiny::_build_coercion at line 10
BEGIN {
811µs $Type::Coercion::AUTHORITY = 'cpan:TOBYINK';
912µs $Type::Coercion::VERSION = '2.000001';
10125µs12µs}
# spent 2µs making 1 call to Type::Coercion::BEGIN@7
11
1211µs$Type::Coercion::VERSION =~ tr/_//d;
13
14214µs13µs
# spent 3µs within Type::Coercion::BEGIN@14 which was called: # once (3µs+0s) by Type::Tiny::_build_coercion at line 14
use Eval::TypeTiny qw<>;
# spent 3µs making 1 call to Type::Coercion::BEGIN@14
15214µs256µs
# spent 31µs (6+25) within Type::Coercion::BEGIN@15 which was called: # once (6µs+25µs) by Type::Tiny::_build_coercion at line 15
use Scalar::Util qw< blessed >;
# spent 31µs making 1 call to Type::Coercion::BEGIN@15 # spent 25µs making 1 call to Exporter::import
1621.86ms11µs
# spent 1µs within Type::Coercion::BEGIN@16 which was called: # once (1µs+0s) by Type::Tiny::_build_coercion at line 16
use Types::TypeTiny qw<>;
# spent 1µs making 1 call to Type::Coercion::BEGIN@16
17
18sub _croak ($;@) { require Error::TypeTiny; goto \&Error::TypeTiny::croak }
19
2011µsrequire Type::Tiny;
21
22__PACKAGE__->Type::Tiny::_install_overloads(
23
# spent 7µs (5+2) within Type::Coercion::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Type/Coercion.pm:27] which was called: # once (5µs+2µs) by Type::Coercion::_build_compiled_coercion at line 265
q("") => sub {
2417µs22µs caller =~ m{^(Moo::HandleMoose|Sub::Quote)}
# spent 1µs making 1 call to Type::Coercion::CORE:match # spent 1µs making 1 call to Type::Coercion::display_name
25 ? $_[0]->_stringify_no_magic
26 : $_[0]->display_name;
27 },
285251µs
# spent 32µs within Type::Coercion::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Type/Coercion.pm:28] which was called 52 times, avg 615ns/call: # 48 times (29µs+0s) by Type::Tiny::has_coercion at line 472 of Type/Tiny.pm, avg 604ns/call # 4 times (3µs+0s) by Type::Tiny::coercion at line 439 of Type/Tiny.pm, avg 750ns/call
q(bool) => sub { 1 },
2914µs147µs q(&{}) => "_overload_coderef",
# spent 47µs making 1 call to Type::Tiny::_install_overloads
30);
31
32__PACKAGE__->Type::Tiny::_install_overloads(
33 q(~~) => sub { $_[0]->has_coercion_for_value( $_[1] ) },
3411µs17µs) if Type::Tiny::SUPPORT_SMARTMATCH();
# spent 7µs making 1 call to Type::Tiny::_install_overloads
35
36sub _overload_coderef {
37 my $self = shift;
38
39 if ( "Sub::Quote"->can( "quote_sub" ) && $self->can_be_inlined ) {
40 $self->{_overload_coderef} =
41 Sub::Quote::quote_sub( $self->inline_coercion( '$_[0]' ) )
42 if !$self->{_overload_coderef} || !$self->{_sub_quoted}++;
43 }
44 else {
45 Scalar::Util::weaken( my $weak = $self );
46 $self->{_overload_coderef} ||= sub { $weak->coerce( @_ ) };
47 }
48
49 $self->{_overload_coderef};
50} #/ sub _overload_coderef
51
52
# spent 6.21ms (454µs+5.75) within Type::Coercion::new which was called 58 times, avg 107µs/call: # 54 times (408µs+5.59ms) by Type::Tiny::_build_coercion at line 503 of Type/Tiny.pm, avg 111µs/call # 3 times (41µs+153µs) by Type::Library::add_coercion at line 274 of Type/Library.pm, avg 65µs/call # once (5µs+9µs) by Types::Standard::Dict::__coercion_generator at line 251 of Types/Standard/Dict.pm
sub new {
535810µs my $class = shift;
545837µs my %params = ( @_ == 1 ) ? %{ $_[0] } : @_;
55
565817µs $params{name} = '__ANON__' unless exists( $params{name} );
575815µs my $C = delete( $params{type_coercion_map} ) || [];
585811µs my $F = delete( $params{frozen} );
59
605818µs my $self = bless \%params, $class;
61588µs1121µs $self->add_type_coercions( @$C ) if @$C;
# spent 121µs making 1 call to Type::Coercion::add_type_coercions
625847µs585.44ms $self->_preserve_type_constraint;
# spent 5.44ms making 58 calls to Type::Coercion::_preserve_type_constraint, avg 94µs/call
6358110µs5837µs Scalar::Util::weaken( $self->{type_constraint} ); # break ref cycle
# spent 37µs making 58 calls to Scalar::Util::weaken, avg 638ns/call
64586µs $self->{frozen} = $F if $F;
65
665855µs58147µs unless ( $self->is_anon ) {
# spent 147µs making 58 calls to Type::Coercion::is_anon, avg 3µs/call
67
68 # First try a fast ASCII-only expression, but fall back to Unicode
69311µs65µs $self->name =~ /^_{0,2}[A-Z][A-Za-z0-9_]+$/sm
# spent 4µs making 3 calls to Type::Coercion::CORE:match, avg 1µs/call # spent 1µs making 3 calls to Type::Coercion::name, avg 333ns/call
70 or eval q( use 5.008; $self->name =~ /^_{0,2}\p{Lu}[\p{L}0-9_]+$/sm )
71 or _croak '"%s" is not a valid coercion name', $self->name;
72 }
73
7458114µs return $self;
75} #/ sub new
76
77sub _stringify_no_magic {
78 sprintf(
79 '%s=%s(0x%08x)', blessed( $_[0] ), Scalar::Util::reftype( $_[0] ),
80 Scalar::Util::refaddr( $_[0] )
81 );
82}
83
847081µs
# spent 30µs within Type::Coercion::name which was called 70 times, avg 429ns/call: # 61 times (27µs+0s) by Type::Coercion::is_anon at line 176, avg 443ns/call # 3 times (1µs+0s) by Type::Coercion::new at line 69, avg 333ns/call # 3 times (1µs+0s) by Type::Coercion::qualified_name at line 171, avg 333ns/call # 3 times (1µs+0s) by Type::Library::add_coercion at line 275 of Type/Library.pm, avg 333ns/call
sub name { $_[0]{name} }
8513µs
# spent 1µs within Type::Coercion::display_name which was called: # once (1µs+0s) by Type::Coercion::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Type/Coercion.pm:27] at line 24
sub display_name { $_[0]{display_name} ||= $_[0]->_build_display_name }
86sub library { $_[0]{library} }
87
88
# spent 11µs (9+2) within Type::Coercion::type_constraint which was called 6 times, avg 2µs/call: # 3 times (6µs+1000ns) by Type::Coercion::has_type_constraint at line 103, avg 2µs/call # 2 times (1µs+1µs) by Type::Coercion::can_be_inlined at line 318, avg 1µs/call # once (2µs+0s) by Type::Coercion::inline_coercion at line 361
sub type_constraint {
89612µs62µs $_[0]{type_constraint} ||= $_[0]->_maybe_restore_type_constraint;
# spent 2µs making 6 calls to Type::Tiny::__ANON__[Type/Tiny.pm:101], avg 333ns/call
90}
915664µs
# spent 31µs within Type::Coercion::type_coercion_map which was called 56 times, avg 554ns/call: # 48 times (26µs+0s) by Type::Tiny::has_coercion at line 472 of Type/Tiny.pm, avg 542ns/call # 4 times (3µs+0s) by Type::Coercion::add_type_coercions at line 250, avg 750ns/call # 2 times (2µs+0s) by Type::Coercion::can_be_inlined at line 321, avg 1µs/call # once (0s+0s) by Type::Coercion::_build_compiled_coercion at line 261 # once (0s+0s) by Type::Coercion::inline_coercion at line 352
sub type_coercion_map { $_[0]{type_coercion_map} ||= [] }
92sub moose_coercion { $_[0]{moose_coercion} ||= $_[0]->_build_moose_coercion }
93
94
# spent 496µs (6+490) within Type::Coercion::compiled_coercion which was called: # once (6µs+490µs) by Type::Tiny::exportables at line 1220 of Type/Tiny.pm
sub compiled_coercion {
9516µs1490µs $_[0]{compiled_coercion} ||= $_[0]->_build_compiled_coercion;
# spent 490µs making 1 call to Type::Coercion::_build_compiled_coercion
96}
97620µs
# spent 7µs within Type::Coercion::frozen which was called 6 times, avg 1µs/call: # 2 times (2µs+0s) by Type::Coercion::can_be_inlined at line 315, avg 1µs/call # 2 times (2µs+0s) by Type::Coercion::add_type_coercions at line 234, avg 1µs/call # once (2µs+0s) by Type::Library::make_immutable at line 319 of Type/Library.pm # once (1µs+0s) by Type::Tiny::exportables at line 1220 of Type/Tiny.pm
sub frozen { $_[0]{frozen} ||= 0 }
98sub coercion_generator { $_[0]{coercion_generator} }
99sub parameters { $_[0]{parameters} }
100sub parameterized_from { $_[0]{parameterized_from} }
101
10234µs
# spent 2µs within Type::Coercion::has_library which was called 3 times, avg 667ns/call: # 3 times (2µs+0s) by Type::Coercion::qualified_name at line 167, avg 667ns/call
sub has_library { exists $_[0]{library} }
10336µs37µs
# spent 14µs (7+7) within Type::Coercion::has_type_constraint which was called 3 times, avg 5µs/call: # 2 times (6µs+6µs) by Type::Coercion::can_be_inlined at line 318, avg 6µs/call # once (1µs+1µs) by Type::Coercion::inline_coercion at line 360
sub has_type_constraint { defined $_[0]->type_constraint } # sic
# spent 7µs making 3 calls to Type::Coercion::type_constraint, avg 2µs/call
10434µs
# spent 2µs within Type::Coercion::has_coercion_generator which was called 3 times, avg 667ns/call: # 3 times (2µs+0s) by Type::Coercion::is_parameterizable at line 426, avg 667ns/call
sub has_coercion_generator { exists $_[0]{coercion_generator} }
105sub has_parameters { exists $_[0]{parameters} }
106
107
# spent 5.44ms (260µs+5.18) within Type::Coercion::_preserve_type_constraint which was called 58 times, avg 94µs/call: # 58 times (260µs+5.18ms) by Type::Coercion::new at line 62, avg 94µs/call
sub _preserve_type_constraint {
108583µs my $self = shift;
109 $self->{_compiled_type_constraint_check} =
110 $self->{type_constraint}->compiled_check
11158221µs1165.18ms if $self->{type_constraint};
# spent 5.14ms making 58 calls to Type::Tiny::compiled_check, avg 89µs/call # spent 39µs making 58 calls to Type::Tiny::__ANON__[Type/Tiny.pm:101], avg 672ns/call
112}
113
114sub _maybe_restore_type_constraint {
115 my $self = shift;
116 if ( my $check = $self->{_compiled_type_constraint_check} ) {
117 return Type::Tiny->new( constraint => $check );
118 }
119 return; # uncoverable statement
120}
121
122sub add {
123 my $class = shift;
124 my ( $x, $y, $swap ) = @_;
125
126 Types::TypeTiny::is_TypeTiny( $x ) and return $x->plus_fallback_coercions( $y );
127 Types::TypeTiny::is_TypeTiny( $y ) and return $y->plus_coercions( $x );
128
129 _croak "Attempt to add $class to something that is not a $class"
130 unless blessed( $x )
131 && blessed( $y )
132 && $x->isa( $class )
133 && $y->isa( $class );
134
135 ( $y, $x ) = ( $x, $y ) if $swap;
136
137 my %opts;
138 if ( $x->has_type_constraint
139 and $y->has_type_constraint
140 and $x->type_constraint == $y->type_constraint )
141 {
142 $opts{type_constraint} = $x->type_constraint;
143 }
144 elsif ( $x->has_type_constraint and $y->has_type_constraint ) {
145
146 # require Type::Tiny::Union;
147 # $opts{type_constraint} = "Type::Tiny::Union"->new(
148 # type_constraints => [ $x->type_constraint, $y->type_constraint ],
149 # );
150 }
151 $opts{display_name} ||= "$x+$y";
152 delete $opts{display_name} if $opts{display_name} eq '__ANON__+__ANON__';
153
154 my $new = $class->new( %opts );
155 $new->add_type_coercions( @{ $x->type_coercion_map } );
156 $new->add_type_coercions( @{ $y->type_coercion_map } );
157 return $new;
158} #/ sub add
159
160sub _build_display_name {
161 shift->name;
162}
163
164
# spent 12µs (9+3) within Type::Coercion::qualified_name which was called 3 times, avg 4µs/call: # 3 times (9µs+3µs) by Eval::TypeTiny::type_to_coderef at line 169 of Eval/TypeTiny.pm, avg 4µs/call
sub qualified_name {
16530s my $self = shift;
166
16733µs32µs if ( $self->has_library and not $self->is_anon ) {
# spent 2µs making 3 calls to Type::Coercion::has_library, avg 667ns/call
168 return sprintf( "%s::%s", $self->library, $self->name );
169 }
170
17134µs31µs return $self->name;
# spent 1µs making 3 calls to Type::Coercion::name, avg 333ns/call
172}
173
174
# spent 153µs (126+27) within Type::Coercion::is_anon which was called 61 times, avg 3µs/call: # 58 times (122µs+25µs) by Type::Coercion::new at line 66, avg 3µs/call # 3 times (4µs+2µs) by Type::Library::add_coercion at line 279 of Type/Library.pm, avg 2µs/call
sub is_anon {
1756112µs my $self = shift;
17661137µs6127µs $self->name eq "__ANON__";
# spent 27µs making 61 calls to Type::Coercion::name, avg 443ns/call
177}
178
179
# spent 3µs within Type::Coercion::_clear_compiled_coercion which was called 2 times, avg 2µs/call: # 2 times (3µs+0s) by Type::Coercion::add_type_coercions at line 254, avg 2µs/call
sub _clear_compiled_coercion {
18021µs delete $_[0]{_overload_coderef};
18124µs delete $_[0]{compiled_coercion};
182}
183
184112107µs
# spent 75µs within Type::Coercion::freeze which was called 56 times, avg 1µs/call: # 37 times (58µs+0s) by Type::Library::make_immutable at line 318 of Type/Library.pm, avg 2µs/call # 14 times (15µs+0s) by Type::Tiny::parameterize at line 1082 of Type/Tiny.pm, avg 1µs/call # 3 times (2µs+0s) by Type::Tiny::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Type/Tiny.pm:1073] at line 1072 of Type/Tiny.pm, avg 667ns/call # 2 times (0s+0s) by Types::Standard::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Types/Standard.pm:732] at line 731 of Types/Standard.pm, avg 0s/call
sub freeze { $_[0]{frozen} = 1; $_[0] }
185sub i_really_want_to_unfreeze { $_[0]{frozen} = 0; $_[0] }
186
187sub coerce {
188 my $self = shift;
189 return $self->compiled_coercion->( @_ );
190}
191
192sub assert_coerce {
193 my $self = shift;
194 my $r = $self->coerce( @_ );
195 $self->type_constraint->assert_valid( $r )
196 if $self->has_type_constraint;
197 return $r;
198}
199
200sub has_coercion_for_type {
201 my $self = shift;
202 my $type = Types::TypeTiny::to_TypeTiny( $_[0] );
203
204 return "0 but true"
205 if $self->has_type_constraint
206 && $type->is_a_type_of( $self->type_constraint );
207
208 my $c = $self->type_coercion_map;
209 for ( my $i = 0 ; $i <= $#$c ; $i += 2 ) {
210 return !!1 if $type->is_a_type_of( $c->[$i] );
211 }
212 return;
213} #/ sub has_coercion_for_type
214
215sub has_coercion_for_value {
216 my $self = shift;
217 local $_ = $_[0];
218
219 return "0 but true"
220 if $self->has_type_constraint
221 && $self->type_constraint->check( @_ );
222
223 my $c = $self->type_coercion_map;
224 for ( my $i = 0 ; $i <= $#$c ; $i += 2 ) {
225 return !!1 if $c->[$i]->check( @_ );
226 }
227 return;
228} #/ sub has_coercion_for_value
229
230
# spent 529µs (63+466) within Type::Coercion::add_type_coercions which was called 2 times, avg 264µs/call: # once (30µs+378µs) by JSON::Schema::Modern::BEGIN@31 at line 234 of Types/Standard.pm # once (33µs+88µs) by Type::Coercion::new at line 61
sub add_type_coercions {
23121µs my $self = shift;
23222µs my @args = @_;
233
23424µs22µs _croak "Attempt to add coercion code to a Type::Coercion which has been frozen"
# spent 2µs making 2 calls to Type::Coercion::frozen, avg 1µs/call
235 if $self->frozen;
236
23720s while ( @args ) {
23845µs416µs my $type = Types::TypeTiny::to_TypeTiny( shift @args );
# spent 16µs making 4 calls to Types::TypeTiny::to_TypeTiny, avg 4µs/call
239
240411µs834µs if ( blessed $type and my $method = $type->can( 'type_coercion_map' ) ) {
# spent 31µs making 4 calls to Type::Tiny::can, avg 8µs/call # spent 3µs making 4 calls to Scalar::Util::blessed, avg 750ns/call
241 push @{ $self->type_coercion_map }, @{ $method->( $type ) };
242 }
243 else {
24441µs my $coercion = shift @args;
24543µs4227µs _croak "Types must be blessed Type::Tiny objects"
# spent 227µs making 4 calls to Types::TypeTiny::is_TypeTiny, avg 57µs/call
246 unless Types::TypeTiny::is_TypeTiny( $type );
24745µs4181µs _croak "Coercions must be code references or strings"
# spent 181µs making 4 calls to Types::TypeTiny::is_StringLike, avg 45µs/call
248 unless Types::TypeTiny::is_StringLike( $coercion )
249 || Types::TypeTiny::is_CodeLike( $coercion );
25046µs43µs push @{ $self->type_coercion_map }, $type, $coercion;
# spent 3µs making 4 calls to Type::Coercion::type_coercion_map, avg 750ns/call
251 }
252 } #/ while ( @args )
253
25422µs23µs $self->_clear_compiled_coercion;
# spent 3µs making 2 calls to Type::Coercion::_clear_compiled_coercion, avg 2µs/call
25524µs return $self;
256} #/ sub add_type_coercions
257
258
# spent 490µs (49+441) within Type::Coercion::_build_compiled_coercion which was called: # once (49µs+441µs) by Type::Coercion::compiled_coercion at line 95
sub _build_compiled_coercion {
25910s my $self = shift;
260
26112µs10s my @mishmash = @{ $self->type_coercion_map };
# spent 0s making 1 call to Type::Coercion::type_coercion_map
262 return sub { $_[0] }
26311µs unless @mishmash;
264
265112µs4441µs if ( $self->can_be_inlined ) {
# spent 199µs making 1 call to Eval::TypeTiny::eval_closure # spent 120µs making 1 call to Type::Coercion::inline_coercion # spent 115µs making 1 call to Type::Coercion::can_be_inlined # spent 7µs making 1 call to Type::Coercion::__ANON__[Type/Coercion.pm:27]
266 return Eval::TypeTiny::eval_closure(
267 source => sprintf( 'sub ($) { %s }', $self->inline_coercion( '$_[0]' ) ),
268 description => sprintf( "compiled coercion '%s'", $self ),
269 );
270 }
271
272 # These arrays will be closed over.
273 my ( @types, @codes );
274 while ( @mishmash ) {
275 push @types, shift @mishmash;
276 push @codes, shift @mishmash;
277 }
278 if ( $self->has_type_constraint ) {
279 unshift @types, $self->type_constraint;
280 unshift @codes, undef;
281 }
282
283 my @sub;
284
285 for my $i ( 0 .. $#types ) {
286 push @sub,
287 $types[$i]->can_be_inlined
288 ? sprintf( 'if (%s)', $types[$i]->inline_check( '$_[0]' ) )
289 : sprintf( 'if ($checks[%d]->(@_))', $i );
290 push @sub,
291 !defined( $codes[$i] )
292 ? sprintf( ' { return $_[0] }' )
293 : Types::TypeTiny::is_StringLike( $codes[$i] ) ? sprintf(
294 ' { local $_ = $_[0]; return scalar(%s); }',
295 $codes[$i]
296 )
297 : sprintf( ' { local $_ = $_[0]; return scalar($codes[%d]->(@_)) }', $i );
298 } #/ for my $i ( 0 .. $#types)
299
300 push @sub, 'return $_[0];';
301
302 return Eval::TypeTiny::eval_closure(
303 source => sprintf( 'sub ($) { %s }', join qq[\n], @sub ),
304 description => sprintf( "compiled coercion '%s'", $self ),
305 environment => {
306 '@checks' => [ map $_->compiled_check, @types ],
307 '@codes' => \@codes,
308 },
309 );
310} #/ sub _build_compiled_coercion
311
312
# spent 163µs (24+139) within Type::Coercion::can_be_inlined which was called 2 times, avg 82µs/call: # once (20µs+95µs) by Type::Coercion::_build_compiled_coercion at line 265 # once (4µs+44µs) by Type::Coercion::inline_coercion at line 350
sub can_be_inlined {
31320s my $self = shift;
314
31522µs22µs return unless $self->frozen;
# spent 2µs making 2 calls to Type::Coercion::frozen, avg 1µs/call
316
317 return
31824µs633µs if $self->has_type_constraint
# spent 19µs making 2 calls to Type::Tiny::can_be_inlined, avg 10µs/call # spent 12µs making 2 calls to Type::Coercion::has_type_constraint, avg 6µs/call # spent 2µs making 2 calls to Type::Coercion::type_constraint, avg 1µs/call
319 && !$self->type_constraint->can_be_inlined;
320
32121µs22µs my @mishmash = @{ $self->type_coercion_map };
# spent 2µs making 2 calls to Type::Coercion::type_coercion_map, avg 1µs/call
32221µs while ( @mishmash ) {
32321µs my ( $type, $converter ) = splice( @mishmash, 0, 2 );
32421µs225µs return unless $type->can_be_inlined;
# spent 25µs making 2 calls to Type::Tiny::can_be_inlined, avg 12µs/call
32523µs277µs return unless Types::TypeTiny::is_StringLike( $converter );
# spent 77µs making 2 calls to Types::TypeTiny::is_StringLike, avg 38µs/call
326 }
32725µs return !!1;
328} #/ sub can_be_inlined
329
330sub _source_type_union {
331 my $self = shift;
332
333 my @r;
334 push @r, $self->type_constraint if $self->has_type_constraint;
335
336 my @mishmash = @{ $self->type_coercion_map };
337 while ( @mishmash ) {
338 my ( $type ) = splice( @mishmash, 0, 2 );
339 push @r, $type;
340 }
341
342 require Type::Tiny::Union;
343 return "Type::Tiny::Union"->new( type_constraints => \@r, tmp => 1 );
344} #/ sub _source_type_union
345
346
# spent 120µs (32+88) within Type::Coercion::inline_coercion which was called: # once (32µs+88µs) by Type::Coercion::_build_compiled_coercion at line 265
sub inline_coercion {
34710s my $self = shift;
34811µs my $varname = $_[0];
349
35011µs148µs _croak "This coercion cannot be inlined" unless $self->can_be_inlined;
# spent 48µs making 1 call to Type::Coercion::can_be_inlined
351
35211µs10s my @mishmash = @{ $self->type_coercion_map };
# spent 0s making 1 call to Type::Coercion::type_coercion_map
35311µs return "($varname)" unless @mishmash;
354
35510s my ( @types, @codes );
35610s while ( @mishmash ) {
35710s push @types, shift @mishmash;
35811µs push @codes, shift @mishmash;
359 }
36012µs12µs if ( $self->has_type_constraint ) {
# spent 2µs making 1 call to Type::Coercion::has_type_constraint
36110s12µs unshift @types, $self->type_constraint;
# spent 2µs making 1 call to Type::Coercion::type_constraint
36211µs unshift @codes, undef;
363 }
364
36511µs my @sub;
366
36711µs for my $i ( 0 .. $#types ) {
36825µs236µs push @sub, sprintf( '(%s) ?', $types[$i]->inline_check( $varname ) );
# spent 36µs making 2 calls to Type::Tiny::inline_check, avg 18µs/call
36924µs push @sub,
370 ( defined( $codes[$i] ) && ( $varname eq '$_' ) )
371 ? sprintf( 'scalar(do { %s }) :', $codes[$i] )
372 : defined( $codes[$i] ) ? sprintf(
373 'scalar(do { local $_ = %s; %s }) :', $varname,
374 $codes[$i]
375 )
376 : sprintf( '%s :', $varname );
377 } #/ for my $i ( 0 .. $#types)
378
37911µs push @sub, "$varname";
380
381132µs "@sub";
382} #/ sub inline_coercion
383
384sub _build_moose_coercion {
385 my $self = shift;
386
387 my %options = ();
388 $options{type_coercion_map} =
389 [ $self->freeze->_codelike_type_coercion_map( 'moose_type' ) ];
390 $options{type_constraint} = $self->type_constraint
391 if $self->has_type_constraint;
392
393 require Moose::Meta::TypeCoercion;
394 my $r = "Moose::Meta::TypeCoercion"->new( %options );
395
396 return $r;
397} #/ sub _build_moose_coercion
398
399sub _codelike_type_coercion_map {
400 my $self = shift;
401 my $modifier = $_[0];
402
403 my @orig = @{ $self->type_coercion_map };
404 my @new;
405
406 while ( @orig ) {
407 my ( $type, $converter ) = splice( @orig, 0, 2 );
408
409 push @new, $modifier ? $type->$modifier : $type;
410
411 if ( Types::TypeTiny::is_CodeLike( $converter ) ) {
412 push @new, $converter;
413 }
414 else {
415 push @new, Eval::TypeTiny::eval_closure(
416 source => sprintf( 'sub { local $_ = $_[0]; %s }', $converter ),
417 description => sprintf( "temporary compiled converter from '%s'", $type ),
418 );
419 }
420 } #/ while ( @orig )
421
422 return @new;
423} #/ sub _codelike_type_coercion_map
424
425
# spent 8µs (6+2) within Type::Coercion::is_parameterizable which was called 3 times, avg 3µs/call: # 3 times (6µs+2µs) by Eval::TypeTiny::type_to_coderef at line 149 of Eval/TypeTiny.pm, avg 3µs/call
sub is_parameterizable {
42637µs32µs shift->has_coercion_generator;
# spent 2µs making 3 calls to Type::Coercion::has_coercion_generator, avg 667ns/call
427}
428
429sub is_parameterized {
430 shift->has_parameters;
431}
432
433sub parameterize {
434 my $self = shift;
435 return $self unless @_;
436 $self->is_parameterizable
437 or _croak "Constraint '%s' does not accept parameters", "$self";
438
439 @_ = map Types::TypeTiny::to_TypeTiny( $_ ), @_;
440
441 return ref( $self )->new(
442 type_constraint => $self->type_constraint,
443 type_coercion_map =>
444 [ $self->coercion_generator->( $self, $self->type_constraint, @_ ) ],
445 parameters => \@_,
446 frozen => 1,
447 parameterized_from => $self,
448 );
449} #/ sub parameterize
450
451sub _reparameterize {
452 my $self = shift;
453 my ( $target_type ) = @_;
454
455 $self->is_parameterized or return $self;
456 my $parent = $self->parameterized_from;
457
458 return ref( $self )->new(
459 type_constraint => $target_type,
460 type_coercion_map => [
461 $parent->coercion_generator->( $parent, $target_type, @{ $self->parameters } )
462 ],
463 parameters => \@_,
464 frozen => 1,
465 parameterized_from => $parent,
466 );
467} #/ sub _reparameterize
468
469sub isa {
470 my $self = shift;
471
472 if ( $INC{"Moose.pm"}
473 and blessed( $self )
474 and $_[0] eq 'Moose::Meta::TypeCoercion' )
475 {
476 return !!1;
477 }
478
479 if ( $INC{"Moose.pm"}
480 and blessed( $self )
481 and $_[0] =~ /^(Class::MOP|MooseX?)::/ )
482 {
483 my $r = $self->moose_coercion->isa( @_ );
484 return $r if $r;
485 }
486
487 $self->SUPER::isa( @_ );
488} #/ sub isa
489
490sub can {
491 my $self = shift;
492
493 my $can = $self->SUPER::can( @_ );
494 return $can if $can;
495
496 if ( $INC{"Moose.pm"}
497 and blessed( $self )
498 and my $method = $self->moose_coercion->can( @_ ) )
499 {
500 return sub { $method->( shift->moose_coercion, @_ ) };
501 }
502
503 return;
504} #/ sub can
505
506
# spent 8µs (5+3) within Type::Coercion::AUTOLOAD which was called: # once (5µs+3µs) by Types::Standard::Dict::__coercion_generator at line 267 of Types/Standard/Dict.pm
sub AUTOLOAD {
50710s my $self = shift;
50815µs13µs my ( $m ) = ( our $AUTOLOAD =~ /::(\w+)$/ );
# spent 3µs making 1 call to Type::Coercion::CORE:match
50913µs return if $m eq 'DESTROY';
510
511 if ( $INC{"Moose.pm"}
512 and blessed( $self )
513 and my $method = $self->moose_coercion->can( $m ) )
514 {
515 return $method->( $self->moose_coercion, @_ );
516 }
517
518 _croak q[Can't locate object method "%s" via package "%s"], $m,
519 ref( $self ) || $self;
520} #/ sub AUTOLOAD
521
522# Private Moose method, but Moo uses this...
523sub _compiled_type_coercion {
524 my $self = shift;
525 if ( @_ ) {
526 my $thing = $_[0];
527 if ( blessed( $thing ) and $thing->isa( "Type::Coercion" ) ) {
528 $self->add_type_coercions( @{ $thing->type_coercion_map } );
529 }
530 elsif ( Types::TypeTiny::is_CodeLike( $thing ) ) {
531 require Types::Standard;
532 $self->add_type_coercions( Types::Standard::Any(), $thing );
533 }
534 } #/ if ( @_ )
535 $self->compiled_coercion;
536} #/ sub _compiled_type_coercion
537
53810s*compile_type_coercion = \&compiled_coercion;
539sub meta { _croak( "Not really a Moose::Meta::TypeCoercion. Sorry!" ) }
540
54115µs1;
542
543__END__
 
# spent 8µs within Type::Coercion::CORE:match which was called 5 times, avg 2µs/call: # 3 times (4µs+0s) by Type::Coercion::new at line 69, avg 1µs/call # once (3µs+0s) by Type::Coercion::AUTOLOAD at line 508 # once (1µs+0s) by Type::Coercion::__ANON__[/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/Type/Coercion.pm:27] at line 24
sub Type::Coercion::CORE:match; # opcode