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

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