Performance: default values for shared_ptr arguments


I’ve stumbled on a pretty interesting data point.
Most of the current compilers (clang 6/gcc 8.2/MSVC17) will likely re-create std::shared_ptr ( bs::SPtr ) instance for every call that has a default argument of the form const shared_ptr<T> &arg=nullptr
A mandatory godbolt comparison


void bind()
    RenderAPI& rapi = RenderAPI_instance();

will construct/destruct a null SPtr<CommandBuffer> 3 times.
whereas a call of the form:

void bind(const SPtr<CommandBuffer>& commandBuffer = nullptr)
    RenderAPI& rapi = RenderAPI_instance();

only constructs the null at the call site of bind


That’s very interesting indeed. I’ll think about it a bit and see what would be the best way to avoid this. Possibly by having a separate set of overloads or if possible pass the command buffer parameter as a raw ptr.


Another thing, it appears that std::bind can be slower then lambda and we have quite a few places that use std::bind :slight_smile:


Also, if You decide on any changes, post issues on github? I or someone else might want to take a stab at them.