diff --git a/5-UsefulConceptsAndSTL/6-SmartPointers.ipynb b/5-UsefulConceptsAndSTL/6-SmartPointers.ipynb index 26ead5b06b9aa7e1006f0158c0ff62498d226f2b..4b54bd133cea1cb681d17b6d3a22a729ab2e7b7a 100644 --- a/5-UsefulConceptsAndSTL/6-SmartPointers.ipynb +++ b/5-UsefulConceptsAndSTL/6-SmartPointers.ipynb @@ -561,6 +561,23 @@ "This simplifies the reading, especially if templates are also involved... " ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## The cost of using smart pointers\n", + "\n", + "This [article](https://www.modernescpp.com/index.php/memory-and-performance-overhead-of-smart-pointer) provides an analysis of the cost involved both in performance and memory when using a smart pointer. To put in the nutshell, its conclusions are:\n", + "\n", + "- `std::unique_ptr` bears no overhead in memory (except in a very edge case you can safely ignore for now) and little overhead in performance, at least for the latter when compiler optimizations are enabled (we will talk about them in a later [notebook](../6-InRealEnvironment/3-Compilers.ipynb)).\n", + "- `std::shared_ptr` incurs a memory overhead (to keep the reference count) and a performance overhead as well (which is partially mitigated if the memory was allocated through `std::make_shared`). The performance overhead is especially important when no optimizations are involved.\n", + "\n", + "This highlights once more what we said earlier:\n", + "\n", + "- Use smart pointers: RAII is simply too precious to manage properly your ressources!\n", + "- Use `std::unique_ptr` wherever you can, and use `std::shared_ptr` when you really need several instances of the same ressource.\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -628,5 +645,5 @@ } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }