Nabla icon indicating copy to clipboard operation
Nabla copied to clipboard

Small Vector Optimization for `dynamic_array`

Open devshgraphicsprogramming opened this issue 2 years ago • 0 comments

Description

Currently the dynamic_array is only possible to be created on the heap, we'd like for it to be creatable on the stack as well.

But its a base for smart_refctd_dynamic_array which strictly disallows copies and moves (except of the handle) so it always needs to live on the heap.

Therefore we need a unique_dynamic_array.

Description of the related problem

For use cases see CVulkanCommandBuffer which simply declares stack arrays

Solution proposal

First of all the base classes of dynamic_array should be rewritten to allow for:

  1. Union of allocator state with compile time fixed count item storage
  2. choose the compile time fixed item count
  3. make the allocator a && or be perfectly forwarded

We'd rewrite the thing to something akin to this

template<class T, size_t SmallVectorSize=1023/sizeof(T)+1, class allocator=core::allocator<T>, typename... OverAlignmentTypes>
class alignas(ResolveAlignment<allocator,AlignedBase<T>,AlignedBase<alignof(OverAlignmentTypesTypes)>...>) dynamic_array_base
{
   protected:
      dynamic_array_base(allocator&& _allocator, size_t _itemCount) : m_allocator(std::move(_allocator)), m_itemCount(_itemCount) {}
   
      allocator m_allocator;
      size_t m_itemCount;
      alignas(ResolveAlignment<allocator,AlignedBase<T>,AlignedBase<alignof(OverAlignmentTypesTypes)>...>) union
      {
      T m_data[SmallVectorSize];
      };
      
   public:
	_NBL_NO_COPY_FINAL(dynamic_array_base);
	_NBL_NO_MOVE_FINAL(dynamic_array_base);
};

Then the following should be constructible on the stack

unique_dynamic_array<VkBuffer> buffers(bufferCount);

Additional context (Visual Studio)

MSVC finally follows the C++ standard so gccHappyVar can be renamed just to _allocator cause its finally needed

@Przemog1 made a Visual Studio debugger visualizer, now its needs to be updated to properly display the data https://github.com/Devsh-Graphics-Programming/Nabla/blob/a16fec199ca775bfc41a0f8e03aff90ff37197bb/tools/debug/VisualStudio/DynamicArrayVisualizer.natvis