Live data from Hacker News

Should small Rust structs be passed by-copy or by-borrow? (2019)

forrestthewoods.com

11–20 of 238 posts

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#11
I would go with the version that gives the clean user interface (that is, by copy in this case). If it turns out that the other version is significantly more performant and this additional performance is critical for the end users consider adding the by-borrow option.

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.

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#12
I don’t feel like this gave a satisfactory answer the question. Since everything was inlined, the argument passing convention made no difference in the micro benchmarks. But what happens when it does not inline? Then you would actually be testing by-borrow be by-copy instead of how good rust is at optimizing.

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#13
post #3

This 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.

Always curious how Ada solves ABI issue with such optimizations in place.

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#14
post #3

This 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.

> This is one advantage of Ada, where parameters are abstractly declared as "in" or "in out" or "out".

Also Fortran has "in", "inout" and "out".

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#15
I 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

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#16
It's compiled, so, without any investigation at all, I would have been disappointed if there were any significant difference in the code emitted in these cases. I would expect the compiler to do the efficient thing based on usage rather than the particular syntax. I may have too much faith in the compiler.

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#17

I 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

> * Sprinkling & around everything in math expressions does make them ugly. Maybe rust needs an asBorrow or similar?

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)

#18
This is not really surprising in such a case. The Rust compiler is pretty good at optimizing out uneeded copies. Here it does see that the copied value is not used after the function call, so it should simply not emit the copies in the final assembly.

Re: Should small Rust structs be passed by-copy or by-borrow? (2019)

#19
For this code, the compiler inlined the call. So there should be no difference between pass by copy or pass by reference, which is what was measured. Where it could matter is when the code isn’t inlined. But with small structs it might not matter all that much.

It 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)

#20
The general usability impact matters slightly less than it looks here, in part because the `do_math` with references in the article has two extra &s, and in part because methods autoreference when called like x.f().

Performance-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.

Post reply on HN