]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
intervals: Do not sort cached set elements over and over again
authorPhil Sutter <phil@nwl.cc>
Thu, 16 Jun 2022 08:56:12 +0000 (10:56 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sat, 18 Jun 2022 22:55:20 +0000 (00:55 +0200)
When adding element(s) to a non-empty set, code merged the two lists and
sorted the result. With many individual 'add element' commands this
causes substantial overhead. Make use of the fact that
existing_set->init is sorted already, sort only the list of new elements
and use list_splice_sorted() to merge the two sorted lists.

Add set_sort_splice() and use it for set element overlap detection and
automerge.

A test case adding ~25k elements in individual commands completes in
about 1/4th of the time with this patch applied.

Joint work with Pablo.

Fixes: 3da9643fb9ff9 ("intervals: add support to automerge with kernel elements")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/expression.h
src/intervals.c
src/mergesort.c

index 53194c9248e4b3866f9c2b348f35ffa60f1418b9..cf7319b65e0e17c2d1ddd78542045adc947f1be7 100644 (file)
@@ -481,6 +481,7 @@ extern struct expr *compound_expr_alloc(const struct location *loc,
 extern void compound_expr_add(struct expr *compound, struct expr *expr);
 extern void compound_expr_remove(struct expr *compound, struct expr *expr);
 extern void list_expr_sort(struct list_head *head);
+extern void list_splice_sorted(struct list_head *list, struct list_head *head);
 
 extern struct expr *concat_expr_alloc(const struct location *loc);
 
index e20341320da2ff8117ddaa9327b59bb0d82de842..dcc06d18d594cf9c97e4345bdd855d1a61813b2d 100644 (file)
@@ -118,6 +118,26 @@ static bool merge_ranges(struct set_automerge_ctx *ctx,
        return false;
 }
 
+static void set_sort_splice(struct expr *init, struct set *set)
+{
+       struct set *existing_set = set->existing_set;
+
+       set_to_range(init);
+       list_expr_sort(&init->expressions);
+
+       if (!existing_set)
+               return;
+
+       if (existing_set->init) {
+               set_to_range(existing_set->init);
+               list_splice_sorted(&existing_set->init->expressions,
+                                  &init->expressions);
+               init_list_head(&existing_set->init->expressions);
+       } else {
+               existing_set->init = set_expr_alloc(&internal_location, set);
+       }
+}
+
 static void setelem_automerge(struct set_automerge_ctx *ctx)
 {
        struct expr *i, *next, *prev = NULL;
@@ -222,18 +242,7 @@ int set_automerge(struct list_head *msgs, struct cmd *cmd, struct set *set,
                return 0;
        }
 
-       if (existing_set) {
-               if (existing_set->init) {
-                       list_splice_init(&existing_set->init->expressions,
-                                        &init->expressions);
-               } else {
-                       existing_set->init = set_expr_alloc(&internal_location,
-                                                           set);
-               }
-       }
-
-       set_to_range(init);
-       list_expr_sort(&init->expressions);
+       set_sort_splice(init, set);
 
        ctx.purge = set_expr_alloc(&internal_location, set);
 
@@ -591,18 +600,7 @@ int set_overlap(struct list_head *msgs, struct set *set, struct expr *init)
        struct expr *i, *n, *clone;
        int err;
 
-       if (existing_set) {
-               if (existing_set->init) {
-                       list_splice_init(&existing_set->init->expressions,
-                                        &init->expressions);
-               } else {
-                       existing_set->init = set_expr_alloc(&internal_location,
-                                                           set);
-               }
-       }
-
-       set_to_range(init);
-       list_expr_sort(&init->expressions);
+       set_sort_splice(init, set);
 
        err = setelem_overlap(msgs, set, init);
 
index 8e6aac5fb24edfc91ef4b153f51ee6fb8dcae130..dca71422dd94713b18b1aee313d255b07243d17b 100644 (file)
@@ -70,7 +70,7 @@ static int expr_msort_cmp(const struct expr *e1, const struct expr *e2)
        return ret;
 }
 
-static void list_splice_sorted(struct list_head *list, struct list_head *head)
+void list_splice_sorted(struct list_head *list, struct list_head *head)
 {
        struct list_head *h = head->next;
        struct list_head *l = list->next;