netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [nft PATCH] JSON: Improve performance of json_events_cb()
@ 2020-05-13 14:38 Phil Sutter
  2020-05-13 15:11 ` Eric Garver
  0 siblings, 1 reply; 2+ messages in thread
From: Phil Sutter @ 2020-05-13 14:38 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel, Eric Garver

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>
---
 src/parser_json.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/parser_json.c b/src/parser_json.c
index 4468407b0ecd0..3a84bd96af31f 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -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");
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [nft PATCH] JSON: Improve performance of json_events_cb()
  2020-05-13 14:38 [nft PATCH] JSON: Improve performance of json_events_cb() Phil Sutter
@ 2020-05-13 15:11 ` Eric Garver
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Garver @ 2020-05-13 15:11 UTC (permalink / raw)
  To: Phil Sutter; +Cc: Pablo Neira Ayuso, netfilter-devel

On Wed, May 13, 2020 at 04:38:03PM +0200, Phil Sutter wrote:
> 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>
> ---
>  src/parser_json.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/src/parser_json.c b/src/parser_json.c
> index 4468407b0ecd0..3a84bd96af31f 100644
> --- a/src/parser_json.c
> +++ b/src/parser_json.c
> @@ -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");
> -- 
> 2.26.2

Acked-by: Eric Garver <eric@garver.life>

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-05-13 15:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-13 14:38 [nft PATCH] JSON: Improve performance of json_events_cb() Phil Sutter
2020-05-13 15:11 ` Eric Garver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).