Note: The cache-line is not the same size on every platform, nor is the size of a pointer always the same.. be careful about making assumptions based on the numbers in the question.
Why std::make_shared
?
std::make_shared
exists for three (main) reasons;
- Means to allocate memory for both the ref-counter, and the object being tracked at once (generally memory allocations are expensive);
- an exception-safe way to construct and initialize a
std::shared_ptr
;
- and code brevity.
What about the cache-line and std::make_shared
?
Honestly this is beyond the scope and purpose of std::make_shared
. The C++ Standard has no idea of what a "cache-line" is, and the design described in the standard is written in such matter that it's not targetting any specific platform.
Even if there will be *cache-misse*s because the ref-counter and the object can't fit inside the same cache-line, we still have all the benefits previously listed, and std::make_shared
still does the job it's intended to solve.
Note: One could say that "keeping the ref-counter and the object close together in memory" is just a sweet little bonus.