[Opensrf-commits] r1571 - trunk/src/libopensrf
svn at svn.open-ils.org
svn at svn.open-ils.org
Mon Jan 5 12:50:21 EST 2009
Author: scottmk
Date: 2009-01-05 12:50:15 -0500 (Mon, 05 Jan 2009)
New Revision: 1571
Modified:
trunk/src/libopensrf/osrf_json_tools.c
Log:
This update boosts the performance of the jsonFormatString function.
1. Replaced the old _tabs function, which required the construction and
destruction of a growing_buffer, with a new append_indentation function,
which adds white space to an existing growing_buffer. This change
eliminates a passel of mallocs and frees.
2. Removed the call to strlen() from the loop condition.
3. Replaced calls to buffer_fadd(), a fairly slow function, with calls
to OSRF_BUFFER_ADD_CHAR() and append_indentation(). Also: replaced a
call to buffer_add_char with the corresponding macro.
4. Eliminated a harmless but wasteful bug that sometimes added
indentation to the end of a line.
In my benchmarking, using a moderately complex JSON string 201
characters long, the new version was seven times as fast as the old.
Modified: trunk/src/libopensrf/osrf_json_tools.c
===================================================================
--- trunk/src/libopensrf/osrf_json_tools.c 2009-01-05 17:36:42 UTC (rev 1570)
+++ trunk/src/libopensrf/osrf_json_tools.c 2009-01-05 17:50:15 UTC (rev 1571)
@@ -21,11 +21,11 @@
static jsonObject* findMultiPathRecurse( const jsonObject* o, const char* root );
static jsonObject* _jsonObjectEncodeClass( const jsonObject* obj, int ignoreClass );
-static char* _tabs(int count) {
- growing_buffer* buf = buffer_init(24);
- int i;
- for(i=0;i<count;i++) OSRF_BUFFER_ADD(buf, " ");
- return buffer_release(buf);
+static void append_indentation( growing_buffer* buf, int depth ) {
+ size_t n = 2 * depth;
+ char indent[ n ];
+ memset( indent, ' ', n );
+ buffer_add_n( buf, indent, n );
}
char* jsonFormatString( const char* string ) {
@@ -34,38 +34,31 @@
growing_buffer* buf = buffer_init(64);
int i;
int depth = 0;
- char* tab = NULL;
char c;
- for(i=0; i!= strlen(string); i++) {
+ for(i = 0; string[i]; i++) {
c = string[i];
if( c == '{' || c == '[' ) {
- tab = _tabs(++depth);
- buffer_fadd( buf, "%c\n%s", c, tab);
- free(tab);
+ OSRF_BUFFER_ADD_CHAR( buf, c );
+ OSRF_BUFFER_ADD_CHAR( buf, '\n' );
+ append_indentation( buf, ++depth );
} else if( c == '}' || c == ']' ) {
- tab = _tabs(--depth);
- buffer_fadd( buf, "\n%s%c", tab, c);
- free(tab);
+ OSRF_BUFFER_ADD_CHAR( buf, '\n' );
+ append_indentation( buf, --depth );
+ OSRF_BUFFER_ADD_CHAR( buf, c );
- if(string[i+1] != ',') {
- tab = _tabs(depth);
- buffer_fadd( buf, "%s", tab );
- free(tab);
- }
-
} else if( c == ',' ) {
- tab = _tabs(depth);
- buffer_fadd(buf, ",\n%s", tab);
- free(tab);
+ OSRF_BUFFER_ADD_CHAR( buf, ',' );
+ OSRF_BUFFER_ADD_CHAR( buf, '\n' );
+ append_indentation( buf, depth );
- } else { buffer_add_char(buf, c); }
-
+ } else
+ OSRF_BUFFER_ADD_CHAR(buf, c);
}
return buffer_release(buf);
@@ -297,7 +290,3 @@
return arr;
}
-
-
-
-
More information about the opensrf-commits
mailing list