c – 如何在此上下文中使用CRTP删除虚拟方法?

我有以下类似于我的代码库的程序.一个FunctionState类,它执行某种算法(可能在多个线程中),以及一个Function类,它控制FunctionState类的使用方式,并可能进行一些算法设置/拆卸操作.

#include <iostream>
#include <vector>

class FunctionState;

class Function {
public:
    virtual FunctionState* NewFunctionState() = 0;

protected:
    std::vector<FunctionState*> states;
};

class FunctionState {
public:
    FunctionState(Function* func) : mFunc(func) {}

    virtual void RunState() = 0;
    void ExecuteFunctionLotsAndLotsOfTimes();

private:
    Function* mFunc;
};

#define VERY_BIG_NUMBER 10

void FunctionState::ExecuteFunctionLotsAndLotsOfTimes() {
    for(int i = 0; i < VERY_BIG_NUMBER; ++i) {
        RunState();
    }
};

class PrintFunction : public Function {
    FunctionState* NewFunctionState();
};

class PrintFunctionState : public FunctionState {
public:
    PrintFunctionState(PrintFunction* func) : FunctionState(func) {}

    void RunState() override {
        std::cout << "in print function state" << '\n';
    }
};

FunctionState* PrintFunction::NewFunctionState() {
    FunctionState* state = new PrintFunctionState(this);
    states.push_back(state);
    return state;
}

class AddFunction : public Function {
    FunctionState* NewFunctionState();
};

class AddFunctionState : public FunctionState {
public:
    AddFunctionState(AddFunction* func) : FunctionState(func), x(0) {}

    void RunState() override {
        ++x;
    }
private:
    int x;
};

FunctionState* AddFunction::NewFunctionState() {
    FunctionState* state = new AddFunctionState(this);
    states.push_back(state);
    return state;
}


int main() {
    Function* func = new PrintFunction();
    Function* func2 = new AddFunction();
    std::vector<Function*> vec = {func, func2};

    for(auto& func : vec) {
        func->NewFunctionState()->ExecuteFunctionLotsAndLotsOfTimes();
    }

    return 0;
}

现在我已经分析了我的代码,并且已经看到FunctionState :: ExecuteFunctionLotsAndLotsOfTimes()有一个热点.问题是这个函数循环很多次并调用RunState(),它是FunctionState类的虚函数.在那里,我执行了许多操作,这些操作可能会清除L1缓存中的vtable指针,导致循环的每次迭代都会丢失L1缓存.

所以我想删除虚拟呼叫的需要.我决定用CRTP做一个好方法. FunctionState类将获取实现它的类的类型的模板参数,并调用它的适当方法,不需要对RunState()进行虚拟调用.

现在,当我尝试将其移至CRTP时,我遇到了Function类的一些问题:

>我如何转发声明FunctionState类(因为它现在是模板化的)?
>我是否还需要向Function类添加模板参数?

3.如果我模板化,那么Function对象的构造会是什么样子?如何删除使用Function对象指定类型参数的类的需要?

请注意,这只是我真实代码库的一个简单版本.真正的代码库是10K行代码(不是无法管理的,但完全重写是不可能的).

此外,如果有另一种方法来删除不涉及CRTP的RunState()的虚拟调用,那么这也是值得赞赏的.

我尝试使用CRTP:

#include <iostream>
#include <vector>

class Function;

template<class T>
class FunctionState {
public:
    FunctionState(Function* func) : mFunc(func) {}

    void RunState() {
        static_cast<T*>(this)->RunState();
    };

    void ExecuteFunctionLotsAndLotsOfTimes();
private:
    Function* mFunc;
};

class Function {
public:
    virtual FunctionState* NewFunctionState() = 0;

protected:
    std::vector<FunctionState*> states;
};

#define VERY_BIG_NUMBER 10

template <typename T>
void FunctionState<T>::ExecuteFunctionLotsAndLotsOfTimes() {
    for(int i = 0; i < VERY_BIG_NUMBER; ++i) {
        RunState();
    }
};

class PrintFunctionState;
class PrintFunction : public Function {
    PrintFunctionState* NewFunctionState();
};

class PrintFunctionState : public FunctionState<PrintFunctionState> {
public:
    PrintFunctionState(PrintFunction* func) : FunctionState<PrintFunctionState>(func) {}

    void RunState() {
        std::cout << "in print function state" << '\n';
    }
};

PrintFunctionState* PrintFunction::NewFunctionState() {
    PrintFunctionState* state = new PrintFunctionState(this);
    states.push_back(state);
    return state;
}

class AddFunctionState;
class AddFunction : public Function {
    AddFunctionState* NewFunctionState();
};

class AddFunctionState : public FunctionState<AddFunctionState> {
public:
    AddFunctionState(AddFunction* func) : FunctionState<AddFunctionState>(func), x(0) {}

    void RunState() {
        ++x;
    }
private:
    int x;
};

AddFunctionState* AddFunction::NewFunctionState() {
    AddFunctionState* state = new AddFunctionState(this);
    states.push_back(state);
    return state;
}


int main() {
    Function* func = new PrintFunction();
    Function* func2 = new AddFunction();
    std::vector<Function*> vec = {func, func2};

    for(auto& func : vec) {
        func->NewFunctionState()->ExecuteFunctionLotsAndLotsOfTimes();
    }

    return 0;
}

最佳答案 基于类型擦除和CRTP的混合解决方案怎么样?

它遵循一个基于问题片段的最小的工作示例:

#include <iostream>
#include <vector>

class PrintFunctionState;
class AddFunctionState;
class FunctionState;

class Function {
    template<typename T>
    static FunctionState * InternalNewFunctionState(Function *self, std::vector<FunctionState*> &states) {
        FunctionState* state = new T(self);
        states.push_back(state);
        return state;
    }

public:
    template<typename T>
    static Function * create() {
        Function *func = new Function;
        func->internalNewFunctionState = &InternalNewFunctionState<T>;
        return func;
    }

    FunctionState* NewFunctionState() {
        return internalNewFunctionState(this, states);
    }

private:
    FunctionState * (*internalNewFunctionState)(Function *, std::vector<FunctionState*> &);
    std::vector<FunctionState*> states;
};

class FunctionState {
public:
    FunctionState() = default;
    virtual ~FunctionState() = default;
    virtual void ExecuteFunctionLotsAndLotsOfTimes() = 0;
};

template<typename Derived>
class IntermediateFunctionState: public FunctionState {
public:
    IntermediateFunctionState(Function* func) : mFunc(func) {}

    void ExecuteFunctionLotsAndLotsOfTimes() override {
        Derived *self = static_cast<Derived *>(this);
        for(int i = 0; i < 10; ++i) {
            self->RunState();
        }
    }

private:
    Function* mFunc;
};

class PrintFunctionState : public IntermediateFunctionState<PrintFunctionState> {
public:
    PrintFunctionState(Function* func) : IntermediateFunctionState(func) {}

    void RunState() {
        std::cout << "in print function state" << '\n';
    }
};

class AddFunctionState : public IntermediateFunctionState<AddFunctionState> {
public:
    AddFunctionState(Function* func) : IntermediateFunctionState(func), x(0) {}

    void RunState() {
        std::cout << "in add function state" << '\n';
        ++x;
    }

private:
    int x;
};

int main() {
    Function* func = Function::create<PrintFunctionState>();
    Function* func2 = Function::create<AddFunctionState>();
    std::vector<Function*> vec = { func, func2 };

    for(auto& func : vec) {
        func->NewFunctionState()->ExecuteFunctionLotsAndLotsOfTimes();
    }

    return 0;
}

我删除了几个不再需要的类.
希望代码说明一切,如果我可以添加更多细节,请在评论中告诉我.

点赞