我有以下类似于我的代码库的程序。一个 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;
    }
    

    我删除了一些不再需要的类。
    希望代码不言自明,如果我可以添加更多详细信息,请在评论中告诉我。

    关于c++ - 在这种情况下,如何使用 CRTP 删除虚拟方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41063388/

    10-11 18:27