From e58e0a3f75a409690da964fe717e2686f309b313 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 18 May 2015 10:49:48 -0600 Subject: [PATCH] Update 07-Considering_Performance.md --- 07-Considering_Performance.md | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/07-Considering_Performance.md b/07-Considering_Performance.md index ed0138b..820b7c1 100644 --- a/07-Considering_Performance.md +++ b/07-Considering_Performance.md @@ -33,6 +33,12 @@ template class MyTemplatedType; This is a proactive approach to simplify compilation time and rebuilding dependencies. +### Avoid Unnecessary Template Instantiations + +Templates are not free to instantiate. Instantiating many templates, or templates with more code than necessary increases compiled code size and build time. + +For more examples see [this article](http://blog2.emptycrate.com/content/template-code-bloat-revisited-smaller-makeshared). + ### Firewall Frequently Changing Header Files @@ -132,6 +138,36 @@ would suffice. However, MSVC2013 doesn’t seem to like this code yet. shared_ptr objects are much more expensive to copy than you think they should be. This is because the reference count must be atomic and thread safe. So this comment just re-enforces the note above - avoid temporaries and too many copies of objects. Just because we are using a pImpl it does not mean our copies are free. +### Reduce Copies and Reassignments as Much as Possible + +This can be facilited in some cases with an [immediate-invoked lambda](http://blog2.emptycrate.com/content/complex-object-initialization-optimization-iife-c11). + +```c++ +// Bad Idea +std::string somevalue; + +if (somecase()) { + somevalue = "Value A"; +} else { + somevalue = "Value B"; +} +``` + +```c++ +// Better Idea +const std::string somevalue = [&](){ + if (somecase()) { + return "Value A"; + } else { + return "Value B"; + } + }(); +``` + +### Avoid Excess Exceptions + +Exceptions which are thrown and captured internally during normal processing slow down the application execution. They also destroy the user experience from within a debugger, as debuggers monitor and report on each exception event. It is best to just avoid internal exception processing when possible. + ### Get rid of “new” We already know that we should not be using raw memory access, so we are using `unique_ptr` and `shared_ptr` instead, right?