DaveGamble / cJSON

Ultralightweight JSON parser in ANSI C
MIT License
10.81k stars 3.21k forks source link

Code size optimization #367

Open nazrchorn opened 5 years ago

nazrchorn commented 5 years ago

Hi,

I would like to keep code size as small as possible for my project. Analyzed my project size with different versions of cJSON build by IAR. Here is some data: Existing project with some old cJSON: 107126 bytes cJSON updated to 1.1.0: 106570 bytes cJSON updated to 1.2.0: 106542 bytes cJSON updated to 1.3.2: 104902 bytes cJSON updated to 1.4.0: 106346 bytes cJSON updated to 1.5.0: failed to build cJSON updated to 1.6.0: 110946 bytes cJSON updated to 1.7.0: 111010 bytes cJSON updated to 1.7.12(latest): 111002 bytes

So the optimal by size for me is v1.3.2. I would like to take the latest, but ~6kb increase is huge.

Could you please optimize the code size of latest release to the minimum possible?

For example by removing sscanf call from print_number optimizes ~4500bytes because of large sscanf library size. I believe there is better method how to check number of nonzero digits before converting it to string instead of scanning string and rework printf.

gatzka commented 5 years ago

What is the processor architecture you are compiling for? The numbers you give seem quite large for me. On x86 I've ~13 kByte, on ARM 7 kByte.

So it's most probably up to the C library adding to the code size...

nazrchorn commented 5 years ago

ARM cortex CM0p I reported numbers of "Existing project" + cJSON with different versions and see how total size differs. There are some other code which is constant in my compare.

Yes, C libraries like "sscanf" also needs to be calculated if they are used by cJSON functions.

7kb is measured for full cJSON v1.7.12? How many did you get for 1.3.2?