[Opensrf-commits] r1305 - in trunk: include/opensrf src/libopensrf

svn at svn.open-ils.org svn at svn.open-ils.org
Fri Apr 11 11:20:09 EDT 2008


Author: miker
Date: 2008-04-11 10:42:31 -0400 (Fri, 11 Apr 2008)
New Revision: 1305

Modified:
   trunk/include/opensrf/osrf_hash.h
   trunk/src/libopensrf/osrf_hash.c
Log:
Patch from Scott McKellar:

This patch boosts the performance of an osrfHashIterator a bit, by
reusing the "current" buffer whenever possible instead of freeing and
reallocating it on every iteration.

I wrote a benchmark that repeatedly traverses a 27-item hash table.
The new version is about 10% faster.  This improvement is not as
dramatic as I had hoped for, but it's easy to get, and it won't hurt.



Modified: trunk/include/opensrf/osrf_hash.h
===================================================================
--- trunk/include/opensrf/osrf_hash.h	2008-04-11 14:13:49 UTC (rev 1304)
+++ trunk/include/opensrf/osrf_hash.h	2008-04-11 14:42:31 UTC (rev 1305)
@@ -15,6 +15,7 @@
 
 struct _osrfHashIteratorStruct {
 	char* current;
+	size_t currsize;  // length of "current" buffer
 	int currentIdx;
 	osrfHash* hash;
 	osrfStringArray* keys;

Modified: trunk/src/libopensrf/osrf_hash.c
===================================================================
--- trunk/src/libopensrf/osrf_hash.c	2008-04-11 14:13:49 UTC (rev 1304)
+++ trunk/src/libopensrf/osrf_hash.c	2008-04-11 14:42:31 UTC (rev 1305)
@@ -227,6 +227,7 @@
 	itr->hash = hash;
 	itr->currentIdx = 0;
 	itr->current = NULL;
+	itr->currsize = 0;
 	itr->keys = osrfHashKeysInc(hash);
 	return itr;
 }
@@ -234,8 +235,24 @@
 void* osrfHashIteratorNext( osrfHashIterator* itr ) {
 	if(!(itr && itr->hash)) return NULL;
 	if( itr->currentIdx >= itr->keys->size ) return NULL;
-	free(itr->current);
-	itr->current = strdup(osrfStringArrayGetString(itr->keys, itr->currentIdx++));
+
+	// Copy the string to iter->current
+	const char * curr = osrfStringArrayGetString(itr->keys, itr->currentIdx++);
+	size_t new_len = strlen(curr);
+	if( new_len >= itr->currsize ) {
+		// We need a bigger buffer
+
+		if(0 == itr->currsize) itr->currsize = 64; //default size
+		do {
+			itr->currsize *= 2;
+		} while( new_len >= itr->currsize );
+
+		if(itr->current)
+			free(itr->current);
+		itr->current = safe_malloc(itr->currsize);
+	}
+	strcpy(itr->current, curr);
+	
 	char* val = osrfHashGet( itr->hash, itr->current );
 	return val;
 }
@@ -248,10 +265,9 @@
 
 void osrfHashIteratorReset( osrfHashIterator* itr ) {
 	if(!itr) return;
-	free(itr->current);
+	itr->current[0] = '\0';
 	itr->keys = osrfHashKeysInc(itr->hash);
 	itr->currentIdx = 0;
-	itr->current = NULL;
 }
 
 



More information about the opensrf-commits mailing list