5 ms·
Yes - even if you ask for pass by move, it might pass a reference for a large struct.
by richardwhiuk 7y ago
Yes - even if you ask for pass by move, it might pass a reference for a large struct.
- vintermann 7y agoThat's interesting, and a bit surprising. I know Ada does this, but it's explicit about only specifying semantics ("in, "out" or "in out") and letting the compiler decide. Surely C or C++ optimizers won't do this?
- jdmichal 7y agoIt's not as surprising when you consider that moving the value into the function also makes it permanently unavailable to the caller. So what difference does it really make? The semantics are the same either way. The lifetime of the object ends within the function.
- gpm 7y ago> Surely C or C++ optimizers won't do this? When they can prove it doesn't change the semantics on ways that violate the standard I think you will find that they do. Rust doesn't really have it's own optimizer after all, just a repurposed C/C++ one.
- quietbritishjim 7y agoFor objects with trivial constructors and destructors, that's certainly a possibility. But anything with non-trivial move constructor or destructor, e.g. anything that allocates memory, is going to be much harder to optimise in C++. The problem is, "moving" a C++ object means executing an arbitrary function, which at the very least needs to put the object to a "do nothing on destruction" state. The moved-from object still exists and has to have its destructor run (which is also an arbitrary function). In Rust, even an unoptimised move is just a bitwise copy, and the moved-from object doesn't need a destructor run because its lifetime ends completely when it is moved from.
- kzrdude 7y agoLet's disregard inlining etc, so that we have actual function calls in both languages. C and Rust compilers do the same thing - they follow a calling convention. It will tell you how to pass a struct, if it should be passed as a pointer or in registers etc.
- richardwhiuk 7y agoRust doesn't have a defined, standardized, binary calling convention, to the extent that C does.
- kzrdude 7y agoI agree, but that doesn't mean that it picks different calling conventions for each function call it compiles or something like that. Each version & platform of Rustc will have a calling convention that it sticks to when compiling function calls.
- richardwhiuk 7y agoIt can pick a calling style on a per function basis.
- deleted 7y ago[deleted]