Live data from Hacker News

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

forrestthewoods.com

41–50 of 238 posts

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

#41
post #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.

As long as the calling convention is deterministic from the declaration of the function it should be fine right?

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

#42
post #40

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

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

#43
post #40

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

My (brief) experience with Rust was that, while I had to struggle to learn the borrow-checker, I didn't have lots of "mundane details" to worry about - if any, less than C(++).

What did you have in mind?

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

#44

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

In the vast majority of situations (1) you'll prematurely optimize in the wrong place and (2) yes the profiler will point to a single, easy-to-fix smoking gun.

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)

#45
post #40

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

True, I probably was using Rust for the wrong type of problem, i.e. was hoping to write a user-level application with a graphical UI at the time.

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)

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

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

[1] https://doc.rust-lang.org/std/primitive.i32.html

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

#47
You are comparing two completely different compilers; I wouldn't worry all that much about the difference between rust and C++. If you do want to compare them directly, why not use LLVM for C++ as well? That will highlight any language-specific differences.

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

#48
post #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".

and GL/SL IIRC ...

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

#49

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.

In Rust it's considered idiomatic to pass things by-value whenever you can. Usually this is also the most performant option, since it avoids dereferencing in the callee.

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)

#50
post #22
post #8

Earlier 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.)

However, in practice Rust's calling convention does actually depend on the operating system. So on Linux Rust will make use of the stack red zone, while on Windows it doesn't. (Also some codegen in LLVM depends on the operating system)
Post reply on HN