我有一个使用通过new []分配的常规数组的自定义循环缓冲区实现,然后使用std::move将元素std::move到数组中。 这里是我的push()方法的实现:
void push(value_type&& value) { _content[_end] = std::move(value); // 9.2% of execution is spend here increment(); // 0.6% here }我正在进入数组的对象基本上只是一个指针和一个std::unique_ptr :
struct Task { Task() {} Function function; Batch *batch; };和Function看起来像这样:
class Function { public: template<typename F> Function(F&& f) : _implementation(new ImplementationType<F>(std::move(f))) {} void operator() () { _implementation->Call(); } Function() = default; Function(Function&& other) : _implementation(std::move(other._implementation)) {} Function& operator=(Function&& other) { _implementation = std::move(other._implementation); return *this; } Function(const Function&) = delete; Function(Function&) = delete; Function& operator= (const Function&) = delete; private: struct Base { virtual void Call() = 0; virtual ~Base() {} }; template<typename F> struct ImplementationType : Base { ImplementationType(F&& f) : function(std::move(f)) {} void Call() { function(); } F function; }; std::unique_ptr<Base> _implementation; };我在循环中重复调用ringbuffers push()方法来填充缓冲区中的任务,没有其他计算发生在那里。 我希望std::move()开销很小,绝对不会占用我计算时间的最大块。 任何人都可以指出我在这里做错的正确方向吗?
I have a custom ringbuffer implementation which uses a normal array allocated via new [], and then uses std::move to move elements into the array. Here is the implementation of my push() method:
void push(value_type&& value) { _content[_end] = std::move(value); // 9.2% of execution is spend here increment(); // 0.6% here }The objects I'm moving into the array are basically just a pointer and a std::unique_ptr:
struct Task { Task() {} Function function; Batch *batch; };And Function looks like this:
class Function { public: template<typename F> Function(F&& f) : _implementation(new ImplementationType<F>(std::move(f))) {} void operator() () { _implementation->Call(); } Function() = default; Function(Function&& other) : _implementation(std::move(other._implementation)) {} Function& operator=(Function&& other) { _implementation = std::move(other._implementation); return *this; } Function(const Function&) = delete; Function(Function&) = delete; Function& operator= (const Function&) = delete; private: struct Base { virtual void Call() = 0; virtual ~Base() {} }; template<typename F> struct ImplementationType : Base { ImplementationType(F&& f) : function(std::move(f)) {} void Call() { function(); } F function; }; std::unique_ptr<Base> _implementation; };I call the ringbuffers push() method repeatedly in a loop to fill the buffer up with tasks, there is no other computation happening there. I would expect the std::move() to have very little overhead, and definitely not eat up the biggest chunk of my computation time. Can anyone point me into the right direction of what I'm doing wrong here?
最满意答案
std::move本身在运行时不做任何事情; 它只是将其参数转换为适合传递给移动赋值运算符的右值 。 这是一项需要时间的任务。
如果_content[_end]不为空,那么重新分配唯一指针将删除旧对象。 也许这就是花时间?
std::move itself does nothing at runtime; it just casts its argument into an rvalue suitable for passing to the move-assignment operator. It's the assignment that will be taking time.
If _content[_end] isn't empty, then reassigning the unique pointer will delete the old object. Perhaps that's what's taking the time?
更多推荐
发布评论