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>.