From: Ralph Corderoy Date: Wed, 26 Apr 2017 23:14:28 +0000 (+0100) Subject: sbr/vector.c: Zero the growth with memset(3), not loop. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/c8caafcb4a39516eaec330f9ea229ea52e5fe46b?hp=f77e0b578f71edb6f9105d5af8d54c8237cd9078 sbr/vector.c: Zero the growth with memset(3), not loop. When the resize functions grow the vectors, as they always do, zero the new slots with memset(3) rather than a for loop. Particularly of note for the bit vector where it was bvector_clear()ing one bit at a time. Although a NULL pointer needn't have a representation of all-zero bits, the code was already assuming that, e.g. on the initial allocation, so we're no worse off. --- diff --git a/sbr/vector.c b/sbr/vector.c index 1167fa24..3bf4c1f0 100644 --- a/sbr/vector.c +++ b/sbr/vector.c @@ -29,6 +29,8 @@ * constants in the code below must also be changed to a 1 that's at * least as wide as the new type. */ + +/* The *sizeof* struct bvector's bits member. Not its size in bits. */ #define BVEC_SIZEOF_BITS (sizeof *(((bvector_t)NULL)->bits)) #define BVEC_WORD(max) ((max) / (BVEC_SIZEOF_BITS * CHAR_BIT)) #define BVEC_OFFSET(max) ((max) % (BVEC_SIZEOF_BITS * CHAR_BIT)) @@ -127,7 +129,6 @@ static void bvector_resize (bvector_t vec, size_t maxsize) { size_t old_maxsize = vec->maxsize; size_t bytes; - size_t i; while ((vec->maxsize *= 2) < maxsize) ; @@ -137,8 +138,9 @@ bvector_resize (bvector_t vec, size_t maxsize) { memcpy(vec->bits, vec->tiny, sizeof vec->tiny); } else vec->bits = mh_xrealloc(vec->bits, bytes); - for (i = old_maxsize; i < vec->maxsize; ++i) - bvector_clear (vec, i); + + memset(vec->bits + (old_maxsize / (BVEC_SIZEOF_BITS * CHAR_BIT)), + 0, (vec->maxsize - old_maxsize) / CHAR_BIT); } unsigned long @@ -220,13 +222,12 @@ svector_size (svector_t vec) { static void svector_resize (svector_t vec, size_t maxsize) { size_t old_maxsize = vec->maxsize; - size_t i; while ((vec->maxsize *= 2) < maxsize) ; vec->strs = mh_xrealloc (vec->strs, vec->maxsize * sizeof (char *)); - for (i = old_maxsize; i < vec->maxsize; ++i) - vec->strs[i] = NULL; + memset(vec->strs + old_maxsize, 0, + (vec->maxsize - old_maxsize) * sizeof *vec->strs); } @@ -282,11 +283,10 @@ ivector_atp (ivector_t vec, size_t i) { static void ivector_resize (ivector_t vec, size_t maxsize) { size_t old_maxsize = vec->maxsize; - size_t i; while ((vec->maxsize *= 2) < maxsize) ; vec->ints = mh_xrealloc (vec->ints, vec->maxsize * sizeof (int)); - for (i = old_maxsize; i < vec->maxsize; ++i) - vec->ints[i] = 0; + memset(vec->ints + old_maxsize, 0, + (vec->maxsize - old_maxsize) * sizeof *vec->ints); }