std :: move()作为性能瓶颈? [英] std::move() as performance bottleneck?

查看:584
本文介绍了std :: move()作为性能瓶颈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ringbuffer实现,使用通过 new [] 分配的正常数组,然后使用 std :: move 将元素移动到数组中。这里是我的 push()方法的实现:

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
}

我移动到数组中的对象基本上是只是一个指针和 std :: unique_ptr

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;
};

并且函数 p>

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;
};

我调用ringbuffers push()反复在一个循环中填充缓冲区任务,没有其他计算发生。我希望 std :: move()有非常少的开销,绝对不会吃掉我的计算时间的最大块。

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 本身在运行时什么也不做;它只是将其参数转换为适用于传递给移动赋值运算符的右值

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.

如果 _content [_end] 不为空,则重新分配唯一的指针将删除旧对象。也许这是花费的时间?

If _content[_end] isn't empty, then reassigning the unique pointer will delete the old object. Perhaps that's what's taking the time?

这篇关于std :: move()作为性能瓶颈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆