The clarity of the code using a particular library is such an big (but often under-appreciated) benefit that I would heavily lean in this direction when considering interface options. My 2c.
Should small Rust structs be passed by-copy or by-borrow? (2019)
11–20 of 238 posts
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#12Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#13This is one advantage of Ada, where parameters are abstractly declared as "in" or "in out" or "out". The compiler can then decide how to best implement it for that specific size and architecture.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#14This is one advantage of Ada, where parameters are abstractly declared as "in" or "in out" or "out". The compiler can then decide how to best implement it for that specific size and architecture.
Also Fortran has "in", "inout" and "out".
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#15* Sprinkling & around everything in math expressions does make them ugly. Maybe rust needs an asBorrow or similar?
* If you inline everything then the speed is the same.
* Link time optimizations are also an easy win.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#16Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#17I just went through all of this when building a raytracer. * Sprinkling & around everything in math expressions does make them ugly. Maybe rust needs an asBorrow or similar? * If you inline everything then the speed is the same. * Link time optimizations are also an easy win. https://github.com/mcallahan/lightray
Do you mean AsRef, or do you mean magic which automatically borrows parameters and is specifically what rust does not do any more than e.g. C does?
Though you can probably get both if the by-ref version is faster (or more convenient internally): wrap the by-ref function with a by-value wrapper which is #[inline]-ed, this way the interface is by value but the actual parameter passing is byref (as the value-consuming wrapper will be inlined and essentially removed).
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#18Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#19It does sometimes matter though. One optimization I’ve seen in a few places is to box the error type, so that a result doesn’t copy the (usually empty) error by value on the stack. That actually makes a small performance difference, on the order of about 5-10%.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#20Performance-wise, if you're likely to touch every element in a type anyway, err on the side of copies. They are going to have to end up in registers eventually anyway, so you might as well let the caller find out the best way to put them there.