A potential lesson here (i.e. I am applying confirmation bias to retroactively view this article as justification for a strongly held opinion, lol): Unless you are gonna benchmark something, for details like this you should pretty much always just trust the damn compiler and write the code in the most maintainable way. This comes up in code review a LOT at my work: - "you can write this simpler with XYZ" - "but that…
edit: I misread the previous post. Ignore this. How are you using the word simpler? Because to me that implies a combination of more obvious and number of lines of code. Something that a benchmark shouldn't be involved in. For example asking someone to delete 10 lines of code and instead use go's ` net.SplitHostPort` would be an example of "simpler".
Should small Rust structs be passed by-copy or by-borrow? (2019)
91–100 of 238 posts
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#92I always prefer by-borrow. That's because in the future this struct may become non-copy and that means some unnecessary refactoring. My thinking is a bit like "don't take ownership if not needed" - the "not needed" part is the most important thing. Don't require things that are not needed.
Exactly, and if performance at some point matters: benchmark! And I would bet 9 times out of 10 it won't be the bottleneck or even make a measurable difference.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#93Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#94A potential lesson here (i.e. I am applying confirmation bias to retroactively view this article as justification for a strongly held opinion, lol): Unless you are gonna benchmark something, for details like this you should pretty much always just trust the damn compiler and write the code in the most maintainable way. This comes up in code review a LOT at my work: - "you can write this simpler with XYZ" - "but that…
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? (2019)
#95A potential lesson here (i.e. I am applying confirmation bias to retroactively view this article as justification for a strongly held opinion, lol): Unless you are gonna benchmark something, for details like this you should pretty much always just trust the damn compiler and write the code in the most maintainable way. This comes up in code review a LOT at my work: - "you can write this simpler with XYZ" - "but that…
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…
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#96Earlier quoted context omitted.
The thing is not everything can be expressed as a DAG. And finding it towards the end of writing your program after hours of fighting with borrow checker is extremely unpleasant. And I don't think I ever landed in the situation where I could fix the discrepancy by sprinkling in few Rc, RefCells and such. So I prefer to write with RefCells from the start and when I got the thing working and I am ambitious enough then…
There are many many many ways you can express something and it's very likely that one of those ways is a DAG. The issue here is that you are writing C++ code rather than Rust code.
Two separate synced trees? Is it worth it?
> The issue here is that you are writing C++ code rather than Rust code.
How dare you! I'm writing TypeScript code! ;-)
Rust is not Forth. I can write whatever I want and there's nothing wrong with that.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#97Oh neat, that’s my blog. My old posts don’t resurface on HN that often. Lots of criticism of my methodology in the comments here. That’s fine. That post was more of a self nerd snipe that went way deeper than I expected. I hoped that my post would lead to a more definitive answer from some actual experts in the field. Unfortunately that never happened, afaik. Bummer.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#98A potential lesson here (i.e. I am applying confirmation bias to retroactively view this article as justification for a strongly held opinion, lol): Unless you are gonna benchmark something, for details like this you should pretty much always just trust the damn compiler and write the code in the most maintainable way. This comes up in code review a LOT at my work: - "you can write this simpler with XYZ" - "but that…
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#99Earlier quoted context omitted.
As a Rust beginner that likes to learn the hard way I think I have some insights why Rust seems cumbersome and/or hard for programmers trying it. Rust uses syntax that feels familiar but means completely different things than in pretty much any other language. For example '=' doesn't mean assign handle or copy. It by default means move. 'let' doesn't mean create a name for something. It means create physical space fo…
> For example '=' doesn't mean assign handle or copy. It by default means move. Some complain about this, but the fact is there's no such thing as a zero-overhead "copy" for non-trivial types. C++ started out with = meaning clone the object which was an even bigger footgun, and support for move had to be added after the fact.
It's very elegant solution for simple, small data types. But it further occludes how meaningfully Rust is different from everything else because thanks to that = sometimes does mean copy.
Re: Should small Rust structs be passed by-copy or by-borrow? (2019)
#100Earlier quoted context omitted.
How is that semantically different from Rust? in - regular function arguments inout - mut function arguments out - function return Is there any additional information that a compiler can infer from Ada’s parameter syntax?
The difference between passing by reference vs. by value is observable when comparing pointers to the original vs. to the argument. This difference may be unobservable in Ada though (not sure), so Ada would have more freedom choosing between the two.