# HG changeset patch # User Mike Becker # Date 1487608138 -3600 # Node ID 7cf1e41833a25f55a8da2eed04cd1c5e85d899e8 # Parent 7a63b4986b5b68928ce903af4eedddc4624d387a reduces amount of realloc calls in sstrsplit diff -r 7a63b4986b5b -r 7cf1e41833a2 ucx/string.c --- a/ucx/string.c Mon Feb 20 17:12:14 2017 +0100 +++ b/ucx/string.c Mon Feb 20 17:28:58 2017 +0100 @@ -218,7 +218,8 @@ } ssize_t nmax = *n; - sstr_t* result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)); + size_t arrlen = 16; + sstr_t* result = (sstr_t*) almalloc(allocator, arrlen*sizeof(sstr_t)); if (result) { sstr_t curpos = s; @@ -250,17 +251,20 @@ /* allocate memory for the next string */ j++; - sstr_t* reallocated = (sstr_t*) - alrealloc(allocator, result, j*sizeof(sstr_t)); - if (reallocated) { - result = reallocated; - } else { - for (ssize_t i = 0 ; i < j-1 ; i++) { - alfree(allocator, result[i].ptr); + if (j > arrlen) { + arrlen *= 2; + sstr_t* reallocated = (sstr_t*) alrealloc( + allocator, result, arrlen*sizeof(sstr_t)); + if (reallocated) { + result = reallocated; + } else { + for (ssize_t i = 0 ; i < j-1 ; i++) { + alfree(allocator, result[i].ptr); + } + alfree(allocator, result); + *n = -2; + return NULL; } - alfree(allocator, result); - *n = -2; - return NULL; } } else { /* nmax reached, copy the _full_ remaining string */