From d94ef63cb3d5206a5c70c1a99a77747c887f23ee Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 2 Jun 2025 22:18:55 +0200 Subject: [PATCH] Allocator: Use calloc when alignment is compatible calloc is generally faster than zeroing separately after a regular allocation. Our allocator API exposed an allocation call with "calloc" in the name that didn't actually use "calloc" because it had an alignment argument (there is no standardized calloc-with-alignment provided by the OS). However, we can still use calloc internally if the alignment fits within the default. That just aligns the function better with performance expectations. Pull Request: https://projects.blender.org/blender/blender/pulls/139749 --- intern/guardedalloc/intern/mallocn_lockfree_impl.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/intern/guardedalloc/intern/mallocn_lockfree_impl.cc b/intern/guardedalloc/intern/mallocn_lockfree_impl.cc index 438bc1c14ef..a1528481a58 100644 --- a/intern/guardedalloc/intern/mallocn_lockfree_impl.cc +++ b/intern/guardedalloc/intern/mallocn_lockfree_impl.cc @@ -498,9 +498,13 @@ void *MEM_lockfree_calloc_arrayN_aligned(const size_t len, const size_t alignment, const char *str) { + /* There is no lower level #calloc with an alignment parameter, so unless the alignment is less + * than or equal to what we'd get by default, we have to fall back to #memset unfortunately. */ + if (alignment <= MEM_MIN_CPP_ALIGNMENT) { + return MEM_lockfree_calloc_arrayN(len, size, str); + } + size_t bytes_num; - /* There is no lower level #calloc with an alignment parameter, so we have to fallback to using - * #memset unfortunately. */ void *ptr = mem_lockfree_malloc_arrayN_aligned(len, size, alignment, str, bytes_num); if (!ptr) { return nullptr;