Live data from Hacker News

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

forrestthewoods.com

11–20 of 115 posts

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

#11
Note that Rust's "clippy" linter has a `trivially_copy_pass_by_ref` lint which recommends changing by-reference to by-copy for sufficiently small structs, but presumably it's aiming to be conservative.

AIUI it recommends by-copy for structs of at most 8 bytes because it thinks that's the appropriate limit for 32-bit targets, and they don't like the set of warnings issued to change too much for different targets.

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

#12
post #11

Note that Rust's "clippy" linter has a `trivially_copy_pass_by_ref` lint which recommends changing by-reference to by-copy for sufficiently small structs, but presumably it's aiming to be conservative. AIUI it recommends by-copy for structs of at most 8 bytes because it thinks that's the appropriate limit for 32-bit targets, and they don't like the set of warnings issued to change too much for different targets.

Curiously that linter rule source has a very in-depth discussion of "setting the limit", but entirely disregards that it is the sum of parameter sizes, not the individual parameter (which is all the linter looks at).

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

#13
A good rule is to let the caller decide. If input parameters are by-reference and the caller wants to make them by-reference, congrats, no problem. If caller wants the parameters to by a copy, they can create a clone and pass a reference. But if you impose the input parameters to by copy, there's no way back.

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

#15
> Blech! Having to explicitly borrow temporary values is super gross.

You can cut down on a lot of that by implementing basic operations (Add, Mul, etc.) for both value and reference types.

    impl Add for Vector3 { … }
    impl Add for Vector3 { … }
    impl Add for &Vector3 { … }
    impl Add for &Vector3 { … }
(The first three can delegate to the last one, unless there are value-specific optimizations you want to apply.)

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

#16
Interesting article. I enjoyed it.

I have two thoughts.

====

1. Regarding the benchmark itself, I wonder out loud if CPU caching could have a meaningful alter on the results. The article says :

> I randomly generate 4000 spheres, capsules, segments, and triangles.

It is not clear to me if this is enough to fill CPU L1 cache or not. My guess is that it is not. If all the benchmark indeed happens within the L1 cache, I wonder how a bigger workload would affect the results. Maybe it does not change anything. But maybe it does.

====

2. The author mentioned two criterion for choosing between by-copy and by-borrow : performance and ergonomics. I would add a third, somewhat related to ergonomics : let's call it semantics. When some piece of data naturally belongs somewhere and some others entities are natural "readers" of it, it might make sense to use the ownership / borrowing mechanism of Rust to naturally reflect this relationship, regardless of performance or code ergonomics.

In particular, a borrow guarantees you always work on a read-only, up to date value of the data. Meanwhile, after a pass by-copy the life cycles of the data seen by the caller and the data seen by the callee become independent of each other. This can have consequences, particularly in a multi-threaded environment.

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

#17
post #8

This purports to discuss pass-by-copy or reference but ends up not discussing that particular issue at all since every single compiler naturally inlined and vectorized his trivial micro-benchmark. If you were to discuss the headline issue, I guess you could delve deep into calling conventions, register pressure, various instruction set extensions that compilers can abuse to carry the data.. or you take the compiler o…

[deleted]

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

#20
The Cpp Core Guidelines suggest that the cutoff between copy and const reference should be "up to two or three words" (item F.16). Following this advice, the f32 vector would be passed by value, and the f64 vector would end up in the grey zone.

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppC...

Post reply on HN