Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2022-05-25 17:28:07 +0300
committerJacques Lucke <jacques@blender.org>2022-05-25 17:28:07 +0300
commita337e7738fbf2ec1eaec4c1deb46d28840885758 (patch)
treeb7b3d436363d8ab07c9fbfa0067233c1c604b652 /source/blender/blenlib/BLI_memory_utils.hh
parentf381c31ac69cb8a6175a54dc31863e13e9b199d6 (diff)
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
Diffstat (limited to 'source/blender/blenlib/BLI_memory_utils.hh')
-rw-r--r--source/blender/blenlib/BLI_memory_utils.hh24
1 files changed, 15 insertions, 9 deletions
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<typename T> using destruct_ptr = std::unique_ptr<T, DestructValueAtAddr
* An `AlignedBuffer` is a byte array with at least the given size and alignment. The buffer will
* not be initialized by the default constructor.
*/
-template<size_t Size, size_t Alignment> class alignas(Alignment) AlignedBuffer {
- private:
- /* Don't create an empty array. This causes problems with some compilers. */
- char buffer_[(Size > 0) ? Size : 1];
+template<size_t Size, size_t Alignment> 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<Size == 0, Empty, Sized>;
+ 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<size_t Size, size_t Alignment> class alignas(Alignment) AlignedBuffer {
*/
template<typename T, int64_t Size = 1> class TypedBuffer {
private:
- AlignedBuffer<sizeof(T) * (size_t)Size, alignof(T)> buffer_;
+ BLI_NO_UNIQUE_ADDRESS AlignedBuffer<sizeof(T) * (size_t)Size, alignof(T)> buffer_;
public:
operator T *()