diff --git a/lib/Biodiverse/BaseStruct.pm b/lib/Biodiverse/BaseStruct.pm index 66208cf6b..3449e1e90 100644 --- a/lib/Biodiverse/BaseStruct.pm +++ b/lib/Biodiverse/BaseStruct.pm @@ -362,18 +362,17 @@ sub get_element_hash { sub get_element_name_as_array_aa { my ($self, $element, $csv_object) = @_; - # caching saves a little time for large data sets - # but needs to be shared with a "parent" object to make a difference - # e.g. a spatial object copies from a groups object + my $arr = $self->{ELEMENTS}{$element}{_ELEMENT_ARRAY}; + return wantarray ? @$arr : $arr + if $arr; + state $el_list_ref_cache_name = '_ELEMENT_ARRAY_REF_CACHE'; my $element_list_ref_cache = $self->get_cached_value_dor_set_default_href ($el_list_ref_cache_name); - $self->{ELEMENTS}{$element}{_ELEMENT_ARRAY} = $element_list_ref_cache->{$element}; + $arr = $self->{ELEMENTS}{$element}{_ELEMENT_ARRAY} = $element_list_ref_cache->{$element}; - return wantarray - ? @{$element_list_ref_cache->{$element}} - : $element_list_ref_cache->{$element} - if $element_list_ref_cache->{$element}; + return wantarray ? @$arr : $arr + if $arr; # package level cache state $_el_array_cache = {};