From: Kevin Day Date: Sun, 12 Dec 2021 05:59:06 +0000 (-0600) Subject: Update: Improve performance by removing redundant memset(). X-Git-Tag: 0.5.7~42 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=c567ef798e17b0c91b4eff17157140c175b71f84;p=fll Update: Improve performance by removing redundant memset(). The calloc() program is supposed to guarantee 0 filled data. Either the libc or the kernel know how to optimize this automatically using numerous tricks based on architecture or lack thereof. This makes calloc() potentially faster than malloc()+memset(). Calling calloc()+memset() is just ridiculous. Remove the calls to memset() that follow a calloc() call. This is guaranteed to be a performance increase (but how much? I didn't bother trying to find out). Cleanup some comments. --- diff --git a/level_0/f_memory/c/memory.c b/level_0/f_memory/c/memory.c index 1d03bda..9bc3121 100644 --- a/level_0/f_memory/c/memory.c +++ b/level_0/f_memory/c/memory.c @@ -26,7 +26,7 @@ extern "C" { if (!pointer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - // prevent double-frees. + // Prevent double-frees. if (!*pointer || !size) { return F_data_not; } @@ -39,7 +39,7 @@ extern "C" { free(*pointer); - // it has been deallocated, so reset the pointer. + // It has been deallocated, so reset the pointer. *pointer = 0; return F_none; @@ -53,7 +53,7 @@ extern "C" { if (!pointer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - // prevent double-frees. + // Prevent double-frees. if (!*pointer) { return F_data_not; } @@ -66,7 +66,7 @@ extern "C" { free(*pointer); - // it has been deallocated, so reset the pointer. + // It has been deallocated, so reset the pointer. *pointer = 0; return F_none; @@ -85,16 +85,9 @@ extern "C" { return F_data_not; } - // Some people use malloc(size * length) to produce the same results. - // This has been observed to sometimes causes an increase in L1/L2 cache misses (0.02% L1 increase, 0.01% L2 increase). *pointer = calloc(length, size); if (*pointer) { - - // uint8_t * is of a data size size of 1, casting it to uint8_t should result in a single-length increment. - // this is done to avoid problems with (void *) having arithmetic issues. - memset(((uint8_t *) *pointer), 0, size * length); - return F_none; } @@ -110,7 +103,7 @@ extern "C" { if (!pointer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - // prevent double-allocations. + // Prevent double-allocations. if (*pointer || !length) { return F_data_not; } @@ -139,8 +132,8 @@ extern "C" { } #endif // _f_memory_USE_posix_memalign_ - // uint8_t * is of a data size size of 1, casting it to uint8_t should result in a single-length increment. - // this is done to avoid problems with (void *) having arithmetic issues. + // uint8_t * is of a data size of 1, casting it to uint8_t should result in a single-length increment. + // This is done to avoid problems with (void *) having arithmetic issues. memset(((uint8_t *) *pointer), 0, length); return F_none; diff --git a/level_0/f_memory/c/private-memory.c b/level_0/f_memory/c/private-memory.c index 271021a..8b5761c 100644 --- a/level_0/f_memory/c/private-memory.c +++ b/level_0/f_memory/c/private-memory.c @@ -17,7 +17,7 @@ extern "C" { if (length_new < length_old) { // uint8_t * is of a data size size of 1, casting it to uint8_t should result in a single-length increment. - // this is done to avoid problems with (void *) having arithmetic issues. + // This is done to avoid problems with (void *) having arithmetic issues. memset(((uint8_t *) *pointer) + length_new, 0, type_size * (length_old - length_new)); } } @@ -29,7 +29,7 @@ extern "C" { if (length_new > length_old) { // uint8_t * is of a data size size of 1, casting it to bool should result in a single-length increment. - // this is done to avoid problems with (void *) having arithmetic issues. + // This is done to avoid problems with (void *) having arithmetic issues. memset(((uint8_t *) new_pointer) + (type_size * length_old), 0, type_size * (length_new - length_old)); } @@ -43,7 +43,7 @@ extern "C" { else { free(*pointer); - // assure that the pointer is always 0 after deallocation. + // Assure that the pointer is always 0 after deallocation. *pointer = 0; return F_none; @@ -53,8 +53,6 @@ extern "C" { *pointer = calloc(type_size, length_new); if (*pointer) { - memset(*pointer, 0, type_size * length_new); - return F_none; } } @@ -81,7 +79,7 @@ extern "C" { if (length_new > length_old) { // uint8_t * is of a data size size of 1, casting it to bool should result in a single-length increment. - // this is done to avoid problems with (void *) having arithmetic issues. + // This is done to avoid problems with (void *) having arithmetic issues. memset(((uint8_t *) new_pointer) + (type_size * length_old), 0, type_size * (length_new - length_old)); } @@ -95,7 +93,7 @@ extern "C" { else { free(*pointer); - // assure that the pointer is always 0 after deallocation. + // Assure that the pointer is always 0 after deallocation. *pointer = 0; return F_none; @@ -105,8 +103,6 @@ extern "C" { *pointer = calloc(type_size, length_new); if (*pointer) { - memset(*pointer, 0, type_size * length_new); - return F_none; } }