目前,我正在通过下面介绍的函数MyClass::Enable存储函数调用。从某种意义上说,它是通用的,我稍后可以通过循环遍历commanList在消费者线程上回放该函数,而不必提供任何函数参数。
然而,通过快速性能检查,与使用函数查找表(这会破坏通用部分)并存储函数参数以及函数查找位置相比,此方法完成存储函数所需的处理时间要长 2 倍。(如果这没有意义,请道歉:它本质上是一个功能记录器)。
是否有更性能友好的 std::tr1::bind [boost::bind] 版本,或者是否有更好的方法以通用方式记录函数调用并稍后播放?
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);
}
};