From bf2398c1486ec9fd284293df00f72e743e77996e Mon Sep 17 00:00:00 2001 From: scottmk Date: Wed, 5 Aug 2009 12:53:19 +0000 Subject: [PATCH] Performance tweak to jsonIterator. 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 | 2 +- src/libopensrf/osrf_json_object.c | 16 +++++++++------- src/libopensrf/osrf_legacy_json.c | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/opensrf/osrf_json.h b/include/opensrf/osrf_json.h index 237b314..4dd8ead 100644 --- a/include/opensrf/osrf_json.h +++ b/include/opensrf/osrf_json.h @@ -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; diff --git a/src/libopensrf/osrf_json_object.c b/src/libopensrf/osrf_json_object.c index 75785d7..c9b6a29 100644 --- a/src/libopensrf/osrf_json_object.c +++ b/src/libopensrf/osrf_json_object.c @@ -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; } diff --git a/src/libopensrf/osrf_legacy_json.c b/src/libopensrf/osrf_legacy_json.c index 31d62b1..cd43d13 100644 --- a/src/libopensrf/osrf_legacy_json.c +++ b/src/libopensrf/osrf_legacy_json.c @@ -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); -- 2.11.0