User Tools

Site Tools


cplusplus:goingnative2013

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
cplusplus:goingnative2013 [2013/09/18 10:14] dblumecplusplus:goingnative2013 [2022/10/06 00:39] – [Advanced Optimization] whitespace only dblume
Line 67: Line 67:
 CINDER_APP_BASIC( MyApp, RendererDx ) CINDER_APP_BASIC( MyApp, RendererDx )
 </file> </file>
 +
 +===== Advanced Optimization =====
 +
 +Use lambda in place of std::bind to save memory. A std::bind() on a pointer-to-member-function will allocate 24 bytes on the heap for the bind structure. If instead you use a lambda, then this fits into 4 bytes and avoids any allocation.
 +
 +Replace:
 +
 +    g(this, std::bind(&m_f, this)),
 +
 +With:
 +
 +    g(this, [this](){this->m_f();}),
 +
 +Note that subtle changes to object lifetimes is possible. This code:
 +
 +    std::bind(f, std::weak_ptr<Object>(n));
 +
 +is not the same as this code:
 +
 +    [n]{ f(std::weak_ptr<Object>(n)); }
 +
 +The above code will bind the shared_ptr<Node> into the lambda's capture, extending the lifetime.
 +But it is the same as this:
 +
 +    [wp = std::weak_ptr<Object>(n)]{ f(wp); };
 +
 +In this case the lambda's capture only has the weak pointer.
cplusplus/goingnative2013.txt · Last modified: 2023/04/12 20:44 by 127.0.0.1