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.
Should small Rust structs be passed by-copy or by-borrow? (2019)
41–50 of 238 posts
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#42This is one of the problems I have with writing rust code. You have to think about so many mundane details that you barely have time left to think about more important and more interesting things.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#43This is one of the problems I have with writing rust code. You have to think about so many mundane details that you barely have time left to think about more important and more interesting things.
What did you have in mind?
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#44Earlier quoted context omitted.
Agreed - and this applies in nearly every language: start simple, trust your compiler, and optimize only when performance becomes untenable.
The assumption behind such arguments is when a performance problem does arise, a profiler will point to a single, easy to fix, smoking gun. Unfortunately this is not always the case. Performance problems can be hard to diagnose and hard to fix. A lot of damage has been done by unexamined / dogmatic "root of all evil" mantra.
Situations otherwise are the exception, rather than the rule, and it takes an expert to (1) recognize those situations and (2) know exactly how to write optimized code in that situation.
That's why "don't prematurely optimize" is a good rule of thumb - because it works the majority of the time, and it takes experience to know when not to apply it.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#45This is one of the problems I have with writing rust code. You have to think about so many mundane details that you barely have time left to think about more important and more interesting things.
Well, it is a systems programming language. Thinking about exactly how the language passes bits around is the whole point. Rust should specify a stable ABI already so that everyone can form a good mental model of what their code becomes once compiled.
Rust is probably better used for writing fast low-level libraries that you call from higher level languages, possibly with a garbage collector, so you don't waste time thinking about memory management while you design/write your high-level application.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#46I don’t think you ever have to write code like this. Implement your math traits in terms for both value and reference types like the standard library does.
Go down to Trait Implementations for scalar types, for instance i32 [1]
impl Add for &i32
impl Add for i32
impl Add for &i32
impl Add for i32
Once you do that your ergonomics should be exactly the same as with built in scalar types.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#47Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#48This 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)
#49I 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.
Of course, if your struct is truly enormous, you may want to break this rule to avoid large copies. But in that case you probably want to Box the struct anyway.
Of course, if your struct contains something that can't be copied--like a Vec--you'll have to decide whether to clone the whole struct (and thus the vector in it), pass the struct by-borrow, or find some other solution.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#50Earlier quoted context omitted.
Because rust is a compiled language and therefore it means you compile your code to a certain architecture. Who told you about syscalls ? There are systems not using syscalls
There are no syscalls or equivalent operating system calls in the code paths measured. The architecture is also independent of the operating system, with exceptions in some languages for the calling convention (not in rust, afaik, or at least rust makes no guarantees there.)