Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small Vector Optimization for dynamic_array #440

Open
devshgraphicsprogramming opened this issue Dec 13, 2022 · 0 comments
Open

Small Vector Optimization for dynamic_array #440

devshgraphicsprogramming opened this issue Dec 13, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@devshgraphicsprogramming
Copy link
Member

devshgraphicsprogramming commented Dec 13, 2022

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

@devshgraphicsprogramming devshgraphicsprogramming added the enhancement New feature or request label Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant