I have a class that implements reference counting:
- It's sole data member is a pointer to an object containing an invasive reference count
- The "Rule of 5" methods are all declared noexcept
- The default constructor initializes the pointer data member to
nullptr - Move operations set the moved-from pointer data member to
nullptr - When the pointer data member is
nullptrthe destructor is effectively a nop
By instrumenting the methods I do see that growing the vector occurs by repeatedly move-constructing from an old object into new memory and then destroying that old object.
Intuitively, I known that growing the vector could be performed more efficiently as
- Allocate new, larger chunk of memory
- Block copy the contents of the old chunk to the front of the new chunk
- Install the new chunk
- Free the old chunk
Is there, in C++20, any magic incantation that can persuade std::vector to be this efficient?