Fossil SCM

Performance optimization to frequently used blob_append() routine.

drh 2019-09-24 20:05 trunk
Commit 933923747cb62c197e0a919c870fdf577221a9efeba7ae631dce40767961b4bf
1 file changed +21 -1
+21 -1
--- src/blob.c
+++ src/blob.c
@@ -277,12 +277,19 @@
277277
pBlob->xRealloc = blobReallocStatic;
278278
}
279279
280280
/*
281281
** Append text or data to the end of a blob.
282
+**
283
+** The blob_append_full() routine is a complete implementation.
284
+** The blob_append() routine only works for cases where nData>0 and
285
+** no resizing is required, and falls back to blob_append_full() if
286
+** either condition is not met, but runs faster in the common case
287
+** where all conditions are met. The use of blob_append() is
288
+** recommended, unless it is known in advance that nData<0.
282289
*/
283
-void blob_append(Blob *pBlob, const char *aData, int nData){
290
+void blob_append_full(Blob *pBlob, const char *aData, int nData){
284291
sqlite3_int64 nNew;
285292
assert( aData!=0 || nData==0 );
286293
blob_is_init(pBlob);
287294
if( nData<0 ) nData = strlen(aData);
288295
if( nData==0 ) return;
@@ -300,10 +307,23 @@
300307
}
301308
}
302309
memcpy(&pBlob->aData[pBlob->nUsed], aData, nData);
303310
pBlob->nUsed += nData;
304311
pBlob->aData[pBlob->nUsed] = 0; /* Blobs are always nul-terminated */
312
+}
313
+void blob_append(Blob *pBlob, const char *aData, int nData){
314
+ sqlite3_int64 nUsed;
315
+ assert( aData!=0 || nData==0 );
316
+ blob_is_init(pBlob);
317
+ if( nData<=0 || pBlob->nUsed + nData >= pBlob->nAlloc ){
318
+ blob_append_full(pBlob, aData, nData);
319
+ return;
320
+ }
321
+ nUsed = pBlob->nUsed;
322
+ pBlob->nUsed += nData;
323
+ pBlob->aData[pBlob->nUsed] = 0;
324
+ memcpy(&pBlob->aData[nUsed], aData, nData);
305325
}
306326
307327
/*
308328
** Append a single character to the blob
309329
*/
310330
--- src/blob.c
+++ src/blob.c
@@ -277,12 +277,19 @@
277 pBlob->xRealloc = blobReallocStatic;
278 }
279
280 /*
281 ** Append text or data to the end of a blob.
 
 
 
 
 
 
 
282 */
283 void blob_append(Blob *pBlob, const char *aData, int nData){
284 sqlite3_int64 nNew;
285 assert( aData!=0 || nData==0 );
286 blob_is_init(pBlob);
287 if( nData<0 ) nData = strlen(aData);
288 if( nData==0 ) return;
@@ -300,10 +307,23 @@
300 }
301 }
302 memcpy(&pBlob->aData[pBlob->nUsed], aData, nData);
303 pBlob->nUsed += nData;
304 pBlob->aData[pBlob->nUsed] = 0; /* Blobs are always nul-terminated */
 
 
 
 
 
 
 
 
 
 
 
 
 
305 }
306
307 /*
308 ** Append a single character to the blob
309 */
310
--- src/blob.c
+++ src/blob.c
@@ -277,12 +277,19 @@
277 pBlob->xRealloc = blobReallocStatic;
278 }
279
280 /*
281 ** Append text or data to the end of a blob.
282 **
283 ** The blob_append_full() routine is a complete implementation.
284 ** The blob_append() routine only works for cases where nData>0 and
285 ** no resizing is required, and falls back to blob_append_full() if
286 ** either condition is not met, but runs faster in the common case
287 ** where all conditions are met. The use of blob_append() is
288 ** recommended, unless it is known in advance that nData<0.
289 */
290 void blob_append_full(Blob *pBlob, const char *aData, int nData){
291 sqlite3_int64 nNew;
292 assert( aData!=0 || nData==0 );
293 blob_is_init(pBlob);
294 if( nData<0 ) nData = strlen(aData);
295 if( nData==0 ) return;
@@ -300,10 +307,23 @@
307 }
308 }
309 memcpy(&pBlob->aData[pBlob->nUsed], aData, nData);
310 pBlob->nUsed += nData;
311 pBlob->aData[pBlob->nUsed] = 0; /* Blobs are always nul-terminated */
312 }
313 void blob_append(Blob *pBlob, const char *aData, int nData){
314 sqlite3_int64 nUsed;
315 assert( aData!=0 || nData==0 );
316 blob_is_init(pBlob);
317 if( nData<=0 || pBlob->nUsed + nData >= pBlob->nAlloc ){
318 blob_append_full(pBlob, aData, nData);
319 return;
320 }
321 nUsed = pBlob->nUsed;
322 pBlob->nUsed += nData;
323 pBlob->aData[pBlob->nUsed] = 0;
324 memcpy(&pBlob->aData[nUsed], aData, nData);
325 }
326
327 /*
328 ** Append a single character to the blob
329 */
330

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button