Commits

Peter Shinners committed 45e1b21

Optimize creation of values.

Memory for string combined with value object. Value structures initialized with a single assignment.

  • Participants
  • Parent commits d497eeb

Comments (0)

Files changed (1)

 
 /* JSON Structure Support */
 
-static struct JSON_Value* JSON_value_new(enum JSON_Value_Type type) {
-    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value));
-    if (!pResult) {
-        return 0;
-    }
-    pResult->type= type;
-    pResult->string_value= 0;
-    pResult->next_value= 0;
+struct JSON_Value* JSON_value_new_string_with_buffer(size_t length) {
+    static struct JSON_Value initString= {JSON_VALUE_TYPE_STRING};
+    // Round length up to the nearest interval
+    size_t interval = sizeof(struct JSON_Value);
+    size_t remainder = length % interval;
+    size_t padded = length + (interval - remainder);
+    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value) + padded);
+    *pResult = initString;
+    pResult->string_value= (char*)(pResult + 1);
     return pResult;
 }
 
 struct JSON_Value* JSON_Value_New_String(const char* stringData) {
-    struct JSON_Value* pResult= JSON_value_new(JSON_VALUE_TYPE_STRING);
-    pResult->string_value= strdup(stringData);
+    size_t length = (strlen(stringData) + 1) * sizeof(char);
+    struct JSON_Value* pResult = JSON_value_new_string_with_buffer(length);
+    memcpy((void*)pResult->string_value, stringData, length);
     return pResult;
 }
 
+
 struct JSON_Value* JSON_Value_New_Number(double numberValue) {
-    struct JSON_Value* pResult= JSON_value_new(JSON_VALUE_TYPE_NUMBER);
+    static struct JSON_Value initNumber= {JSON_VALUE_TYPE_NUMBER};
+    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value));
+    *pResult = initNumber;
     pResult->number_value= numberValue;
     return pResult;
 }
 
 struct JSON_Value* JSON_Value_New_Object() {
-    return JSON_value_new(JSON_VALUE_TYPE_OBJECT);
+    static struct JSON_Value initObject= {JSON_VALUE_TYPE_OBJECT};
+    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value));
+    *pResult = initObject;
+    return pResult;
 }
 
 struct JSON_Value* JSON_Value_New_Array() {
-    return JSON_value_new(JSON_VALUE_TYPE_ARRAY);
+    static struct JSON_Value initArray= {JSON_VALUE_TYPE_ARRAY};
+    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value));
+    *pResult = initArray;
+    return pResult;
 }
+
 struct JSON_Value* JSON_Value_New_True() {
-    return JSON_value_new(JSON_VALUE_TYPE_TRUE);
+    static struct JSON_Value initTrue= {JSON_VALUE_TYPE_TRUE};
+    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value));
+    *pResult = initTrue;
+    return pResult;
 }
 
 struct JSON_Value* JSON_Value_New_False() {
-    return JSON_value_new(JSON_VALUE_TYPE_FALSE);
+    static struct JSON_Value initFalse= {JSON_VALUE_TYPE_FALSE};
+    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value));
+    *pResult = initFalse;
+    return pResult;
 }
 
 struct JSON_Value* JSON_Value_New_Null() {
-    return JSON_value_new(JSON_VALUE_TYPE_NULL);
+    static struct JSON_Value initNull= {JSON_VALUE_TYPE_NULL};
+    struct JSON_Value* pResult= (struct JSON_Value*)malloc(sizeof(struct JSON_Value));
+    *pResult = initNull;
+    return pResult;
 }
 
 void JSON_Value_Free(struct JSON_Value* pValue) {
             pChildObject= pNext;
         }
     }
-    else if (pValue->type == JSON_VALUE_TYPE_STRING && pValue->string_value) {
-        free((void*)pValue->string_value);
-        pValue->string_value= 0;
-    }
     free(pValue);
 }
 
 
     for (;;) {
         if (*pState->currentChar == '\"') {
-            result= JSON_value_new(JSON_VALUE_TYPE_STRING);
-            result->string_value= (char*)malloc((stringLength+1)*sizeof(char));
+            result= JSON_value_new_string_with_buffer((stringLength + 1) * sizeof(char));
             decodeStringEscaping((char*)result->string_value, stringValueStart, pState->currentChar);
             pState->currentLine+= lineCount;
             JSON_next_char(pState);