From a337e7738fbf2ec1eaec4c1deb46d28840885758 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 25 May 2022 16:28:07 +0200 Subject: BLI: use no_unique_address attribute Even though the `no_unique_address` attribute has only been standardized in C++20, compilers seem to support it with C++17 already. This attribute allows reducing the memory footprint of structs which have empty types as data members (usually that is an allocator or inline buffer in Blender). Previously, one had to use the empty base optimization to achieve the same effect, which requires a lot of boilerplate code. The types that benefit from this the most are `Vector` and `Array`, which usually become 8 bytes smaller. All types which use these core data structures get smaller as well of course. Differential Revision: https://developer.blender.org/D14993 --- source/blender/blenlib/BLI_memory_utils.hh | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'source/blender/blenlib/BLI_memory_utils.hh') diff --git a/source/blender/blenlib/BLI_memory_utils.hh b/source/blender/blenlib/BLI_memory_utils.hh index d7c41ae88a8..940542c9f1d 100644 --- a/source/blender/blenlib/BLI_memory_utils.hh +++ b/source/blender/blenlib/BLI_memory_utils.hh @@ -317,30 +317,36 @@ template using destruct_ptr = std::unique_ptr class alignas(Alignment) AlignedBuffer { - private: - /* Don't create an empty array. This causes problems with some compilers. */ - char buffer_[(Size > 0) ? Size : 1]; +template class AlignedBuffer { + struct Empty { + }; + struct alignas(Alignment) Sized { + /* Don't create an empty array. This causes problems with some compilers. */ + std::byte buffer_[Size > 0 ? Size : 1]; + }; + + using BufferType = std::conditional_t; + BLI_NO_UNIQUE_ADDRESS BufferType buffer_; public: operator void *() { - return buffer_; + return this; } operator const void *() const { - return buffer_; + return this; } void *ptr() { - return buffer_; + return this; } const void *ptr() const { - return buffer_; + return this; } }; @@ -351,7 +357,7 @@ template class alignas(Alignment) AlignedBuffer { */ template class TypedBuffer { private: - AlignedBuffer buffer_; + BLI_NO_UNIQUE_ADDRESS AlignedBuffer buffer_; public: operator T *() -- cgit v1.2.3