[Opensrf-commits] r1807 - trunk/src/libopensrf (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Oct 5 12:30:01 EDT 2009


Author: scottmk
Date: 2009-10-05 12:29:57 -0400 (Mon, 05 Oct 2009)
New Revision: 1807

Modified:
   trunk/src/libopensrf/osrf_message.c
Log:
Performance tweak to message handling.

In five locations in osrf_message.c, we were turning a jsonObject into
a JSON string, only to parse it again back into a jsonObject:

-- osrf_message_add_object_param()
-- osrfMessageToJSON() (two occurrences)
-- osrf_message_deserialize() (two occurrences)

That's silly.

This new version copies the original jsonObject directly, with no round
trip through a JSON string.  We use jsonObjectDecodeClass() to do the
copy, in order to make sure that all class hints are decoded into
classnames.

(Until recently, jsonObjectDecodeClass() would remove classnames that were
already present.  That's presumably the reason for the curious and
inefficient two-step procedure that we were using up till now.)

Also: In two locations (in osrf_message_add_object_param() and
osrf_message_add_param())  We used jsonParseString() to parse a
hard-coded "[]".  I replaced those calls with equivalent (and faster)
calls to jsonNewObjectType( JSON_ARRAY );

Also: in osrf_message_set_result_content() I eliminated a pointless test
for nullity of the json_string variable.  An earlier test already
guarantees that json_string is not NULL.

M    src/libopensrf/osrf_message.c


Modified: trunk/src/libopensrf/osrf_message.c
===================================================================
--- trunk/src/libopensrf/osrf_message.c	2009-10-05 14:48:24 UTC (rev 1806)
+++ trunk/src/libopensrf/osrf_message.c	2009-10-05 16:29:57 UTC (rev 1807)
@@ -49,13 +49,23 @@
 }
 
 
+/**
+	@brief Wrap a copy of a jsonObject in a JSON_ARRAY and store it in an osrfMessage.
+	@param msg Pointer to the osrfMessage.
+	@param o Pointer to the jsonObject of which a copy is to be stored.
+
+	Make a copy of the input jsonObject, with all classnames encoded with
+	JSON_CLASS_KEY and JSON_DATA_KEY.  Append it to a JSON_ARRAY stored at
+	msg->_params.
+
+	If there is nothing at msg->_params, create a new JSON_ARRAY
+	for it and add the new object as the first element.
+*/
 void osrf_message_add_object_param( osrfMessage* msg, const jsonObject* o ) {
 	if(!msg|| !o) return;
 	if(!msg->_params)
-		msg->_params = jsonParseString("[]");
-	char* j = jsonObjectToJSON(o);
-	jsonObjectPush(msg->_params, jsonParseString(j));
-	free(j);
+		msg->_params = jsonNewObjectType( JSON_ARRAY );
+	jsonObjectPush(msg->_params, jsonObjectDecodeClass( o ));
 }
 
 void osrf_message_set_params( osrfMessage* msg, const jsonObject* o ) {
@@ -78,7 +88,7 @@
 /* only works if parse_json_params is false */
 void osrf_message_add_param( osrfMessage* msg, const char* param_string ) {
 	if(msg == NULL || param_string == NULL) return;
-	if(!msg->_params) msg->_params = jsonParseString("[]");
+	if(!msg->_params) msg->_params = jsonNewObjectType( JSON_ARRAY );
 	jsonObjectPush(msg->_params, jsonParseString(param_string));
 }
 
@@ -100,7 +110,7 @@
 void osrf_message_set_result_content( osrfMessage* msg, const char* json_string ) {
 	if( msg == NULL || json_string == NULL) return;
 	msg->result_string =	strdup(json_string);
-	if(json_string) msg->_result_content = jsonParseString(json_string);
+	msg->_result_content = jsonParseString(json_string);
 }
 
 
@@ -178,8 +188,6 @@
 	char sc[64];
 	osrf_clearbuf(sc, sizeof(sc));
 
-	char* str;
-
 	INT_TO_STRING(msg->thread_trace);
 	jsonObjectSetKey(json, "threadTrace", jsonNewObject(INTSTR));
 
@@ -216,9 +224,7 @@
 			payload = jsonNewObject(NULL);
 			jsonObjectSetClass(payload, "osrfMethod");
 			jsonObjectSetKey(payload, "method", jsonNewObject(msg->method_name));
-			str = jsonObjectToJSON(msg->_params);
-			jsonObjectSetKey(payload, "params", jsonParseString(str));
-			free(str);
+			jsonObjectSetKey( payload, "params", jsonObjectDecodeClass( msg->_params ) );
 			jsonObjectSetKey(json, "payload", payload);
 
 			break;
@@ -230,9 +236,7 @@
 			jsonObjectSetKey(payload, "status", jsonNewObject(msg->status_text));
 			snprintf(sc, sizeof(sc), "%d", msg->status_code);
 			jsonObjectSetKey(payload, "statusCode", jsonNewObject(sc));
-			str = jsonObjectToJSON(msg->_result_content);
-			jsonObjectSetKey(payload, "content", jsonParseString(str));
-			free(str);
+			jsonObjectSetKey(payload, "content", jsonObjectDecodeClass( msg->_result_content ));
 			jsonObjectSetKey(json, "payload", payload);
 			break;
 	}
@@ -335,11 +339,9 @@
 
 				tmp0 = jsonObjectGetKeyConst(tmp,"params");
 				if(tmp0) {
-					char* s = jsonObjectToJSON(tmp0);
-					new_msg->_params = jsonParseString(s);
+					new_msg->_params = jsonObjectDecodeClass( tmp0 );
 					if(new_msg->_params && new_msg->_params->type == JSON_NULL) 
 						new_msg->_params->type = JSON_ARRAY;
-					free(s);
 				}
 
 				tmp0 = jsonObjectGetKeyConst(tmp,"status");
@@ -358,9 +360,7 @@
 
 				tmp0 = jsonObjectGetKeyConst(tmp,"content");
 				if(tmp0) {
-					char* s = jsonObjectToJSON(tmp0);
-					new_msg->_result_content = jsonParseString(s);
-					free(s);
+					new_msg->_result_content = jsonObjectDecodeClass( tmp0 );
 				}
 
 			}



More information about the opensrf-commits mailing list