compiler-optimization

How does clang manage to compile this code with undefined behavior into this machine code?

元气小坏坏 提交于 2019-12-10 11:32:01
问题 It's a variation of code from this tweet, just shorter one and not causing any damage to noobs. We have this code: typedef int (*Function)(); static Function DoSmth; static int Return7() { return 7; } void NeverCalled() { DoSmth = Return7; } int main() { return DoSmth(); } You see that NeverCalled() is never called in the code, don't you? Here's what Compiler Explorer shows when clang 3.8 is selected with -Os -std=c++11 -Wall Code emitted is: NeverCalled(): retq main: movl $7, %eax retq as if

Do comments get translated to machine code? C++

▼魔方 西西 提交于 2019-12-10 10:59:13
问题 When a program written in C++ has comments, are those comments translated into machine language or do they never get that far? If I write a C++ program with an entire book amount of comments between two commands, will my program take longer to compile or run any slower? 回答1: Comments are normally stripped out during preprocessing, so the compiler itself never sees them at all. They can (and normally do) slow compilation a little though--the preprocessor has to read through the entire comment

Using `size_t` for lengths impacts on compiler optimizations?

旧城冷巷雨未停 提交于 2019-12-10 07:53:46
问题 While reading this question, I've seen the first comment saying that: size_t for length is not a great idea, the proper types are signed ones for optimization/UB reasons. followed by another comment supporting the reasoning. Is it true? The question is important, because if I were to write e.g. a matrix library, the image dimensions could be size_t , just to avoid checking if they are negative. But then all loops would naturally use size_t . Could this impact on optimization? 回答1: size_t

Empty derived optimization

风流意气都作罢 提交于 2019-12-10 04:57:16
问题 Most C++ programmers know about the empty base class optimazation as a technique/idiom. What happens with empty child classes? For example class EmptyBase { int i; }; template<typename T> class Derived : T { }; std::cout << sizeof(Derived<EmptyBase>); // Is there a standard verdic on this? Similarly to the EBO there should be an EDO stating that since a derived class doesn't provide any more members, nor introduces any virtual ones to its parametrizing type , it should not require more memory

Documentation about compiler options for Swift

你离开我真会死。 提交于 2019-12-10 04:37:05
问题 Hello, i want to start running some microbenchmarks on Apples Swift-language. However i feel it is hard to find proper documentation for the different options in regards of compiler optimization. I have read a lot of questions and articles about other peoples microbenchmarks of the language, however it would be nice to have some firm documentation on the subject. In the latest beta, the ones to use seems to be: -Onone No optimizations -O Safe optimizations? -Ounchecked (Replaced -Ofast)

Could a Java compiler reorder function calls?

喜夏-厌秋 提交于 2019-12-10 02:09:50
问题 I know that java compiler can actually reorder code instructions. But can java reorder function calls? For example: ... //these lines may be reordered a=7; b=5; ... //but what about this? callOne(); callTwo(); 回答1: If it can determine that doing so would have no effect on the result, then yes. Since it can't, the compiler won't. JIT can however inline the calls, since it knows if the methods are overridden, and it can then rearrange the code, if it sees fit. Since it can only do so if it can

Why is my JVM doing some runtime loop optimization and making my code buggy?

前提是你 提交于 2019-12-10 02:06:34
问题 Consider the following java code: public int main() { int i = 1111; for (; rules(i) != true && i < Integer.MAX_VALUE; i++) { //LOG.debug("Testing i: " + i); } System.out.println("The mystery number is: " + i); return i; } protected boolean rules(int nb) { //... } I've found out that even when the for loop continuation evaluation is true , the loop will stop being executed when its body is empty. The final result of main is wrong ( i is 16698 about 98% of the time and sometimes a little higher

slow JDK8 compilation

泄露秘密 提交于 2019-12-09 22:57:08
问题 Trying to upgrade to JDK8 on a big project, compilation goes really slow on JDK8 compared to JDK7. Running the compiler in verbose mode, JDK8 compiler stops at a big generated converter class(Mapping) for entities from server to client. The converter methods in several cases call other converter methods from the same Mapping class. As a workaround tried to split the Mapping file into multiple files. This visibly improved performance when only compiling the Mapping class or it's containing

How not specify an exact order of evaluation of function argument helps C & C++ compiler to generate optimized code?

◇◆丶佛笑我妖孽 提交于 2019-12-09 19:14:25
问题 #include <iostream> int foo() { std::cout<<"foo() is called\n"; return 9; } int bar() { std::cout<<"bar() is called\n"; return 18; } int main() { std::cout<<foo()<<' '<<bar()<<' '<<'\n'; } // Above program's behaviour is unspecified // clang++ evaluates function arguments from left to right: http://melpon.org/wandbox/permlink/STnvMm1YVrrSRSsB // g++ & MSVC++ evaluates function arguments from right to left // so either foo() or bar() can be called first depending upon compiler. Output of above

Passing by Value and copy elision optimization

房东的猫 提交于 2019-12-09 17:23:26
问题 I came upon the article http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ Author's Advice: Don’t copy your function arguments. Instead, pass them by value and let the compiler do the copying. However, I don't quite get what benefits are gained in the two example presented in the article: //Don't T& T::operator=(T const& x) // x is a reference to the source { T tmp(x); // copy construction of tmp does the hard work swap(*this, tmp); // trade our resources for tmp's return *this; //