Skip to content

Commit

Permalink
Optimisations for recursive checking
Browse files Browse the repository at this point in the history
With a lot of dependencies, the recursive checking code was taking a
long time to run. This replaces grepping arrays with hash searches, and
also moves the check outside of a loop it was unneessarily within.
  • Loading branch information
abeverley committed Oct 26, 2023
1 parent 5193d40 commit 6317e6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
29 changes: 13 additions & 16 deletions lib/GADS/Column/Curval.pm
Original file line number Diff line number Diff line change
Expand Up @@ -155,33 +155,30 @@ sub _build_subvals_input_required
my @cols = @{$self->filter->columns_in_subs};
foreach my $col (@cols)
{
$deps{$self->id} ||= [];
push @{$deps{$self->id}}, $col->id;
$deps{$self->id} ||= {};
$deps{$self->id}->{$col->id} = 1;
foreach my $cc (@{$col->depends_on})
{
$deps{$col->id} ||= [];
push @{$deps{$col->id}}, $cc;
$deps{$col->id} ||= {};
$deps{$col->id}->{$cc} = 1;
push @cols, $self->layout->column($cc);
}
foreach my $disp ($self->schema->resultset('DisplayField')->search({
layout_id => $col->id
})->all)
{
$deps{$col->id} ||= [];
push @{$deps{$col->id}}, $disp->display_field_id;
$deps{$col->id} ||= {};
$deps{$col->id}->{$disp->display_field_id} = 1;
push @cols, $self->layout->column($disp->display_field_id);
}
}

my %seen;

# Check for recursive dependencies to prevent hanging in a loop
foreach my $key (keys %deps)
{
my $ret = $self->layout->check_recursive(\%deps, $key);
error __x"Unable to produce list of fields required for filtered field \"{field}\": calculated value or display condition recursive dependencies: {path}",
field => $self->name, path => $ret if $ret;
}

# Check for recursive dependencies to prevent hanging in a loop
foreach my $key (keys %deps)
{
my $ret = $self->layout->check_recursive(\%deps, $key);
error __x"Unable to produce list of fields required for filtered field \"{field}\": calculated value or display condition recursive dependencies: {path}",
field => $self->name, path => $ret if $ret;
}

# Calc values do not need written to by user
Expand Down
9 changes: 6 additions & 3 deletions lib/GADS/Layout.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1206,11 +1206,12 @@ sub check_recursive
# always match
my @path2 = @$path;
my @search = @path2[0..$#path2-1];
if (grep $_ == $key, @search)
my %search = map { $_ => 1 } @search;
if ($search{$key})
{
return join "", map $self->column($_)->name, @$path;
}
foreach my $val (@{$deps->{$key}})
foreach my $val (keys %{$deps->{$key}})
{
# Push onto path
push @$path, $val;
Expand All @@ -1234,7 +1235,9 @@ sub _order_dependencies
my %deps = map {
my $id = $_->id;
$_->id => # Allow fields depend on theirselves, but remove from dependencies to prevent recursion
[grep $_ != $id, @{$_->has_display_field ? $_->display_field_col_ids : $_->depends_on}],
{
map { $_ => 1 } grep $_ != $id, @{$_->has_display_field ? $_->display_field_col_ids : $_->depends_on},
}
} @columns;

my %seen;
Expand Down

0 comments on commit 6317e6d

Please sign in to comment.