Hacker News new | ask | show | jobs
by ridiculous_fish 2230 days ago
Here's a case I stumble on: Rust seems to generate unnecessary branches. Compare copying an optional range:

C++: https://godbolt.org/z/VCf638

Rust: https://rust.godbolt.org/z/jRFiw_

I think the key difference here is that C++ allows specializing optional on trivial types - can anyone shed more light?

1 comments

It looks like the C++ version always does the copy, regardless of whether or not the optional is empty, whereas Rust only bothers copying if there's anything there.

Is this a remnant of #[inline] on Option's Clone impl methods?

With "larger" types (e.g. an optional several-GiB array) it seems like this could save some time depending on where things are in memory.