News for hackers. Stuff that matters.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by Diggsey on Saturday December 31, 2022 @01:17PM (#34199060)
One neat thing here is that the compiler is aware of which types are `Copy` and not internally mutable (not contianing an `UnsafeCell`). For these types, passing `&T` and `T` are equivalent, so the compiler could just choose the faster option.

Even if it's not smart enough to do that today, it could implement this optimization in the future. This could work even without inlining, since the Rust calling convention is unstable, and an optimization based on type size could be incorporated into it.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by zozbot234 on Saturday December 31, 2022 @01:21PM (#34199118)
It would be more advisable to add this as a clippy hint, because `&T` and `T` are not always equivalent wrt. FFI.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by kibwen on Saturday December 31, 2022 @02:59PM (#34200135)
Indeed, but the compiler is still capable of doing it on a case-by-case basis. Quite often the observed semantics are identical and it's easy for the backend to see that a pointer has been created only to be immediately dereferenced.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by cwzwarich on Saturday December 31, 2022 @02:51PM (#34200052)
It would be nice if Rust could do this, but it breaks backwards compatibility. Some existing code depends on pointer values of &T being equal or not equal.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by comex on Saturday December 31, 2022 @04:40PM (#34201050)
As an addendum, LLVM can automatically perform the “&T-as-T” optimization (without inlining) in some cases where the callee function is in the same compilation unit and known to not care about the pointer value. However, these types of optimizations tend to be fragile, easily disturbed when things get slightly complex.