Skip to content

Adjust the size of cJSON from 64 to 56 through memory alignment#608

Open
yangbodong22011 wants to merge 1 commit intoDaveGamble:masterfrom
yangbodong22011:feature-reduce-cJSON-struct-size
Open

Adjust the size of cJSON from 64 to 56 through memory alignment#608
yangbodong22011 wants to merge 1 commit intoDaveGamble:masterfrom
yangbodong22011:feature-reduce-cJSON-struct-size

Conversation

@yangbodong22011
Copy link

@yangbodong22011 yangbodong22011 commented Aug 17, 2021

By adjusting the position, the valueint and type
memory are aligned, occupying a total of 8 bytes
instead of 16 bytes, so sizeof(cJSON) is reduced
from 64 bytes to 56 bytes.

p.s. Although this optimization may not be
effective in the memory allocator's view, because
the size that the allocator may allocate is 64 bytes,
it may be effective in some scenarios, and it will
not be worse than the current situation.

code:

#include <stdio.h>

/* The cJSON structure: */
typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *next;
    struct cJSON *prev;
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    struct cJSON *child;

    /* The type of the item, as above. */
    int type;

    /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    /* The item's number, if type==cJSON_Number */
    double valuedouble;

    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    char *string;
} cJSON;

/* The cJSON structure: */
typedef struct cJSONAfter
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *next;
    struct cJSON *prev;
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    struct cJSON *child;

    /* The type of the item, as above. */
    int type;

    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
    char *valuestring;
    /* The item's number, if type==cJSON_Number */
    double valuedouble;

    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    char *string;
} cJSONAfter;


int main() {

    printf("before: %lu ,after: %lu", sizeof(cJSON), sizeof(cJSONAfter));
    return 0;
}

$./a.out
before: 64 ,after: 56

@yangbodong22011 yangbodong22011 force-pushed the feature-reduce-cJSON-struct-size branch 2 times, most recently from e90904f to c3c6a12 Compare August 25, 2021 07:44
@Alanscut
Copy link
Collaborator

hi @yangbodong22011 , thanks your great contribution! This optimization could indeed save memory in some scenarios, but this is a breaking change, users need change the initiation of the cJSON struct, so it may merged in the next major version, such as 1.8 or 2.0

@Alanscut Alanscut added this to the Future milestone Aug 25, 2021
By adjusting the position, the valueint and type
memory are aligned, occupying a total of 8 bytes
instead of 16 bytes, so sizeof(cJSON) is reduced
from 64 bytes to 56 bytes.

p.s. Although this optimization may not be
effective in the memory allocator's view, because
the size that the allocator may allocate is 64 bytes,
it may be effective in some scenarios, and it will
not be worse than the current situation.
@yangbodong22011 yangbodong22011 force-pushed the feature-reduce-cJSON-struct-size branch from c3c6a12 to 79293f5 Compare August 25, 2021 07:51
@yangbodong22011
Copy link
Author

@Alanscut Thanks for your reply, let's stay open and wait for more comments.

@tristan957
Copy link

This is an ABI-break, not an API break. All that would be required here is incrementing the SOVERSION.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants