Skip to content

Commit 946a642

Browse files
committed
Minor optimizations for Type::Params
1 parent f5a98f5 commit 946a642

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/Type/Params/Parameter.pm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ sub _make_code {
354354
elsif ( $constraint->{uniq} == Any->{uniq} ) {
355355
$coderef->add_line( '1; # ... nothing to do' );
356356
}
357+
elsif ( $args{is_slurpy} and $self->_dont_validate_slurpy ) {
358+
$coderef->add_line( '1; # ... nothing to do' );
359+
}
357360
elsif ( $constraint->can_be_inlined ) {
358361
$coderef->add_line( $strictness_test . sprintf(
359362
"%s\n\tor %s;",
@@ -428,6 +431,28 @@ sub _make_code {
428431
$self;
429432
}
430433

434+
# This list can be reused safely.
435+
my @uniqs;
436+
437+
# If $SLURPY is one of a handful of very loose type constraints, there is
438+
# no need to validate it because we built it as a hashref or arrayref ourself,
439+
# so there's no way it couldn't be a hashref or arrayref.
440+
sub _dont_validate_slurpy {
441+
my $self = shift;
442+
my $type = $self->type or return 1;
443+
if ( not @uniqs ) {
444+
@uniqs = map { $_->{uniq} }
445+
Slurpy,
446+
Slurpy[Any], Any,
447+
Slurpy[Item], Item,
448+
Slurpy[Ref], Ref,
449+
Slurpy[HashRef], HashRef,
450+
Slurpy[ArrayRef], ArrayRef;
451+
}
452+
( $_ == $type->{uniq} and return 1 ) for @uniqs;
453+
return 0;
454+
}
455+
431456
1;
432457

433458
__END__

lib/Type/Params/Signature.pm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ sub _coderef_slurpy {
775775
input_slot => '$SLURPY',
776776
display_var => '$SLURPY',
777777
index => 0,
778+
is_slurpy => 1,
778779
$self->is_named
779780
? ( output_slot => sprintf( '$out{%s}', B::perlstring( $parameter->name ) ) )
780781
: ( output_var => '@out' )
@@ -829,7 +830,7 @@ sub _coderef_end {
829830
}
830831

831832
$self->_coderef_end_extra( $coderef );
832-
$coderef->add_line( $self->_make_return_expression( is_early => 0 ) . ';' );
833+
$coderef->add_line( $self->_make_return_expression( is_early => 0, allow_full_statements => 1 ) . ';' );
833834
$coderef->{indent} =~ s/\t$//;
834835
$coderef->add_line( '}' );
835836

@@ -892,9 +893,13 @@ sub _make_return_expression {
892893
elsif ( $list eq '@_' ) {
893894
return sprintf 'goto( $__NEXT__ )';
894895
}
896+
elsif ( $args{allow_full_statements} and not ( $args{is_early} or not exists $args{is_early} ) ) {
897+
# We are allowed to return full statements, not
898+
# forced to use do {...} to make an expression.
899+
return sprintf '@_ = ( %s ); goto $__NEXT__', $list;
900+
}
895901
else {
896-
return sprintf 'do { @_ = ( %s ); goto $__NEXT__ }',
897-
$list;
902+
return sprintf 'do { @_ = ( %s ); goto $__NEXT__ }', $list;
898903
}
899904
}
900905
elsif ( $args{is_early} or not exists $args{is_early} ) {

0 commit comments

Comments
 (0)