c++ - 允许编译器优化堆内存分配吗?

标签 c++ gcc optimization clang language-lawyer

考虑以下使用 new 的简单代码(我知道没有 delete[],但它与这个问题无关):

int main()
{
    int* mem = new int[100];

    return 0;
}

是否允许编译器优化 new 调用?

在我的研究中,g++ (5.2.0)和 Visual Studio 2015 不会优化 new 调用,while clang (3.0+) does .所有测试均已启用全面优化(g++ 和 clang 为 -O3,Visual Studio 为 Release模式)。

new 是不是在后台进行系统调用,使编译器无法(且非法)对其进行优化?

编辑:我现在已经从程序中排除了未定义的行为:

#include <new>  

int main()
{
    int* mem = new (std::nothrow) int[100];
    return 0;
}

clang 3.0 does not optimize that out没有了,但是 later versions do .

EDIT2:

#include <new>  

int main()
{
    int* mem = new (std::nothrow) int[1000];

    if (mem != 0)
      return 1;

    return 0;
}

clang always returns 1 .

最佳答案

历史似乎是 clang 遵循 N3664: Clarifying Memory Allocation 中规定的规则。它允许编译器围绕内存分配进行优化,但为 Nick Lewycky points out :

Shafik pointed out that seems to violate causality but N3664 started life as N3433, and I'm pretty sure we wrote the optimization first and wrote the paper afterwards anyway.

因此 clang 实现了优化,后来成为作为 C++14 的一部分实现的提案。

基本问题是这是否是 N3664 之前的有效优化,这是一个棘手的问题。我们将不得不去 as-if rule在草案 C++ 标准部分 1.9 Program execution 中提到(emphasis mine):

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.5

注意 5 说:

This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.

由于 new 可能会抛出一个具有可观察行为的异常,因为它会改变程序的返回值,这似乎反对 as-if 规则允许它

虽然,可以说它是何时抛出异常的实现细节,因此即使在这种情况下,clang 也可以决定它不会导致异常,因此省略 new 调用不会违反假设规则

根据 as-if 规则 似乎也可以优化对非抛出版本的调用。

但是我们可以在不同的翻译单元中有一个替换的全局运算符 new,这可能会导致这影响可观察的行为,所以编译器必须有某种方式证明这不是这种情况,否则它将无法在不违反 as-if 规则的情况下执行此优化。在这种情况下,以前版本的 clang 确实优化为 this godbolt example showsCasey here 提供,取这个代码:

#include <cstddef>

extern void* operator new(std::size_t n);

template<typename T>
T* create() { return new T(); }

int main() {
    auto result = 0;
    for (auto i = 0; i < 1000000; ++i) {
        result += (create<int>() != nullptr);
    }

    return result;
}

并将其优化为:

main:                                   # @main
    movl    $1000000, %eax          # imm = 0xF4240
    ret

这确实看起来太激进了,但后来的版本似乎没有这样做。

关于c++ - 允许编译器优化堆内存分配吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873616/

相关文章:

ios - 在 Swift 中优化屏幕反馈绘制立方贝塞尔曲线

c++ - 将 FLOAT 值插入 mysql 表

C++, "Old Fashioned"方式

gcc - CentOS 7 : libstdc++. so.6: 找不到版本 `CXXABI_1.3.9'

c++ - 为什么将函数参数中的 `const ull` 更改为 `const ull&` 会导致性能提升?

在树目录结构中查找历史操作的算法

c++ - CMake 中的路径变量

c++ - 用 setw 截断

c++ - 模板推演失败

linux - 列出 ld 链接器可用的所有符号