Most users of the foreach_rule helper are agnostic to the iteration
order of the rules and as sorting can become a non-neglible overhead for
larger rule sets, make sorting rules optional.

The only caller dependent on a sorted rule set is the rules' index API
endpoint.

Signed-off-by: Daniel Kral <d.k...@proxmox.com>
---
 src/PVE/API2/HA/Rules.pm |  1 +
 src/PVE/HA/Rules.pm      | 10 ++++++----
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/PVE/API2/HA/Rules.pm b/src/PVE/API2/HA/Rules.pm
index ab431019..fb8b465b 100644
--- a/src/PVE/API2/HA/Rules.pm
+++ b/src/PVE/API2/HA/Rules.pm
@@ -194,6 +194,7 @@ __PACKAGE__->register_method({
             },
             type => $type,
             sid => $resource,
+            sorted => 1,
         );
 
         return $res;
diff --git a/src/PVE/HA/Rules.pm b/src/PVE/HA/Rules.pm
index e13769a1..784f2d36 100644
--- a/src/PVE/HA/Rules.pm
+++ b/src/PVE/HA/Rules.pm
@@ -428,7 +428,7 @@ Filters the given C<$rules> according to the C<$opts> and 
loops over the
 resulting rules in the order as defined in the section config and executes
 C<$func> with the parameters C<L<< ($rule, $ruleid) >>>.
 
-The following key-value pairs for C<$opts> as filter properties are:
+The following key-value pairs for C<$opts> are:
 
 =over
 
@@ -438,6 +438,8 @@ The following key-value pairs for C<$opts> as filter 
properties are:
 
 =item C<$exclude_disabled_rules>: Limits C<$rules> to those which are enabled.
 
+=item C<$sorted>: Sorts C<$rules> according to C<< $rules->{order} >>.
+
 =back
 
 =cut
@@ -447,9 +449,9 @@ sub foreach_rule : prototype($$;%) {
 
     my $sid = $opts{sid};
 
-    my @ruleids = sort {
-        $rules->{order}->{$a} <=> $rules->{order}->{$b}
-    } keys %{ $rules->{ids} };
+    my @ruleids = keys $rules->{ids}->%*;
+    @ruleids = sort { $rules->{order}->{$a} <=> $rules->{order}->{$b} } 
@ruleids
+        if defined($opts{sorted});
 
     for my $ruleid (@ruleids) {
         my $rule = $rules->{ids}->{$ruleid};
-- 
2.47.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to