Skip to content

Commit

Permalink
Squeeze a bit more performance out of calc_pe_central_lists
Browse files Browse the repository at this point in the history
Avoid creation of data where possible.
  • Loading branch information
shawnlaffan committed Jun 18, 2016
1 parent 653b255 commit 64da31a
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/Biodiverse/Indices/Phylogenetic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -634,32 +634,33 @@ sub calc_pe_central_lists {
my $c_list = $args{PHYLO_C_LIST}; # those only in nbr set 2
my $a_list = $args{PHYLO_A_LIST};
my $b_list = $args{PHYLO_B_LIST};
my (%wt_list, %local_range_list_c, %global_range_list_c);

my $local_range_list = $args{PE_LOCAL_RANGELIST};
my $global_range_list = $args{PE_RANGELIST};

my %results;

# avoid copies and slices if there are no nodes found only in nbr set 2
if (scalar keys %$c_list) {
my (%wt_list, %local_range_list_c, %global_range_list_c);
# Keep any node found in nbr set 1
@wt_list{keys %$b_list} = @{$base_wt_list}{keys %$b_list};
@wt_list{keys %$a_list} = @{$base_wt_list}{keys %$a_list};

@wt_list{keys %$b_list} = @{$base_wt_list}{keys %$b_list};

my @keepers = keys %wt_list;
@local_range_list_c{@keepers} = @{$local_range_list}{@keepers};
@global_range_list_c{@keepers} = @{$global_range_list}{@keepers};

$results{PEC_WTLIST} = \%wt_list;
$results{PEC_LOCAL_RANGELIST} = \%local_range_list_c;
$results{PEC_RANGELIST} = \%global_range_list_c;
}
else {
%wt_list = %$base_wt_list;
%local_range_list_c = %$local_range_list;
%global_range_list_c = %$global_range_list;
$results{PEC_WTLIST} = $base_wt_list;
$results{PEC_LOCAL_RANGELIST} = $local_range_list;
$results{PEC_RANGELIST} = $global_range_list;
}

my %results = (
PEC_WTLIST => \%wt_list,
PEC_LOCAL_RANGELIST => \%local_range_list_c,
PEC_RANGELIST => \%global_range_list_c,
);

return wantarray ? %results : \%results;
}
Expand Down

0 comments on commit 64da31a

Please sign in to comment.