Performance tweak to jsonIterator.
authorscottmk <scottmk@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 5 Aug 2009 12:53:19 +0000 (12:53 +0000)
committerscottmk <scottmk@9efc2488-bf62-4759-914b-345cdb29e865>
Wed, 5 Aug 2009 12:53:19 +0000 (12:53 +0000)
Instead of storing a malloc'd copy of the key of a JSON_HASH entry,
just store a const pointer to the key string stored in the
internal osrfHash.  That way we don't have to do a malloc and
free every time we bump the iterator.

This change also requires the addition of a couple of const qualifiers
in the client code.

git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1732 9efc2488-bf62-4759-914b-345cdb29e865

include/opensrf/osrf_json.h
src/libopensrf/osrf_json_object.c
src/libopensrf/osrf_legacy_json.c

index 237b314..4dd8ead 100644 (file)
@@ -120,7 +120,7 @@ typedef struct _jsonObjectStruct jsonObject;
 struct _jsonIteratorStruct {
        jsonObject* obj; /* the object we're traversing */
        osrfHashIterator* hashItr; /* the iterator for this hash */
-       char* key; /* if this object is an object, the current key */
+       const char* key; /* if this object is a hash, the current key */
        unsigned long index; /* if this object is an array, the index */
 };
 typedef struct _jsonIteratorStruct jsonIterator;
index 75785d7..c9b6a29 100644 (file)
@@ -397,7 +397,6 @@ jsonIterator* jsonNewIterator(const jsonObject* obj) {
 
 void jsonIteratorFree(jsonIterator* itr) {
        if(!itr) return;
-       free(itr->key);
        osrfHashIteratorFree(itr->hashItr);
        free(itr);
 }
@@ -405,11 +404,14 @@ void jsonIteratorFree(jsonIterator* itr) {
 jsonObject* jsonIteratorNext(jsonIterator* itr) {
        if(!(itr && itr->obj)) return NULL;
        if( itr->obj->type == JSON_HASH ) {
-               if(!itr->hashItr) return NULL;
+               if(!itr->hashItr)
+                       return NULL;
+
                jsonObject* item = osrfHashIteratorNext(itr->hashItr);
-        if(!item) return NULL;
-               free(itr->key);
-               itr->key = strdup( osrfHashIteratorKey(itr->hashItr) );
+               if( item )
+                       itr->key = osrfHashIteratorKey(itr->hashItr);
+               else
+                       itr->key = NULL;
                return item;
        } else {
                return jsonObjectGetIndex( itr->obj, itr->index++ );
@@ -629,14 +631,14 @@ int jsonIsNumeric( const char* s ) {
                if( '0' == *p++ ) {
 
                        // If the first digit is zero, it must be the
-                       // only digit to the lerft of the decimal
+                       // only digit to the left of the decimal
 
                        if( isdigit( (unsigned char) *p ) )
                                return 0;
                }
                else {
 
-                       // Skip oer the following digits
+                       // Skip over the following digits
 
                        while( isdigit( (unsigned char) *p ) ) ++p;
                }
index 31d62b1..cd43d13 100644 (file)
@@ -767,7 +767,7 @@ char* legacy_jsonObjectToJSON( const jsonObject* obj ) {
 
                                buffer_add(buf, "\"");
 
-                               char* key = itr->key;
+                               const char* key = itr->key;
                                int len = strlen(key);
                                char* output = uescape(key, len, 1);
                                buffer_add(buf, output);