1

At the moment I am storing a function call via the function MyClass::Enable that I've presented below. It's generic in the sense that I can later play the function back on a consumer thread by looping through the commanList and not have to provide any function arguments.

However through a quick performance check this method takes 2x longer in processing time to complete the stored function when compared to using a function lookup table (which breaks the generic part) and storing the function arguments as well as the function lookup position. (apologies if this does not make sense : its essentially a function recorder).

Is there a more performance friendly version of std::tr1::bind [boost::bind], or is there a better way of recording a function call in a generic manner and playing it back later ?

class CVertexFormatGLPtr
{
   void Enable(size_t a, size_t b);
   void Disable();
};

class MyClass
{
public:
   typedef std::function<void ()> command;
   typedef std::list<command> commandList;

   // This is a typedef of CVertexFormatPtr & CVertexFormatGLPtr
   VertexFormatMap& m_vertexFormats;

   void Enable(const CVertexFormatPtr& pVFormat, size_t a, size_t b)
   {
      // search to find if vertex buffer is active
      auto itor = m_vertexFormats.find(pVFormat);
      CVertexFormatGLPtr pVFormatGL = CVertexFormatGLPtr();
      if ( itor != m_vertexFormats.end() )
      {
         pVFormatGL = (*itor).second;
      }

      std::function<void()> zeroArg = std::bind(&CVertexFormatGL::Enable, pVFormatGL, a, b);
      m_renderCommands.push_back(zeroArg); 
   }

};
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sent1nel
  • 509
  • 1
  • 7
  • 21
  • `std::bind` is very fast; `std::function<>` is the slow part, as it necessarily uses type erasure internally. – ildjarn Apr 25 '11 at 15:16
  • 1
    Have you tried it with lambdas instead of std::bind? –  Apr 25 '11 at 15:17
  • I'm sure its not `std::function` thats the slow part. It only calls the `operator ()` and if I change it for the fast delegates `fd::delegate` I get exactly the same performance. It is the return type of `bind` that is slow. – Sent1nel Apr 25 '11 at 15:26
  • @Grigory : How would I perform this with Lambda's? I thought Lambda's were only alternatives to function objects (at the time of declaration)? Could you link me to an example of storing function state using a lambda? – Sent1nel Apr 25 '11 at 15:29
  • 2
    @Sent1nel : Instead of `std::bind(&CVertexFormatGL::Enable, pVFormatGL, a, b);` do `[=]() { pVFormatGL.Enable(a, b); }`. – ildjarn Apr 25 '11 at 15:33
  • @lidjarn : Thanks I used the lambda method instead and gave it a little performance test. Good news, it completed the loop in 360 ms as opposed to the `bind` method which took 437 ms. I also tested the same implementation using the fast delegate and boost::function as opposed to std::function. Oddly enough, std::function was faster than both by 30 - 50 ms on average. – Sent1nel Apr 25 '11 at 15:59

0 Answers0