Parse least common datatype last

Null types are almost certainly the least common object, so putting the
check last in the list will be faster.

This gave an approximate 5% performance improvement in parse_value() in
tests.
This commit is contained in:
Roger Light 2023-08-10 08:28:00 +01:00
parent cb8693b058
commit bb27ffa152

14
cJSON.c
View File

@ -1317,13 +1317,6 @@ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buf
}
/* parse the different types of values */
/* null */
if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
{
item->type = cJSON_NULL;
input_buffer->offset += 4;
return true;
}
/* false */
if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
{
@ -1359,6 +1352,13 @@ static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buf
{
return parse_object(item, input_buffer);
}
/* null */
if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
{
item->type = cJSON_NULL;
input_buffer->offset += 4;
return true;
}
return false;
}