The Unexpected Cost of Shared Pointers in C++

2023/08/23
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

The article discusses the unexpected cost of using shared pointers in C++. The author explains that while profiling a query's cold run, they discovered that memset calls were consuming a significant fraction of CPU time. The cause behind this observation is that the internal workings of Oxla, the company's system, utilize distributed object storage and cannot rely on file system caching. The author found that std::make_shared was setting the whole structure to 0 if no default constructor was provided, resulting in inefficient CPU usage. However, adding a constructor to the struct resolved the issue. The article concludes that while this may not be important for most developers, it is crucial to consider the performance implications of using shared pointers, especially for small objects. The author provides measurements to demonstrate the impact of allocations on runtime. Overall, this article highlights the importance of optimizing code for performance and offers insights into the cost of shared pointers in C++.