Blame SOURCES/0038-json-echo-Speedup-seqnum_to_json.patch

acfc56
From 26c4f15080663a12006abf8539ebf28bb223e6d9 Mon Sep 17 00:00:00 2001
acfc56
From: Phil Sutter <psutter@redhat.com>
acfc56
Date: Mon, 7 Dec 2020 18:29:15 +0100
acfc56
Subject: [PATCH] json: echo: Speedup seqnum_to_json()
acfc56
acfc56
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1900565
acfc56
Upstream Status: nftables commit 389a0e1edc89a
acfc56
acfc56
commit 389a0e1edc89a4048a272e569d3349b1d43bc567
acfc56
Author: Phil Sutter <phil@nwl.cc>
acfc56
Date:   Fri Nov 20 20:01:59 2020 +0100
acfc56
acfc56
    json: echo: Speedup seqnum_to_json()
acfc56
acfc56
    Derek Dai reports:
acfc56
    "If there are a lot of command in JSON node, seqnum_to_json() will slow
acfc56
    down application (eg: firewalld) dramatically since it iterate whole
acfc56
    command list every time."
acfc56
acfc56
    He sent a patch implementing a lookup table, but we can do better: Speed
acfc56
    this up by introducing a hash table to store the struct json_cmd_assoc
acfc56
    objects in, taking their netlink sequence number as key.
acfc56
acfc56
    Quickly tested restoring a ruleset containing about 19k rules:
acfc56
acfc56
    | # time ./before/nft -jeaf large_ruleset.json >/dev/null
acfc56
    | 4.85user 0.47system 0:05.48elapsed 97%CPU (0avgtext+0avgdata 69732maxresident)k
acfc56
    | 0inputs+0outputs (15major+16937minor)pagefaults 0swaps
acfc56
acfc56
    | # time ./after/nft -jeaf large_ruleset.json >/dev/null
acfc56
    | 0.18user 0.44system 0:00.70elapsed 89%CPU (0avgtext+0avgdata 68484maxresident)k
acfc56
    | 0inputs+0outputs (15major+16645minor)pagefaults 0swaps
acfc56
acfc56
    Bugzilla: https://bugzilla.netfilter.org/show_bug.cgi?id=1479
acfc56
    Reported-by: Derek Dai <daiderek@gmail.com>
acfc56
    Signed-off-by: Phil Sutter <phil@nwl.cc>
acfc56
---
acfc56
 src/parser_json.c | 28 ++++++++++++++++++----------
acfc56
 1 file changed, 18 insertions(+), 10 deletions(-)
acfc56
acfc56
diff --git a/src/parser_json.c b/src/parser_json.c
acfc56
index ddc694f..107dc38 100644
acfc56
--- a/src/parser_json.c
acfc56
+++ b/src/parser_json.c
acfc56
@@ -3646,42 +3646,50 @@ static int json_verify_metainfo(struct json_ctx *ctx, json_t *root)
acfc56
 }
acfc56
 
acfc56
 struct json_cmd_assoc {
acfc56
-	struct json_cmd_assoc *next;
acfc56
+	struct hlist_node hnode;
acfc56
 	const struct cmd *cmd;
acfc56
 	json_t *json;
acfc56
 };
acfc56
 
acfc56
-static struct json_cmd_assoc *json_cmd_list = NULL;
acfc56
+#define CMD_ASSOC_HSIZE		512
acfc56
+static struct hlist_head json_cmd_assoc_hash[CMD_ASSOC_HSIZE];
acfc56
 
acfc56
 static void json_cmd_assoc_free(void)
acfc56
 {
acfc56
 	struct json_cmd_assoc *cur;
acfc56
+	struct hlist_node *pos, *n;
acfc56
+	int i;
acfc56
 
acfc56
-	while (json_cmd_list) {
acfc56
-		cur = json_cmd_list;
acfc56
-		json_cmd_list = cur->next;
acfc56
-		free(cur);
acfc56
+	for (i = 0; i < CMD_ASSOC_HSIZE; i++) {
acfc56
+		hlist_for_each_entry_safe(cur, pos, n,
acfc56
+					  &json_cmd_assoc_hash[i], hnode)
acfc56
+			free(cur);
acfc56
 	}
acfc56
 }
acfc56
 
acfc56
 static void json_cmd_assoc_add(json_t *json, const struct cmd *cmd)
acfc56
 {
acfc56
 	struct json_cmd_assoc *new = xzalloc(sizeof *new);
acfc56
+	int key = cmd->seqnum % CMD_ASSOC_HSIZE;
acfc56
 
acfc56
-	new->next	= json_cmd_list;
acfc56
 	new->json	= json;
acfc56
 	new->cmd	= cmd;
acfc56
-	json_cmd_list	= new;
acfc56
+
acfc56
+	hlist_add_head(&new->hnode, &json_cmd_assoc_hash[key]);
acfc56
 }
acfc56
 
acfc56
 static json_t *seqnum_to_json(const uint32_t seqnum)
acfc56
 {
acfc56
-	const struct json_cmd_assoc *cur;
acfc56
+	int key = seqnum % CMD_ASSOC_HSIZE;
acfc56
+	struct json_cmd_assoc *cur;
acfc56
+	struct hlist_node *n;
acfc56
 
acfc56
-	for (cur = json_cmd_list; cur; cur = cur->next) {
acfc56
+
acfc56
+	hlist_for_each_entry(cur, n, &json_cmd_assoc_hash[key], hnode) {
acfc56
 		if (cur->cmd->seqnum == seqnum)
acfc56
 			return cur->json;
acfc56
 	}
acfc56
+
acfc56
 	return NULL;
acfc56
 }
acfc56
 
acfc56
-- 
acfc56
1.8.3.1
acfc56