json_dumps performance

138 views
Skip to first unread message

JanssonUser

unread,
Nov 19, 2020, 8:26:23 PM11/19/20
to Jansson users
Hello!
   I  using jansson v2.7. I encode all my data using json object methods. I need to send this encoded data on UDP. So, I am using json_dumps to convert the encoded data to string.
But, this is slowing down the performance heavily. Is there any way I can avoid using json_dumps or any other way to encode data?

For my data, json_dumps is returning a string of ~1700 bytes. 
I don't see any issue with encoding and the encoded data is all fine.
Only issue I see is the json_dumps performance. Just for testing, If I comment out json_dumps() and do just the encoding, performance increases multifold.
Appreciate any suggestions.

Regards,
Ujjvala

Graeme Smecher

unread,
Nov 20, 2020, 7:49:21 PM11/20/20
to jansso...@googlegroups.com
Hi Ujjvala,

On 2020-11-19 5:26 p.m., JanssonUser wrote:
> Hello!
>    I  using jansson v2.7. I encode all my data using json object
> methods. I need to send this encoded data on UDP. So, I am using
> json_dumps to convert the encoded data to string.
> But, this is slowing down the performance heavily. Is there any way I
> can avoid using json_dumps or any other way to encode data?

Just to make sure we're using a common terminology: your JSON is not
actually encoded until you call one of the "dump" methods (json_dumps,
json_dumpf, json_dump_file, json_dump_callback). Until then, you're
using the Jansson API but your data remains in C types with Jansson
wrappers around them.

That's why json_dump() is relatively slow: it's doing the work
associated with converting C types to a textual representation.

If you look at the Jansson source code, you'll find it's actually
leaning on libc to do this work in places: for example, doubles are
rendered as follows: [1]

ret = snprintf(buffer, size, "%.*g", precision, value);

Unicode [2] is probably also another slow spot.

You /could/ rip some of these things out, or try to replace them with
faster implementations. Ultimately, though, string encoding is integral
to the JSON spec. If a faster but compatible implementation was within
reach, Jansson would already have it. By choosing JSON as a
serialization format, you've already prioritized interoperability and
human readability over performance.

You might consider architectural changes to upstream code, i.e.
offloading JSON serialization to another thread.

cheers,
Graeme

[1]: https://github.com/akheron/jansson/blob/master/src/strconv.c#L87
[2]: https://github.com/akheron/jansson/blob/master/src/dump.c#L94

> For my data, json_dumps is returning a string of ~1700 bytes. 
> I don't see any issue with encoding and the encoded data is all fine.
> Only issue I see is the json_dumps performance. Just for testing, If I
> comment out json_dumps() and do just the encoding, performance increases
> multifold.
> Appreciate any suggestions.
>
> Regards,
> Ujjvala
>
> --
> --
> Jansson users mailing list
> jansso...@googlegroups.com
> http://groups.google.com/group/jansson-users
> ---
> You received this message because you are subscribed to the Google
> Groups "Jansson users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to jansson-user...@googlegroups.com
> <mailto:jansson-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jansson-users/416903ae-2d02-4b3e-9441-b34c5c840893n%40googlegroups.com
> <https://groups.google.com/d/msgid/jansson-users/416903ae-2d02-4b3e-9441-b34c5c840893n%40googlegroups.com?utm_medium=email&utm_source=footer>.
pEpkey.asc

Ujjvala Nangineni

unread,
Nov 20, 2020, 8:17:21 PM11/20/20
to jansso...@googlegroups.com
Hi Graeme,
    Thank you very much for the answer. I will look into the optimizations, if any, I can do. I have offloaded the JSON serialization and have a dedicated thread doing only this. I will look at other architectural changes also.

Regards,
Ujjvala

To unsubscribe from this group and stop receiving emails from it, send an email to jansson-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jansson-users/fab6fe3c-a433-1e4d-c991-2028336398fe%40gmail.com.

Ujjvala Nangineni

unread,
Nov 20, 2020, 9:07:44 PM11/20/20
to jansso...@googlegroups.com
Hi Graeme,
   Are there any open source json libraries which deliver better performance, if I can compromise on interoperability and human readability?

Regards,
Ujjvala

Graeme Smecher

unread,
Nov 20, 2020, 10:20:44 PM11/20/20
to jansso...@googlegroups.com
Hi Ujjvala,

I suggest starting here:


The version of Jansson benchmarked there is pretty old, so you may find the results a little out of date.

There are definitely faster JSON libraries, but ultimately JSON has an intrinsic speed limit that other serialization techniques (e.g
 protobuf) won't have.

Best,
Graeme

Reply all
Reply to author
Forward
0 new messages