Windows: Use TBBMalloc for GMP

TBBmalloc_proxy already takes care of any allocations
being done from MSVC compiled code, some of the dependencies
like GMP cannot be build with MSVC and we have to use
mingw to build them. mingw however links against the older
msvcrt.dll for its allocation needs, which TBBMallocProxy
does not hook.

GMP has an option to supply your own allocation functions
so we can still manually redirect them to TBBMalloc.

In a test-file with a boolean geometry node, this patch
uses 32s effective CPU time compared to 52s before.

Differential Revision: https://developer.blender.org/D11435

Reviewed by Campbell Barton, Ray Molenkamp
This commit is contained in:
Erik Abrahamsson 2021-06-09 18:34:17 -06:00 committed by Ray Molenkamp
parent a3226bdf3e
commit 4f81b4b4ce
Notes: blender-bot 2023-02-14 05:25:44 +01:00
Referenced by commit 9df1e0cad5, Fix: Build error with MSVC
2 changed files with 41 additions and 0 deletions

View File

@ -116,6 +116,7 @@ if(WITH_XR_OPENXR)
endif()
if(WITH_GMP)
blender_include_dirs(${GMP_INCLUDE_DIRS})
add_definitions(-DWITH_GMP)
endif()
@ -909,6 +910,7 @@ elseif(WIN32)
DESTINATION "."
CONFIGURATIONS Debug
)
list(APPEND LIB ${TBB_MALLOC_LIBRARIES})
endif()
if(WITH_CODEC_SNDFILE)

View File

@ -209,6 +209,41 @@ char **environ = NULL;
/** \} */
/* -------------------------------------------------------------------- */
/** \name GMP Allocator Workaround
* \{ */
#if (defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_GMP)) || \
defined(DOXYGEN)
# include "gmp.h"
# include "tbb/scalable_allocator.h"
void *gmp_alloc(size_t size)
{
return scalable_malloc(size);
}
void *gmp_realloc(void *ptr, size_t old_size, size_t new_size)
{
return scalable_realloc(ptr, new_size);
}
void gmp_free(void *ptr, size_t size)
{
scalable_free(ptr);
}
/**
* Use TBB's scalable_allocator on Windows.
* TBBmalloc correctly captures all allocations already,
* however, GMP is built with mingw since it doesn't build with msvc,
* which TBB has issues hooking into automatically.
*/
void gmp_blender_init_allocator()
{
mp_set_memory_functions(gmp_alloc, gmp_realloc, gmp_free);
}
#endif
/** \} */
/* -------------------------------------------------------------------- */
/** \name Main Function
* \{ */
@ -342,6 +377,10 @@ int main(int argc,
CCL_init_logging(argv[0]);
#endif
#if defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_GMP)
gmp_blender_init_allocator();
#endif
main_callback_setup();
#if defined(__APPLE__) && !defined(WITH_PYTHON_MODULE) && !defined(WITH_HEADLESS)