[Opensrf-commits] r1732 - in trunk: include/opensrf src/libopensrf (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Aug 5 08:53:20 EDT 2009


Author: scottmk
Date: 2009-08-05 08:53:19 -0400 (Wed, 05 Aug 2009)
New Revision: 1732

Modified:
   trunk/include/opensrf/osrf_json.h
   trunk/src/libopensrf/osrf_json_object.c
   trunk/src/libopensrf/osrf_legacy_json.c
Log:
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.


Modified: trunk/include/opensrf/osrf_json.h
===================================================================
--- trunk/include/opensrf/osrf_json.h	2009-07-29 13:23:55 UTC (rev 1731)
+++ trunk/include/opensrf/osrf_json.h	2009-08-05 12:53:19 UTC (rev 1732)
@@ -120,7 +120,7 @@
 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;

Modified: trunk/src/libopensrf/osrf_json_object.c
===================================================================
--- trunk/src/libopensrf/osrf_json_object.c	2009-07-29 13:23:55 UTC (rev 1731)
+++ trunk/src/libopensrf/osrf_json_object.c	2009-08-05 12:53:19 UTC (rev 1732)
@@ -397,7 +397,6 @@
 
 void jsonIteratorFree(jsonIterator* itr) {
 	if(!itr) return;
-	free(itr->key);
 	osrfHashIteratorFree(itr->hashItr);
 	free(itr);
 }
@@ -405,11 +404,14 @@
 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 @@
 		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;
 		}

Modified: trunk/src/libopensrf/osrf_legacy_json.c
===================================================================
--- trunk/src/libopensrf/osrf_legacy_json.c	2009-07-29 13:23:55 UTC (rev 1731)
+++ trunk/src/libopensrf/osrf_legacy_json.c	2009-08-05 12:53:19 UTC (rev 1732)
@@ -767,7 +767,7 @@
 
 				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);



More information about the opensrf-commits mailing list