2013 Keynote: Chandler Carruth: Optimizing the Emergent Structures of C++
Why Stack Overflow recommends it
Stack Overflow users link it when arguing that pass-by-value and value semantics are often faster than references, because references force the optimizer to assume aliasing.
Recommended by 10 people across 9 different months, 2013 to 2018.
Why Hacker News recommends it
HN lists it among C++ optimization talks and cites it for how much optimizer performance comes from inlining, from an experienced compiler engineer.
Recommended by 1 people across 1 different months, 2013 to 2018.
What it is
Chandler Carruth's CppNow 2013 keynote on how C++ builds complex software from simple emergent structures, and how compilers optimize them through inlining and value semantics.
What people said
Strong rec ComicSansMS · 2014-02 · Stack Overflow · on “How true is "Want Speed? Pass by value"”
For a nice explanation of the optimization opportunities enabled by pass-by-value, check out Chandler Carruth's excellent BoostCon 2013 keynote (youtube video) [this video] .
Recommends Sebastian Redl · 2013-06 · Stack Overflow · on “Why the sequence-operation algorithms predicates are passed by copy?”
@PaperBirdMaster If you really want a performance reason, Chandler's C++Now keynote might offer some, though this was definitely not part of the decision process. [this video]
Recommends Matthias Holzapfel · 2013-06 · Stack Overflow · on “Eigen Library: Take a copy of a templated function parameter”
Im trying to convert the content of an arbitrary Eigen-Vector/Array type to an std::array. I read this part from the Eigen doc [LINK] so this is my function: template<typename DERIVED> std::array<typename Eigen::DenseBase<DERIVED>::Scalar, Eigen::DenseBase<DERIVED>::RowsAtCompileTime> toStl (const Eigen::DenseBase<DERIVED>& input) { ... // Do conversion } The issue is now, that when I remove the ampersand from the input parameter type in order to take a copy, I get an empty input variable. It contains some bogus value for coefficient 0 and the rest is zeroes. When I take the parameter by reference, everything works fine. Can anybody tell me why? PS.: For those who might wonder why I want to
Recommends Tobias · 2013-07 · Stack Overflow · on “Function fills vector yet it becomes empty at some point.”
juanchopanza showed the bug in your code. I would recomend a different style. Change fillArray to: std::vector<int> fillArray(istream& io) { std::vector<int> response; int temp; io>>temp; while (temp!=-999) { response.push_back(temp); io>>temp; } return response; } Used in main: int main() { int actualSize; string fileName; string info; std::vector<int> a; cout << "Enter the input file name "; cin >> fileName; ifstream inData; ofstream outData; inData.open(fileName.c_str()); outData.open("out.txt"); int searchItem; inData>>info; inData>>searchItem; a = fillArray(inData); // If you really like actualSize add actualSize = a.size(); cout <<"Search in vector\n"; outData<<"Search in vector\n"; ou
Recommends Nikos C. · 2013-09 · Stack Overflow · on “int vs const int&”
One thing that might be worth adding is that references can actually result in slower code when the reference needs to be copied (and sometimes even if it doesn't need to be copied.) The canonical article for this is [LINK] But see also the (very interesting) presentation by Chandler Carruth that nicely shows how passing by reference can severely hurt the compiler's optimizer: [this video]
Recommends mattnewport · 2013-10 · Stack Overflow · on “Any efficiency benefit to passing primitive types by reference instead of returning by value?”
In general there won't be any performance benefit and there may well be a performance cost. Consider this code: void foo(const int& a, const int& b, int& res) { res = a + b; res *= a; } int a = 1, b = 2; foo(a, b, a); When a compiler encounters a function like add() it must assume that a and res may alias as in the example call so without global optimizations it will have to generate code that loads a, loads b, then stores the result of a + b to res, then loads a again and performs a multiply, before storing the result back to res. If instead you'd written your function like this: int foo(int a, int b) { int res = a + b; res *= a; return res; } int a = 1, b = 2; int c = foo(a, b); Then the c
Recommends Ali · 2014-02 · Stack Overflow · on “Memory allocation for return value of a function in a loop in C++11: how does it optimize?”
I would imagine that the most efficient thing to do is to allocate a single piece of memory for all the values, both inside the function and outside in the for-loop, that will get overwritten in each iteration. I don't think that any of the current compilers can do that. (I would be stunned to see that.) If you want to get insights, watch Chandler Carruth's talk [this video] . If you need this kind of optimization, you need to do it yourself: Allocate the vector outside the loop and pass it by non-const reference to function() as argument. Of course, don't forget to call clear() when you are done or call clear() first inside function() . All this has nothing to do with move semantics, nothing has
Recommends ComicSansMS · 2014-10 · Stack Overflow · on “Will passing std::string via copying be optimized?”
This is a somewhat controversial issue. In theory, yes the compiler is more likely to apply certain optimizations when passing by value. Chandler Carruth explained how this works for LLVM in his BoostCon 2013 keynote Optimizing the Emergent Structures of C++ (video) [this video] / (slides) [LINK] . The problem is that there are overheads to pass-by-value which the compiler is not able to get rid of. This is true even when you have a conformant C++11 compiler and a movable type like std::string . Herb Sutter discussed the issue of calling conventions in particular in his CppCon2014 talk Essentials of Modern C++ Style (slides) [LINK] . His conclusion is to do it like you learned in C++98-school: Us
Recommends Maxim Egorushkin · 2015-08 · Stack Overflow · on “Use of References or Pointers in Interfaces in C++”
Passing parameters by reference or pointers hinders the optimizer because now it has to consider aliasing and memory model requirements. Prefer passing by value if possible. See [this video] for more details.
Recommends Goobley · 2015-09 · Stack Overflow · on “Should I now be passing by value?”
In this talk [this video] (sorry about the sound) Chandler Carruth suggests not passing by reference, even const reference, in the vast majority of cases due to the way in which it limits the back-end to perform optimisation. He claims that in most cases the copy is negligible - which I am happy to believe, most data structures/classes etc. have a very small part allocated on the stack - especially when compared with the back-end having to assume pointer aliasing and all the nasty things that could be done to a reference type. Let's say that we have large object on the stack - say ~4kB and a function that does something to an instance of this object (assume free-standing function). Classically I