parson icon indicating copy to clipboard operation
parson copied to clipboard

misc improvements to json_serialize_to_buffer() performance

Open commodo opened this issue 2 years ago • 2 comments

Well, the bottleneck for the our use-case is sprintf() for numbers. There are too many numbers to convert on an embedded system.

One minor improvement that I am still hesitant to push, is to return a pessimistic buf-size when json_serialization_size() gets called i.e. to return PARSON_NUM_BUF_SIZE instead of calling spritnf() (which would be a good performance booster). This changes some semantics (also in tests), where more memory would get allocated (thantneeded), but that wouldn't be too bad considering that sprintf() is too slow (which opens up a discussion about which is worse: speed or memory over-use).

sprintf() performance is presented here: https://aras-p.info/blog/2022/02/25/Curious-lack-of-sprintf-scaling/

So in this (patch) series, the idea (I am presenting) is that a user can/could provide their own serializer function for double/numbers (in case they don't like sprintf()). But I also included some minor perf stuff that I found along the way. The improvement (the other stuff adds) isl small, but (generally) they're still worth considering.

Signed-off-by: Alexandru Ardelean [email protected]

commodo avatar Aug 28 '22 17:08 commodo

This looks quite good but I'll need a few days to have a deeper look.

kgabis avatar Aug 29 '22 18:08 kgabis

sure: no hurry from my side;

we need this into the azure-iot-sdk stuff, so these changes will pop-up after some months into our tree (after they trickle);

we're still discussing about how to handle the sprintf() bottle neck; it may just end up needing to not do JSON-to-string conversions every now-n-then;

commodo avatar Aug 30 '22 11:08 commodo

updated based on comment;

commodo avatar Oct 24 '22 06:10 commodo

I've tidied up your changes a bit and restored functions calls to json_value_get_*. I know it feels redundant but I'd rather if it's consistent with the rest of the codebase.

kgabis avatar Nov 12 '22 21:11 kgabis

I've tidied up your changes a bit and restored functions calls to json_value_get_*. I know it feels redundant but I'd rather if it's consistent with the rest of the codebase.

ack no worries from me;

the compiler should be able to optimize there (i hope) to better inline those bits; the biggest performance hit we get is from sprintf() anyway, because there are too many numbers in some json-s

commodo avatar Nov 14 '22 08:11 commodo