Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomisations: Refactor and cache constant labels #952

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rand: Cache constant labels parsed from string
Will save a few cycles for larger sets.
  • Loading branch information
shawnlaffan committed Nov 12, 2024
commit e1b51b9e89d2f3d4be3d769753568d7d5034c02a
8 changes: 7 additions & 1 deletion lib/Biodiverse/Randomise.pm
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,11 @@ sub _parse_labels_not_to_randomise {

return if !$constant_labels;

if (!ref $constant_labels) {
state $cache_key = 'CONSTANT_LABELS_ARRAY';
if (my $cache = $self->get_cached_value ($cache_key)) {
$constant_labels = $cache;
}
elsif (!is_ref $constant_labels) {
$constant_labels = [split /[\r\n]+/, $constant_labels];
# Maybe we were passed a list of key value pairs
# This can happen with pasting from GUI popups
Expand All @@ -802,6 +806,8 @@ sub _parse_labels_not_to_randomise {
. join ' ', @$constant_labels[0 .. $n];
}

$self->set_cached_value ($cache_key => $constant_labels);

return wantarray ? @$constant_labels : $constant_labels;
}

Expand Down