Home > database >  Determining size at which to switch from stack to heap allocation for multidimensional arrays
Determining size at which to switch from stack to heap allocation for multidimensional arrays

Time:07-19

I am developing a C library focused on multidimensional arrays and relevant operations involving these objects. The data for my "Tensor<T,n>" class (which corresponds to an n-dimensional array whose elements are of some numeric type T) is stored in a std::vector object and the elements are accessed via indices by calculating the appropriate index in the one-dimensional data vector using the concept of strides.

I understand that stack allocation is faster than heap allocation, and is generally safer. However, I also understand that heap allocation may be necessary for incredibly large data structures, and that users of this library may need to work with incredibly large data structures. My conclusion from this information is twofold:

  1. for relatively small multidimensional arrays (in terms of number of elements), I should allocate this information on the stack.
  2. for relatively large multidimensional arrays (in terms of number of elements), I should allocate this information on the heap.

I argue that this conclusion implies the existence of a "breaking point" as the size of a hypothetical array increases, at which I should switch from stack allocation to heap allocation. However, I have not been successful in finding resources that might assist me in determining where exactly this "breaking point" could be implemented to optimize efficiency for my user.

Assuming my conclusion is correct, how can I rigorously determine when to switch between the two types of allocation?

CodePudding user response:

A std::vector has a constant size and allocates the actual data always on the heap. So no matter what matrix size you have the Matrix class will be constant size and always store data on the heap.

If you want a heap-free version then you would have to implement a Matrix with std::array. You could use if constexpr to choose between vector and array.

Note that heap allocation cost some time. But they also allow move semantic. The "break even" point might be as little as 4x4 matrixes but you have to test that. new/delete isn't that expensive if it can reuse memory and doesn't have to ask the kernel for more.

Oh, and there is also the option of using a custom allocator for the vector.

  • Related