this post was submitted on 24 Jan 2024
33 points (90.2% liked)
Java
1384 readers
1 users here now
For discussing Java, the JVM, languages that run on the JVM, and other related technologies.
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
The reason I said to use rvalue references is because otherwise it is an apples-to-oranges comparison: in the C++ code you have implicit ABI decisions around the call convention and whose responsibility it is to destroy the temporary.
https://godbolt.org/z/9875qMM6Y (or alternatively: https://godbolt.org/z/9xehs3sYP)
The assembly is identical, the ownership is clearly transferred, and this doesn't need LTO or looking at the function bodies and is entirely done by the C++ compiler. It involves using (when available) a vendor attribute (see trivial_abi, shouldn't be an issue given Rust devs are fine with having only one compiler anyway) and writing a
UniquePtr
class (shouldn't be used in production code, what I've given there is only for illustration purposes) that assumes that the custom deleter cannot have an internal state.This is a zero-runtime-cost abstraction. Now whether the zeroing of that cost can depend on what ABI assumptions you're ready to make, or whether you want to depend on LTO is another thing. We're literally discussing a "problem" that is not really a problem because Rust doesn't have the luxury yet to have that problem: you're easily forgetting that Rust has only one compiler.
A project like that usually takes years, so again, very likely that they began working on it years before that. For instance, Google designed Go in 2007 and announced it in November 2009.
So...you had to make your own version of
unique_ptr
to make it zero-cost? Doesn't that just confirm the original statement you were disagreeing with, thatunique_ptr
has a small runtime cost? Or was there some other reason you thought the creator of the video you shared has "no idea" whatunique_ptr
is?I also don't understand why the standard library can't use the trivial-abi attribute. Different implementations of the standard library aren't required to be interoperable, are they?
I still don't understand what you think is "apples-to-oranges" here. If you change the Rust code to require the C ABI, there's no difference in the generated code: https://godbolt.org/z/1xf9qG3n8