Skip to contentSkip to conversation

JSON: Improve performance of json_events_cb()

PATCH In review Phil Sutter 2 participants 2 messages May 13, 2020 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 JSON: Improve performance of json_events_cb()

The function tries to insert handles into JSON input for echo option. Yet there may be nothing to do if the given netlink message doesn't contain a handle, e.g. if it is an 'add element' command. Calling seqnum_to_json() is pointless overhead in that case, and if input is large this overhead is significant. Better wait with that call until after checking if the message is relevant at all.

---
 src/parser_json.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
src/parser_json.c +6 -3
index 4468407b0ecd0..3a84bd96af31f 100644
@@ -3847,12 +3847,15 @@ static uint64_t handle_from_nlmsg(const struct nlmsghdr *nlh)
 }
 int json_events_cb(const struct nlmsghdr *nlh, struct netlink_mon_handler *monh)
 {
-	json_t *tmp, *json = seqnum_to_json(nlh->nlmsg_seq);
 	uint64_t handle = handle_from_nlmsg(nlh);
+	json_t *tmp, *json;
 	void *iter;
 
-	/* might be anonymous set, ignore message */
-	if (!json || !handle)
+	if (!handle)
+		return MNL_CB_OK;
+
+	json = seqnum_to_json(nlh->nlmsg_seq);
+	if (!json)
 		return MNL_CB_OK;
 
 	tmp = json_object_get(json, "add");
Signature · 1 line
2.26.2
Acked-by on v1 1/1
#1 lore raw
Quoted 37 lines · Phil Sutter

The function tries to insert handles into JSON input for echo option. Yet there may be nothing to do if the given netlink message doesn't contain a handle, e.g. if it is an 'add element' command. Calling seqnum_to_json() is pointless overhead in that case, and if input is large this overhead is significant. Better wait with that call until after checking if the message is relevant at all.

---
 src/parser_json.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
src/parser_json.c +6 -3
index 4468407b0ecd0..3a84bd96af31f 100644
@@ -3847,12 +3847,15 @@ static uint64_t handle_from_nlmsg(const struct nlmsghdr *nlh)
 }
 int json_events_cb(const struct nlmsghdr *nlh, struct netlink_mon_handler *monh)
 {
-	json_t *tmp, *json = seqnum_to_json(nlh->nlmsg_seq);
 	uint64_t handle = handle_from_nlmsg(nlh);
+	json_t *tmp, *json;
 	void *iter;
 
-	/* might be anonymous set, ignore message */
-	if (!json || !handle)
+	if (!handle)
+		return MNL_CB_OK;
+
+	json = seqnum_to_json(nlh->nlmsg_seq);
+	if (!json)
 		return MNL_CB_OK;
 
 	tmp = json_object_get(json, "add");
Signature · 1 line
2.26.2

With this I see a 10x improvement when adding many (hundreds) set elements [1].

Thanks Phil!

[1]: https://bugzilla.redhat.com/show_bug.cgi?id=1834853#c6