Skip to content

Commit

Permalink
get_element_name_as_array_aa: use a package level cache
Browse files Browse the repository at this point in the history
This allows savings for randomisations that
generate many different objects, all with the
same set of elements.
  • Loading branch information
shawnlaffan committed Feb 13, 2024
1 parent 3184526 commit 50f5ab5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/Biodiverse/BaseStruct.pm
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,21 @@ sub get_element_name_as_array_aa {
: $element_list_ref_cache->{$element}
if $element_list_ref_cache->{$element};

# package level cache
state $_el_array_cache = {};
my $element_list_ref = $_el_array_cache->{$element};

if ($element_list_ref) {
# work with a copy of the package array but cache the copy on $self
my $copy = [ @$element_list_ref ];
$self->{ELEMENTS}{$element}{_ELEMENT_ARRAY}
= $element_list_ref_cache->{$element}
= $copy;
return wantarray ? @$copy : $copy;
}

my $quote_char = $self->get_param('QUOTES');
my $element_list_ref = $self->csv2list(
$element_list_ref = $self->csv2list(
string => $element,
sep_char => $self->get_param('JOIN_CHAR'),
quote_char => $quote_char,
Expand All @@ -392,10 +405,13 @@ sub get_element_name_as_array_aa {
}
}

$_el_array_cache->{$element} = $element_list_ref;
my $copy = [@$element_list_ref];

$self->{ELEMENTS}{$element}{_ELEMENT_ARRAY}
= $element_list_ref_cache->{$element}
= $element_list_ref;
return wantarray ? @$element_list_ref : $element_list_ref;
= $copy; # work with a copy
return wantarray ? @$copy : $copy;
}

sub get_element_name_as_array {
Expand Down

0 comments on commit 50f5ab5

Please sign in to comment.