4 ms·
It wouldn't. The compiler would just have to generate the copy when the standard demands it (such as if the function body takes the address of the object).
by mort96 6y ago
It wouldn't. The compiler would just have to generate the copy when the standard demands it (such as if the function body takes the address of the object).
- gpderetta 6y agoYes but then in many cases either (or both!) the caller and the callee might need to make a copy defeating the point of the optimization or even being worse than the original.
- mort96 6y agoIn many cases the callee would have to make a copy, yes. However: 1. In many cases, no copy would have to be made. There are lots of small non-complex functions out there where the compiler can prove that it's safe to not make a copy. 2. In many other cases, a copy has to be made. But the copy is made by the callee, not by the caller. That means that all the instructions necessary to copy the argument ends up in the binary once in the callee, rather than once for every function call, leading to less code bloat (which has its own performance advantages). In fact, a stupid compiler could just always make a copy without analyzing the function body. This would result in a compiler which generates code that's about as fast as it would be with current ABIs, but with a smaller size.
- gpderetta 6y agoYou have to make a copy on the caller or the callee if the address of the object escapes, so you might end up with two extra copies even if nothing in the program mutates the object.
- mort96 6y agoI don't understand how you achieve extra copies? My understanding is that the caller would never make a copy, it would always pass a pointer to large structs. So the absolute worst case, unless I'm missing something, is that we end up with the same number of copies as we do today (i.e one copy per large struct passed as a parameter).
- gpderetta 6y agoIf the address of the object escapes on the caller side then it has to make a copy as the object could be mutated or even just break the distinct address guarantee of the language.
- mort96 6y agoI still don't understand, sorry. If the callee does something which could cause the caller's object to change, such as calling an unknown function or modifying through another pointer which might alias the parameter, the callee would just have to make a copy. Could you provide an example of a situation where there would be more copies made using the proposed ABI than in traditional ABIs?
- gpderetta 6y agoSure, if calling any external function or writing though any pointer would force the callee to copy the object then yes you can have only the callee do the copy, but then it seems that this optimization would apply only to a very small subset of functions.
- mort96 6y agoRight. That was my understanding, but I now see that there are more ways to understand it. I don't know which is correct, so I wrote a response to moonchild's comment here: https://news.ycombinator.com/item?id=27091726 https://news.ycombinator.com/item?id=27091726
- moonchild 6y agostruct A { int m; } global = {5}; int f(struct A a) { global.m = 7; return a.m; } int main() { f(global); // need to make a copy of 'global' here // otherwise f will return 5 instead of 7 }
- mort96 6y ago