News for hackers. Stuff that matters.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by Patrol8394 on Saturday December 31, 2022 @01:58PM (#34199529)
This x 10000 ! If I had a dime for every time I provided this exact feedback in code reviews…I find surprising that a lot of devs in tech industry are obsessed with pointless micro optimizations and they don’t care about writing maintainable, testable simple code. My final comment is always to not outsmart compilers/jvm because they tend to do a much better job that developers.

Please, don’t optimize unless you have reasons to do so and numbers backing that up.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by ok123456 on Saturday December 31, 2022 @02:09PM (#34199629)
This is true for application code. But, Rust is trying to sell itself as a systems language and an embedded language and a language you can write kernel modules in. Memory budget matters in these cases.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by tialaramex on Saturday December 31, 2022 @02:37PM (#34199888)
If memory budget matters, you have a memory budget. So you should be measuring and you can actually tell where you need improvements.

But in practice what we see overwhelmingly is that people want to do this stuff but they aren't measuring, because measuring is boring whereas making the code more complicated to show off how much you think you know about optimisation is easy. Knock it off.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by ok123456 on Saturday December 31, 2022 @02:44PM (#34199966)
Then knock off trying to use Rust as a systems language. Linear types make refactoring this nearly impossible if it does become and issue.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by tialaramex on Saturday December 31, 2022 @02:58PM (#34200113)
Works really nice for me, of course, I actually measure what I'm doing.

You edited your comment, so I guess I will too: Rust doesn't actually have Linear types. Linear types ("must use or compile error") would be tricky to provide, Aria blogged about it back in the day. So that's definitely going to be a problem with your refactoring.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by dahfizz on Saturday December 31, 2022 @02:13PM (#34199659)
It depends on your specialization, I guess. If you're making a website, a few microseconds here and there probably don't matter.

But in my field (Fintech), performance really does matter. Doing the simple, slow thing is just lazy and won't make it through review.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by imron on Saturday December 31, 2022 @03:54PM (#34200640)
Great. Should be easy to prove then.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by dahfizz on Saturday December 31, 2022 @04:19PM (#34200869)
Yup, we have a good benchmarking suite to make sure changes don't cause regressions and that optimization changes actually work.

That said, I think asking a developer to write everything they do twice so that they can A/B test is overboard. You can come back and really aggressively optimize later, but I think the "default" should be the fast thing, rather than the slow & easy thing.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by bmacho on Saturday December 31, 2022 @05:04PM (#34201237)
If "performance really does matter" (in Fintech) then a developer surely can write everything twice or more.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by dahfizz on Saturday December 31, 2022 @05:27PM (#34201397)
We could, yeah. I'm sure you're capable of re-writing everything you do twice. It's just a huge waste of time.

You get just as much benefit by assigning a performance refactor to an engineer when needed vs literally halving or worse the whole teams productivity.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by gpderetta on Saturday December 31, 2022 @06:34PM (#34201860)
Time to market can matter in fintech as much as anywhere else.

Edit: also optimizations often interact with each other and you can't always benchmark all possible combinations, so sometime you do have to rely on experience or reasoning from first principles to decide if an optimisation is worth it or not.

Re:Should Small Rust Structs be Passed by-copy or by-borrow? by elimerl on Saturday December 31, 2022 @05:18PM (#34201340)
The idea is that the "default" is the easy thing, which is usually optimized by the compiler.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by TillE on Saturday December 31, 2022 @07:12PM (#34202139)
When you're writing in a language like Rust or C++, you know broadly what the compiler optimizes and what it probably won't. You do need to do the work yourself a lot of the time.
Re:Should Small Rust Structs be Passed by-copy or by-borrow? by josephg on Saturday December 31, 2022 @02:46PM (#34199986)
My advice is the opposite: if you want to make performance justifications for code, you need a benchmarking suite. I have them for a lot of my projects. (Rust’s criterion is a delight). A good benchmark suite is a subtle thing to write - you want real world testing data, and benchmarks for a range of scenarios. The benchmarks should be run often. For some changes, I‘ll rerun my benchmarks multiple times for a single commit. I benchmark the speed of a few operations, serialisation size, wasm bundle size and some other things.

Having real benchmarking data is eye opening. It was shocking to me how much the wasm bundle size increased when I added serialisation code. The time to serialise / deserialise a big chunk of data for me is 0.5ms - so fast that it’s not worth more microoptimizations. Lots of changes I think will make the code slower have no impact whatsoever on performance. And my instincts are so often wrong. About 50% of microoptimizations I try either have no effect or make the code slightly slower. And it’s quite common for changes that shouldn’t change performance at all to cause significant performance regressions for unexpected reasons.

I’ve also learned how important “short circuit” cases can be for performance. Adding a single early check for the trivial case in a function can sometimes improve end to end performance by 15-20%, which in already well tuned code is massive.

Performance work is really fun. But if you do performance tuning without measuring the results, you’re driving blindfolded. You’re as likely to make your code worse as you are to make it better. Add benchmarks.