From 41e4b4a9d55e41cc9e7e70129cb6f3b453be10cf Mon Sep 17 00:00:00 2001 From: shawnlaffan Date: Sat, 23 Sep 2023 10:57:23 +1000 Subject: [PATCH] Matrices: cache the summary stats Need to take care when updating that the cache is cleared, but generally the summary stats are called after the matrix has been finalised. --- lib/Biodiverse/Matrix.pm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/Biodiverse/Matrix.pm b/lib/Biodiverse/Matrix.pm index e8b082086..72e2b3574 100644 --- a/lib/Biodiverse/Matrix.pm +++ b/lib/Biodiverse/Matrix.pm @@ -391,6 +391,12 @@ sub get_max_value { sub get_summary_stats { my $self = shift; + state $cachename = 'SUMMARY_STATS'; + my $cached = $self->get_cached_value ($cachename); + + return wantarray ? %$cached : $cached + if $cached; + my %data; \my %top_level = $self->{BYELEMENT}; foreach my $href (values %top_level) { @@ -415,6 +421,8 @@ sub get_summary_stats { $r{$key} = $self->round_to_precision_aa($r{$key}, PRECISION) + 0; } + $self->set_cached_value($cachename => \%r); + return wantarray ? %r : \%r; }