Live data from Hacker News

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

forrestthewoods.com

1–10 of 238 posts

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

#4
My first thought was "now what is the calling convention for float parameters again? they are passed in registers right? the compiler can probably arrange so they don't have to actually be copied" and then I realized it will probably even inline it.

Anyway, assuming it's not inlined I would guess pass-by-copy, maybe with an occasional exception in code with heavy register pressure.

Edit: Actually since it's a structure, the calling convention is to memory allocate it and pass a pointer, doh. So it should actually compile the same.

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

#5
post #4

My first thought was "now what is the calling convention for float parameters again? they are passed in registers right? the compiler can probably arrange so they don't have to actually be copied" and then I realized it will probably even inline it. Anyway, assuming it's not inlined I would guess pass-by-copy, maybe with an occasional exception in code with heavy register pressure. Edit: Actually since it's a structu…

> Edit: Actually since it's a structure, the calling convention is to memory allocate it and pass a pointer, doh. So it should actually compile the same.

Depending on calling convention, the structure may be spread out into registers.

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

#7
post #6

It is a problem of statistics and depends on internals of underlying operating system. I’m not sure you really need that sort of optimisation

What does this have to do with the operating system? There are no syscalls in the code measured here.

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

#8
post #7
post #6

It is a problem of statistics and depends on internals of underlying operating system. I’m not sure you really need that sort of optimisation

What does this have to do with the operating system? There are no syscalls in the code measured here.

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

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

#9
post #7
post #6

It is a problem of statistics and depends on internals of underlying operating system. I’m not sure you really need that sort of optimisation

What does this have to do with the operating system? There are no syscalls in the code measured here.

The dependency is on the ABI, which can be OS-dependent. Also, it is a depressingly manual optimization to do: compilers don't know when it is safe to change a reference to a copy (for example) without an analysis of future code that they don't do.

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

#10
I'd be interested to know what the benchmarks of the two rust solutions are when inlining is disabled so we can get an idea of the different performance characteristics of each function call even if it's not a very realistic scenario.

The other question I have is which style should you use when writing a library? It's obviously not possible to benchmark all the software that will call your library but you still want to consider readability, performance as well as other factors such as common convention.

Post reply on HN