From 3e0df5d2d75326f49f5bad55f870cb927a0e6802 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 8 Oct 2025 12:23:22 +0200 Subject: [PATCH] BLI: inline fast case of CacheMutex.ensure I can measure a few percent speedup by doing that in the file from #146949. Pull Request: https://projects.blender.org/blender/blender/pulls/147608 --- source/blender/blenlib/BLI_cache_mutex.hh | 12 +++++++++++- source/blender/blenlib/intern/cache_mutex.cc | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/source/blender/blenlib/BLI_cache_mutex.hh b/source/blender/blenlib/BLI_cache_mutex.hh index 884984cc5fb..5dbb8efd3c7 100644 --- a/source/blender/blenlib/BLI_cache_mutex.hh +++ b/source/blender/blenlib/BLI_cache_mutex.hh @@ -82,7 +82,14 @@ class CacheMutex { * This function is thread-safe under the assumption that the same parameters are passed from * every thread. */ - void ensure(FunctionRef compute_cache); + void ensure(const FunctionRef compute_cache) + { + /* Handle fast case when the cache is up-to-date. */ + if (cache_valid_.load(std::memory_order_acquire)) { + return; + } + this->ensure_impl(compute_cache); + } /** * Reset the cache. The next time #ensure is called, it will recompute that code. @@ -107,6 +114,9 @@ class CacheMutex { { return cache_valid_.load(std::memory_order_relaxed); } + + private: + void ensure_impl(FunctionRef compute_cache); }; } // namespace blender diff --git a/source/blender/blenlib/intern/cache_mutex.cc b/source/blender/blenlib/intern/cache_mutex.cc index 4c83f31ed04..bc3cdfd1ee7 100644 --- a/source/blender/blenlib/intern/cache_mutex.cc +++ b/source/blender/blenlib/intern/cache_mutex.cc @@ -11,7 +11,7 @@ namespace blender { -void CacheMutex::ensure(const FunctionRef compute_cache) +void CacheMutex::ensure_impl(const FunctionRef compute_cache) { if (cache_valid_.load(std::memory_order_acquire)) { return;