Discussion

Why Rust easily optimizes out smart pointers where C++ cannot?

I think that this example can be both entertaining and educating. Smart pointers are often used in high-performance code (like browser engines) so it is interesting to learn how their usage impacts the generated code.

Here is a simple function that adds two numbers using smart pointers. Rust compiles it into two instructions [1], while C++ creates about 100 instructions [2] including unnecessary memory locks.

I wonder, is there a way to improve C++ code so that the smart pointers get optimized out? Did I make some mistake that prevents optimization?

[1] https://godbolt.org/z/YqYK3sPdd

[2] https://godbolt.org/z/bMrhTsjxz

28 points codedokode 4 years ago 60 comments

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton