From: Ralph Corderoy Date: Mon, 24 Apr 2017 20:19:56 +0000 (+0100) Subject: sbr/vector.c: calloc(3) rather than malloc(3) and memset(3). X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/2a4506992638b36cd56c3f3d563dba019306f5af?ds=inline sbr/vector.c: calloc(3) rather than malloc(3) and memset(3). calloc(3) tells libc upfront that the memory needs to be zeroed rather than giving it the news later with memset. Perhaps this allows it to allocate from a CoW page of zero bytes, or it benefits from some other way in memset not having to clear each bit, but it knocks about 15% off the wall-clock time and the number of library calls under ltrace(1). --- diff --git a/sbr/vector.c b/sbr/vector.c index 8765fc60..fc5cc00d 100644 --- a/sbr/vector.c +++ b/sbr/vector.c @@ -51,8 +51,7 @@ bvector_create (size_t init_size) { NEW(vec); bytes = BVEC_BYTES (vec, init_size ? init_size : VEC_INIT_SIZE); - vec->bits = mh_xmalloc (bytes); - memset (vec->bits, 0, bytes); + vec->bits = mh_xcalloc (1, bytes); vec->maxsize = bytes * Nbby; return vec; @@ -156,8 +155,7 @@ svector_create (size_t init_size) { NEW(vec); vec->maxsize = init_size ? init_size : VEC_INIT_SIZE; bytes = vec->maxsize * sizeof (char *); - vec->strs = mh_xmalloc (bytes); - memset (vec->strs, 0, bytes); + vec->strs = mh_xcalloc (1, bytes); vec->size = 0; return vec; @@ -240,8 +238,7 @@ ivector_create (size_t init_size) { NEW(vec); vec->maxsize = init_size ? init_size : VEC_INIT_SIZE; bytes = vec->maxsize * sizeof (int); - vec->ints = mh_xmalloc (bytes); - memset (vec->ints, 0, bytes); + vec->ints = mh_xcalloc (1, bytes); vec->size = 0; return vec;