简体   繁体   English

什么是 gcc 编译器优化?

[英]What are gcc compiler optimizations?

I could think of 3 cases, compiler could represent as a CSP and then optimize, however I don't know if it does.我可以想到 3 种情况,编译器可以表示为 CSP,然后进行优化,但我不知道是否可以。 I don't assume a specific compiler optimization flag, however, to ensure optimization, you could assume -O2 or -O3 flags are given.我不假设特定的编译器优化标志,但是,为了确保优化,您可以假设给出 -O2 或 -O3 标志。

1) passing arguments themselves instead of copying, if the arguments are not used after a function call 1) 如果 arguments 在 function 调用后未使用,则自行传递 arguments 而不是复制

void aFunction(std::string aStr);
...
std::string aString = makeAString(anIntegerInput);
size_t mqSize = aString.size();
aFunction(aString); // or a class method like aClass->aFunction(aString);
std::cout << "Size : " << mqSize << std::endl

since the aString is not used after aFunction call.因为在 aFunction 调用之后不使用 aString 。 It could logically infer that, instead of copying aString, does compiler move the string(aFunction's signature is not string&& - it is not a move operation here)?它可以从逻辑上推断,编译器不是复制 aString,而是移动字符串(aFunction 的签名不是 string&& - 它不是这里的移动操作)? Would making aFunction's input parameter std::string &&aStr force it?将 aFunction 的输入参数设置为 std::string &&aStr 会强制它吗?

2) Here, a T object with default constructor is created, and then copy constructor used to initialize values of a Map. 2) 这里,创建一个带有默认构造函数的 T object,然后复制构造函数用于初始化一个 Map 的值。

template<typename T>
void aMethod(std::map<std::string, T>& p, const std::vector<std::string>& aVec) {
    p.clear();
    T t;
    for (auto it = aVec.begin(); it != aVec.end(); ++it) {
        p[*it] = t;
    }
}

Compiler could detect that the "t" object is only default contructed object and instead of copying, may initialize values of the p map with default constructors.编译器可以检测到“t”object 只是默认构造的 object,而不是复制,可以使用默认构造函数初始化 p map 的值。

3) We have a series of "theSameString" 3)我们有一系列“theSameString”

std::map<std::string, int> temp{{"john", 5}, {"anna", 7}};
int aValue = temp.find("john");
int anotherValue = temp.find("john");
int yetAnotherValue = temp.find("john");

here, does compiler create 4 different "john" const char* datastructures, or for each const char* to be created, checks previous const char* datastructures?在这里,编译器是否创建了 4 个不同的“john” const char* 数据结构,或者对于要创建的每个 const char*,检查以前的 const char* 数据结构? Thank you,谢谢,

Compiler is not allowed to change a copy-constructor call by move-constructor call as it must follow language rules (Notice that if the methods have generally expected semantic, they are only regular methods which might not be equivalent with expected semantic (I see a matrix class where operator++() and operator++(int) behave really differently, one increase row, the other column, so it++; cannot be replaced by ++it; for optimization)).编译器不允许通过 move-constructor 调用更改复制构造函数调用,因为它必须遵循语言规则(请注意,如果方法通常具有预期语义,它们只是可能与预期语义不等效的常规方法(我看到矩阵 class 其中operator++()operator++(int)的行为非常不同,一个增加行,另一列增加,所以it++;不能被++it;用于优化))。

The only allowed behavior change is copy/move (constructor) elision in specified cases.在特定情况下,唯一允许的行为更改是复制/移动(构造函数)省略 and since C++14 new expression .并且由于 C++14新表达式

What it can do is optimization following the as-if rule .它可以做的就是按照as-if rule进行优化。 so any change that doesn't affect observable behavior (I/O, volatile accesses).因此任何不影响可观察行为的更改(I/O、易失性访问)。 "timing" is not an observable behavior, I meant if you print elapsed time of a code, it doesn't disallow optimization of that code. “计时”不是可观察到的行为,我的意思是如果您打印代码的经过时间,它不会禁止优化该代码。

3.) We have a series of "theSameString" 3.) 我们有一系列“theSameString”

compiler is allowed but not required to do that.编译器是允许的,但不需要这样做。

  • "hello world" == "hello world" might return true or false . "hello world" == "hello world"可能返回truefalse
  • "hello world" + 6 == "world" might return true or false . "hello world" + 6 == "world"可能返回truefalse

(remember we compare pointers here, not content). (请记住,我们在这里比较的是指针,而不是内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM