繁体   English   中英

避免虚函数的标准方法

[英]A standard way to avoid virtual functions

我有一个库,里面有很多小对象,现在都有虚函数。 它达到了这样的程度,即指向虚函数表的指针的大小可能超过对象中有用数据的大小(它通常只是一个包含单个float的结构)。 对象是稀疏图上的数值模拟中的元素,因此不能轻易合并等。

我并不关心虚拟函数调用的成本 ,而是关注存储成本 发生的事情是指向虚函数表的指针基本上降低了缓存的效率。 我想知道我是否会更好地将类型id存储为整数,而不是虚函数。

我不能使用静态多态,因为我的所有对象都在一个列表中,我需要能够对由索引选择的项执行操作(这是一个运行时值 - 因此无法静态地确定类型)。

问题是:是否有设计模式或通用算法,可以从接口动态调用函数,给定类型列表(例如在类型列表中)和类型索引?

接口是定义的并且没有太大变化,但是将来会由(可能技能较低的)库用户声明新对象,并且这样做不需要很大的努力。 表现至关重要。 可悲的是,没有C ++ 11。

到目前为止,我可能有一个愚蠢的概念证明:

typedef MakeTypelist(ClassA, ClassB, ClassC) TList; // list of types

enum {
    num_types = 3 // number of items in TList
};

std::vector<CommonBase*> uniform_list; // pointers to the objects
std::vector<int> type_id_list; // contains type ids in range [0, num_types)

template <class Op, class L>
class Resolver { // helper class to make a list of functions
    typedef typename L::Head T;

    // specialized call to op.Op::operator ()<T>(p)
    static void Specialize(CommonBase *p, Op op)
    {
        op(*(T*)p);
    }

    // add a new item to the list of the functions
    static void BuildList(void (**function_list)(CommonBase*, Op))
    {
        *function_list = &Specialize;
        Resolver<Op, typename L::Tail>::BuildList(function_list + 1);
    }
};

template <class Op>
class Resolver<Op, TypelistEnd> { // specialization for the end of the list
    static void BuildList(void (**function_list)(CommonBase*, Op))
    {}
};

/**
 * @param[in] i is index of item
 * @param[in] op is a STL-style function object with template operator ()
 */
template <class Op>
void Resolve(size_t i, Op op)
{
    void (*function_list[num_types])(CommonBase*, Op);
    Resolver<Op, TList>::BuildList(function_list);
    // fill the list of functions using the typelist

    (*function_list[type_id_list[i]])(uniform_list[i], op);
    // call the function
}

我还没有查看过程序集,但我相信如果设置为静态,则可以免费实现函数指针数组的创建。 另一种方法是使用在类型列表上生成的二叉搜索树,这将启用内联。

我最终使用了我在问题中概述的“thunk table”概念。 对于每个操作,都有一个thunk表的实例(它是静态的并通过模板共享 - 因此编译器将自动确保每个操作类型只有一个表实例,而不是每个调用)。 因此,我的对象没有任何虚函数

最重要的是 - 使用简单函数指针而不是虚函数的速度增益可以忽略不计 (但也不会慢)。 获得大量速度的是实现决策树并静态链接所有函数 - 这将一些非计算密集型代码的运行时间提高了约40%

一个有趣的副作用是能够拥有“虚拟”模板功能,这通常是不可能的。

我需要解决的一个问题是我的所有对象都需要有一些接口,因为它们最终会被仿函数以外的一些调用访问。 我为此设计了一个独立的立面。 Facade是一个虚拟类,声明了对象的接口。 分离的外观是此虚拟类的实例,专用于给定的类(对于列表中的所有类, operator []返回所选项的类型的分离外观)。

class CDetachedFacade_Base {
public:
    virtual void DoStuff(BaseType *pthis) = 0;
};

template <class ObjectType>
class CDetachedFacade : public CDetachedFacade_Base {
public:
    virtual void DoStuff(BaseType *pthis)
    {
        static_cast<ObjectType>(pthis)->DoStuff();
        // statically linked, CObjectType is a final type
    }
};

class CMakeFacade {
    BaseType *pthis;
    CDetachedFacade_Base *pfacade;

public:
    CMakeFacade(BaseType *p, CDetachedFacade_Base *f)
        :pthis(p), pfacade(f)
    {}

    inline void DoStuff()
    {
        f->DoStuff(pthis);
    }
};

要使用它,需要做的是:

static CDetachedFacade<CMyObject> facade;
// this is generated and stored in a templated table
// this needs to be separate to avoid having to call operator new all the time

CMyObject myobj;
myobj.DoStuff(); // statically linked

BaseType *obj = &myobj;
//obj->DoStuff(); // can't do, BaseType does not have virtual functions

CMakeFacade obj_facade(obj, &facade); // choose facade based on type id
obj_facade.DoStuff(); // calls CMyObject::DoStuff()

这允许我在代码的高性能部分中使用优化的thunk表,并且仍然具有多态行为对象,以便能够在不需要性能的情况下方便地处理它们。

CRTP是虚函数的编译时替代方法:

    template <class Derived> 
    struct Base
    {
        void interface()
        {
            // ...
            static_cast<Derived*>(this)->implementation();
            // ...
        }

        static void static_func()
        {
            // ...
            Derived::static_sub_func();
            // ...
        }
    };

    struct Derived : Base<Derived>
    {
        void implementation();
        static void static_sub_func();
    };

它依赖于这样一个事实,即成员的定义在被调用之前不会被实例化。 所以Base应该仅在其成员函数的定义中引用Derived的任何成员,而不是在原型或数据成员中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM