命名 RVO 的可能性?

发布于 2024-09-14 22:44:57 字数 342 浏览 2 评论 0原文

我有一个如下所示的函数:

// Fetch 1 MB of data
void GetData(std::vector<char> & outData);

1MB 有点夸张,但我只是想指出,最好避免不必要的复制。

如果我添加此过载:

std::vector<char> GetData()
{
    std::vector<char> result;
    GetData(result);
    return result;
}

那么 RVO 启动的可能性有多大?

I have a function that looks like this:

// Fetch 1 MB of data
void GetData(std::vector<char> & outData);

The 1MB is exaggerated, but I just want to make the point that it's preferable to avoid unnecessary copies.

If I add this overload:

std::vector<char> GetData()
{
    std::vector<char> result;
    GetData(result);
    return result;
}

Then how likely is it that RVO will kick in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

束缚m 2024-09-21 22:44:57

对于大多数最新的编译器(例如,VS 2005 或更新版本、gcc 3.4 或更新版本),这基本上是确定的。我只说“大多数”,因为我还没有测试现有的每个编译器。大概在过去 5 年左右的时间里,我见过的每一个新编译器都包含了它。

With most reasonably recent compilers (e.g., VS 2005 or newer, gcc 3.4 or newer), it's essentially certain. I only say "most" because I haven't tested every compiler in existence. Every new compiler I've looked at in probably the last 5 years or so has included it.

无妨# 2024-09-21 22:44:57

RVO 很可能会发挥作用,因为它是一个非常简单的优化,并且已经存在很长一段时间了。然而,为了让这段代码在中等高性能的代码中具有真正的实用价值,您需要 NRVO。 NRVO 比较难遇到,因为它相对较新。但它是可用的。例如,MS 编译器从 VS2005 开始就实现了它。

RVO will most likely kick in, since it is a pretty simple optimization, which has been available for quite a while. However, in order to give this piece of code real practical value in even moderately high-performance code you'd need NRVO. NRVO is harder to come across, since it is relatively new. Yet it is available. MS compiler, for one example, implements it since VS2005.

疯到世界奔溃 2024-09-21 22:44:57

我认为对此没有任何标准答案:这取决于您的编译器及其功能。

如果您考虑为了方便而实现这一点,为什么不尝试使用您的编译器并查看程序集或分析它并看看会发生什么?关于编译器实际执行的操作的经验证据可能比猜测某些编译器可能执行或不执行的操作更好。

I don't think there's any standard answer to this: it depends on your compiler and what it's capable of.

If you're thinking of implementing this for convenience why not just try in on your compiler(s) and either look at the assembly or profile it and see what happens? Empirical evidence about what your compiler actually does is probably better than guessing what some compilers may or may not do.

谜兔 2024-09-21 22:44:57

那么 RVO 启动的可能性有多大?

思考是软件开发人员的工作,而不是编译器的工作。

编译器通常会进行优化,以使好的代码能够正常运行,而不是坏的代码。

就我个人而言,我使用第一种形式。通常使用指针而不是引用 - 以突出显示参数是输出参数而不是输入参数这一事实。

Then how likely is it that RVO will kick in?

It is a software developer's job to think, not compiler's.

Compilers are generally optimized to make the good code work well - not the bad one.

Personally, I use the first form. Normally with a pointer instead of reference - to highlight the fact that the parameter is the output one, not input.

原谅我要高飞 2024-09-21 22:44:57

另请注意,当您说:

std::vector<char> GetData() 
{ 
//   :
    return result; 
}

vector<char> x = GetData();

在被调用方中,将 result 复制到“返回值”,然后在调用方中,将“返回值”复制到 x >。 NRVO 可以删除其中一个副本,但不能同时删除两者。编译器有义务至少调用复制构造函数一次,因为它必须假设复制构造函数具有必须完成的副作用。

Also, note that when you say:

std::vector<char> GetData() 
{ 
//   :
    return result; 
}

vector<char> x = GetData();

In the callee, result is copied to a "return value", and then in the caller, the "return value" is copied into x. NRVO can get rid of one of those copies, but not both. The compiler is obliged to call the copy ctor at least once, because it has to assume the copy ctor has side-effects which must be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文