compiler-optimization

Is it OK to discard placement new return value when initializing objects

旧街凉风 提交于 2019-12-19 18:28:10
问题 This question originates from the comment section in this thread, and has also got an answer there. However, I think it is too important to be left in the comment section only. So I made this Q&A for it. Placement new can be used to initialize objects at allocated storage, e.g., using vec_t = std::vector<int>; auto p = (vec_t*)operator new(sizeof(vec_t)); new(p) vec_t{1, 2, 3}; // initialize a vec_t at p According to cppref, Placement new If placement_params are provided, they are passed to

Is it OK to discard placement new return value when initializing objects

倖福魔咒の 提交于 2019-12-19 18:28:08
问题 This question originates from the comment section in this thread, and has also got an answer there. However, I think it is too important to be left in the comment section only. So I made this Q&A for it. Placement new can be used to initialize objects at allocated storage, e.g., using vec_t = std::vector<int>; auto p = (vec_t*)operator new(sizeof(vec_t)); new(p) vec_t{1, 2, 3}; // initialize a vec_t at p According to cppref, Placement new If placement_params are provided, they are passed to

Alternative schemes for implementing vptr?

匆匆过客 提交于 2019-12-19 17:40:07
问题 This question is not about the C++ language itself(ie not about the Standard) but about how to call a compiler to implement alternative schemes for virtual function. The general scheme for implementing virtual functions is using a pointer to a table of pointers. class Base { private: int m; public: virtual metha(); }; equivalently in say C would be something like struct Base { void (**vtable)(); int m; } the first member is usually a pointer to a list of virtual functions, etc. (a piece of

GCC standard optimizations behavior

时光怂恿深爱的人放手 提交于 2019-12-19 17:37:12
问题 Here I compile an input program with -O2 optimization level (with gcc 4.8.4) and measure the execution time: gcc -O2 -c test.c -o obj.o TIMEFORMAT='%3R' && time(./obj.o) execution time = 1.825 and when I replace -O2 flag with the list of options that are turned on as defined in GCC manuel in the level -O2 https://gcc.gnu.org/onlinedocs/gcc-4.8.4/gcc/Optimize-Options.html#Optimize-Options like that: gcc -fauto-inc-dec -fcompare-elim -fcprop-registers -fdce -fdefer-pop -fdse -fguess-branch

Can JIT be prevented from optimising away method calls?

谁说我不能喝 提交于 2019-12-19 10:49:13
问题 We are building a tool for average case runtime analysis of Java Byte Code programs. One part of this is measuring real runtimes. So we would take an arbitrary, user provided method that may or may not have a result and may or may not have side effects (examples include Quicksort, factorial, dummy nested loops, ...) and execute it (using reflection), measuring the elapsed time. (Whether or not we benchmark properly at all is besides the point here.) In the benchmarking code, we obviously don

Why is Clang automatically adding attributes to my functions?

。_饼干妹妹 提交于 2019-12-19 09:49:43
问题 I have a piece of code that I'm trying to turn into LLVM bitcode: int main() { volatile double n = 0.45; for (int j = 0; j < 32; j++) { n *= j; } return 0; } I run the following command on it: clang -O0 -S -emit-llvm TrainingCode/trainingCode.cpp -o TrainingCode/trainingCode.ll to generate the following LLVM bitcode (take note of the 6th line, the one with "Function Attrs"): ; ModuleID = 'TrainingCode/trainingCode.cpp' source_filename = "TrainingCode/trainingCode.cpp" target datalayout = "e-m

How to read a .obj file?

空扰寡人 提交于 2019-12-19 08:20:12
问题 In visual studio a object file (.obj) is generating after compiling a c++ file. How to read and understand it? Also how to see the code after compiler optimization in Visual studio 2015. Please redirect if this is already answered. 回答1: Use the DUMPBIN tool from Visual Studio command prompt. Specifically, the /DISASM option shows you the disassembly. Do note the if you have link-time optimization enabled, then at least theoretically the final code may change after the .obj files are linked to

Short-circuited operators and tail recursion

给你一囗甜甜゛ 提交于 2019-12-19 06:45:59
问题 Let's say I have a simple function like this: int all_true(int* bools, int len) { if (len < 1) return TRUE; return *bools && all_true(bools+1, len-1); } This function can be rewritten in a more obviously tail-recursive style as follows: int all_true(int* bools, int len) { if (len < 1) return TRUE; if (!*bools) return FALSE; return all_true(bools+1, len-1); } Logically, there is zero difference between the two; assuming bools contains only TRUE or FALSE (sensibly defined), they do exactly the

Performance of pIter != cont.end() in for loop

ぃ、小莉子 提交于 2019-12-19 05:04:35
问题 I was getting through "Exceptional C++" by Herb Sutter lately, and I have serious doubts about a particular recommendation he gives in Item 6 - Temporary Objects. He offers to find unnecessary temporary objects in the following code: string FindAddr(list<Employee> emps, string name) { for (list<Employee>::iterator i = emps.begin(); i != emps.end(); i++) { if( *i == name ) { return i->addr; } } return ""; } As one of the example, he recommends to precompute the value of emps.end() before the

Replacing arrays access variables with the right integer type

白昼怎懂夜的黑 提交于 2019-12-19 03:59:44
问题 I've had a habit of using int to access arrays (especially in for loops); however I recently discovered that I may have been "doing-it-all-wrong" and my x86 system kept hiding the truth from me. It turns out that int is fine when sizeof(size_t) == sizeof(int) but when used on a system where sizeof(size_t) > sizeof(int) , it causes an additional mov instruction. size_t and ptrdiff_t seem to be the optimal way on the systems I've tested, requiring no additional mov . Here is a shortened example