by UkrainianJew · 4y ago · 0 comments
static int add_numbers(std::shared_ptr<Data> p) {
return p->a + p->b;
}
Try this: static int add_numbers(const std::shared_ptr<Data> &p) {
return p->a + p->b;
}
You'll get 4 instructions (don't forget to put __attribute__((noinline)) on add_numbers()): add_numbers(std::shared_ptr<Data>&):
mov rdx, QWORD PTR [rdi]
mov eax, DWORD PTR [rdx+4]
add eax, DWORD PTR [rdx]
ret
The only difference is that Rust physically placed the structure in 2 registers (hence just 1 instruction), while C++ physically placed it into memory (hence the need to 2 extra instructions fetching the values). static int add_numbers(Data *p) {
return p->a + p->b;
} namespace {
int add_numbers(const Data& p) {
return p.a + p.b;
}
}This is also happening in the Rust code (x.clone()).
That's 4 instructions only for add_numbers(), but add_two_numbers_smart_way() still is unoptimized.
int add_two_numbers_smart_way(int a, int b) {
auto x = std::make_shared<Data>(a, b);
return add_numbers(x);
}
But you still get what you ask the compiler to deliver. If you don't specifically ask it to count references, it won't: static int add_numbers(Data *p) {
return p->a + p->b;
}
int add_two_numbers_smart_way(int a, int b) {
Data x(a, b);
return add_numbers(&x);
}
Here's the result: add_two_numbers_smart_way(int, int):
lea eax, [rdi + rsi]
ret
To answer the original question, it won't optimize the smart pointers away because the smart pointers are not implemented by the compiler. They are a generic construct telling the compiler to allocate memory, increase reference counter, decrease reference counter and free memory. How does it now this is not a unit test for memory allocation API or atomic operations?A better question would be why C++ went for implementing certain things on the library level instead of the compiler level, and the answer here is probably "historical reasons" and someone's judgement call on flexibility vs performance under specific circumstances.
Does rust try to follow the platform C ABI? C++ implementations typically do when it is semantically meaningful.
Rust doesn’t let you utilize this fact on stable yet, which is what you’re thinking of, but the implementation in this case already exists.
* There will be not more than 1 reference to the object at any time.
* The object will be deleted once the reference dies.
That's a zero-overhead GC that is enforced at compile time.
And that is the main appeal of C++. You can get close-to-optimal instruction-level behavior while still describing things at reasonably high levels of abstraction. Yes, it has a hell of a learning curve, but once you master it, you can beat any other language out there.
Untrue:
auto x = std::make_unique<int>(5);
auto& y = *x; // There are now two mutable references to the object represented by x.
>That's a zero-overhead GC that is enforced at compile time.It's not zero overhead unfortunately. Because unique_ptr is not trivially movable or trivially destructible, there are numerous optimizations that every current C++ compiler does not perform, for example being able to pass a unique_ptr in a register.
unique_ptr is reasonable given C++'s limitations, but it doesn't come close to being as good as Rust's Box, which is genuinely zero overhead and also guarantees that there's only 1 mutable reference to the object at any given time.
It does give you non-ambiguous ownership semantics which is a good thing but there's still plenty of ways to misuse/abuse it.
That said if you can make the aliasing guarantees(such as Rust's borrow checker does) that opens up the possibility of automatically applying restrict semantics[1] which enables further set of optimizations available to the compiler.
Also, I definitely would not call std::unique_ptr (or Box) a GC.
Another one is what happens when you move a value out, in C++ it’s in an “indeterminate state”, but in Rust you can’t access a value after move. Which means that you can get a null pointer exception in C++, but not in Rust, and means that destructors for uniq_ptr have to check for this case, whereas in Rust they don’t.
Rust defaulting to move-semantics and requiring explict copies is one of the best decisions in the language.
You should check out Rust's concept of ownership. The goodness of ownership that is implemented in unique_ptr in C++ exists for every type in Rust.