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
This commit is contained in:
Hans Goudey
2025-06-02 22:18:55 +02:00
committed by Hans Goudey
parent 383b3fd678
commit d94ef63cb3

View File

@@ -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;