]> diplodocus.org Git - nmh/commitdiff
sbr/vector.c: calloc(3) rather than malloc(3) and memset(3).
authorRalph Corderoy <ralph@inputplus.co.uk>
Mon, 24 Apr 2017 20:19:56 +0000 (21:19 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Wed, 26 Apr 2017 06:43:39 +0000 (07:43 +0100)
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).

sbr/vector.c

index 8765fc6..fc5cc00 100644 (file)
@@ -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;