]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
JSON: Improve performance of json_events_cb()
authorPhil Sutter <phil@nwl.cc>
Wed, 13 May 2020 14:29:51 +0000 (16:29 +0200)
committerPhil Sutter <phil@nwl.cc>
Thu, 14 May 2020 10:27:24 +0000 (12:27 +0200)
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.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Eric Garver <eric@garver.life>
src/parser_json.c

index 4468407b0ecd07b5e456fedc30cad98b82c8bd9f..3a84bd96af31feb782690e3094e3d7141598c2cc 100644 (file)
@@ -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");