[Question] When to pass copy vs ref? #422
-
Hi there, I'm new to Rust and I'm implementing my own vec3 class for practice, but I'm wondering when to pass as copy vs const ref in functions owned by the class. so I figured to check popular math crates and stumbled upon here. Most of the time I see you just copy the other vector and self but occasionally &self is passed and I don't understand when to use what 🤔 code I'm referring to; https://docs.rs/glam/0.24.1/src/glam/f32/vec3.rs.html#172-178 Could you explain it to me please? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There's not really a hard and fast rule and I don't remember the rules of the Rust ABI. Most of glam is inlined, so it shouldn't make too much difference which one is used. Usually I look at the generated assembly and maybe write a benchmark with both styles to see what seems best. Most likely in an optimised build they will be the same, there might be differences in debug builds which can be a consideration In glam I mostly only use &self with large structures like 4x4 matrices and even then, I don't recall if the asm was different, I made those decisions a while ago :) Here's an example in godbolt, with one implementation of |
Beta Was this translation helpful? Give feedback.
There's not really a hard and fast rule and I don't remember the rules of the Rust ABI. Most of glam is inlined, so it shouldn't make too much difference which one is used. Usually I look at the generated assembly and maybe write a benchmark with both styles to see what seems best. Most likely in an optimised build they will be the same, there might be differences in debug builds which can be a consideration In glam I mostly only use &self with large structures like 4x4 matrices and even then, I don't recall if the asm was different, I made those decisions a while ago :)
Here's an example in godbolt, with one implementation of
cross
takingself
and the other taking&self
, the generated co…