Optimization of the case_insensitive_strcmp function#959
Optimization of the case_insensitive_strcmp function#959gardonkoo wants to merge 4 commits intoDaveGamble:masterfrom
Conversation
| @@ -142,15 +144,16 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned | |||
| return 0; | |||
There was a problem hiding this comment.
Consider calling strcasecmp(3) directly when available after checking NULL pointer? This library function has been optimized, and you almost can't write one faster than it.
There was a problem hiding this comment.
The function case_insensitive_strcmp is a native implementation of the CJSON library.
According to the code, it is mainly used to find items in a JSON object by name.
By default, two NULL values are not considered equal.
It does not care about the length/size of the two strings, neither the magnitude relationship in ASCII order.
It only cares about whether they are equal.
Its functionality is not entirely consistent with that of strcasecmp(3) (at least in the current usage scenario within the source code).
Additionally, strcasecmp(3) is a POSIX-compliant function, while CJSON is a cross-platform library.
Perhaps we need to use macros to distinguish between different platforms and call the optimized functions for each respective platform; or we need to implement a cross-platform general interface ourselves. CJSON chose the latter.
Hi there!
I noticed that case_insensitive_strcmp is a frequently used function, which is always called when searching for cJSON items.
I found that this function can be further optimized. Below is a comparison of the assembly instructions of the two implementations.
Original implementation:
New implementation:
Environment:
OS: Ubuntu 24.04.3 LTS
cmake: 3.28.3
gcc: 13.3.0