CppCon 2019: Chandler Carruth “There Are No Zero-cost Abstractions”
CppCon7 HN recommenders · recommended in 7 months, 2020–2026
talk · advanced · evergreen · 2020–2026 on HN
Chandler Carruth argues in a CppCon 2019 talk that C++ has no truly zero-cost abstractions. Using examples like unique_ptr, ABI constraints and compile-time costs, he shows that abstractions cost runtime, compile time or human effort.
HN commenters cite it whenever zero-cost abstraction claims come up, especially for why unique_ptr is not free on some ABIs.
Up next
3 mo on HNApple's Independent Repair Program is a useless PR stunt.
Louis Rossmann
3 HN recommender · 2020–2021
3 mo on HNWhy Machines That Bend Are Better
Veritasium
3 HN recommender · 2019–2021
1 mo on HNTom Chi: How to Address our Carbon Debt to Heal the Planet
Foresight Institute
2 HN recommender · 2019–2022
3 mo on HNKedi - Full Length Documentary
vlogbrothers
3 HN recommender · 2018–2022
1 mo on HN[#36] Zig Roadmap 2024 - Andrew Kelley
Zig SHOWTIME
1 HN recommend · 2024–2025
2 mo on HNKeynote: The Last Hope for Scala's Infinity War - John A. De Goes
Scalapeño
2 HN recommender · 2018–2021
11 mo on HNCppCon 2015: Andrei Alexandrescu “std::allocator...”
CppCon
7 HN recommender · 2017–2025
12 mo on HNLexical Scanning in Go - Rob Pike
GoogleFOSSSydney
13 HN recommender · 2012–2025
10 mo on HNCppCon 2017: Matt Kulukundis “Designing a Fast, Efficient, Cache-friendly Hash Table, Step by Step”
CppCon
9 HN recommender · 2018–2026
9 mo on HNRustConf 2018 - Closing Keynote - Using Rust For Game Development by Catherine West
Rust Programming Language
9 HN recommender · 2018–2023
10 mo on HNCppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17”
CppCon
13 HN recommender · 2016–2024
9 mo on HNIdeas about a new programming language for games.
Jonathan Blow
7 HN recommender · 2014–2025
12 comments from Hacker News
Yes, tech talks don't need to be boring and I agree on many points on the post. But also not just 12 slides, more like hundreds of slides. One example I have is Chandler Carruths talk in CppCon 2019 "There Are No Zero-cost Abstrations" ▶ this video Chandler presents his point clearly and goes deep enough into the subject.
When it comes to the overhead issues of unique_ptr, I always think of this video here: ▶ this video At 26:50 he gets into how this relates to ABI, and how 'fixing' some of this would require a pretty severe ABI change, as well as destructive move semantics, which would require a severe change to how C++ works. I frankly don't understand C++ well enough to fully judge about all of this, but everything he says seems entirely solid, and showcases how unique_ptr is in fact not zero-cost, and can in fact be a significant performance trade-off when you really need that extra bit of performance. > Rust occasionally forces my hand considerably harder (e.g. RC<RefCell<..>> vs std::shared_ptr<>)) in the direction of performance compromise. How so? Is RC<RefCell<T>> more expensive than std::shared_ptr<T>?
> std::unique_ptr<Foo> doesn't have any overhead over Foo* in code where semantics is the same Not true. And you just refuted yourself by being up the ABI question. You should watch Titus Winters' presentation on this topic where he compiles code using unique_ptr and raw pointers, compares the assembler produced, and explains why unique_ptr has non-zero overhead. The overhead is indeed coming from the ABI. Google has been wanting the next C++ to break ABI but they didn't get enough votes in the standard committee. ▶ this video
There are no zero cost abstractions. (According to Chandler Carruth, and he makes a convincing point). ▶ this video
The other time that you saw it was also probably me. It's from this talk, which is about how a large amount of generated protocol buffer code at Google led to a quadratic increase in compile times: ▶ this video . TL;DW: The reasoning is because if you use a distributed build system, then your compile time is gated by the file with the longest compile time (tail latency). The more files you have, the greater chance that one of them takes a long time. When you generate source files, you tend to produce more files than if you didn't. Most users don't use a distributed build system to compile the kernel, so on further thought, in that case compile times probably scale closer to linear with the number of translation units. But wasted cycles are still wasted cycles, and regardless of how exactly compile times scale, you should still consider the cost of longer compile times when you duplicate code. With regards to link time optimization: sophisticated analyses take superlinear complexity: link . Disclaimer: I work at Google.
With the caveat that I am NOT suggesting this justifies choosing C over C++, I just wanted to mention this talk about how "zero-cost abstraction" is an idealism, not necessarily a reality: ▶ this video That said, I tried to reproduce something similar, and it seems the issue only occurs in my example due to external linkage (adding 'static' fixes it)... but I can't claim this will always resolve the issue: link
The non-destructiveness of move semantics in C++ is very annoying. It causes for example `std::unique_ptr` to not be a zero-cost abstraction in all cases. See ▶ this video for a nice expose on this.
"For 95% of all code that is correct. std::make_* is easy to use and prevents a lot of mistakes, while having no loss of performance." For 95% of code std::unique_ptr has no loss of performance. Perhaps. Just remember that it is not a zero-cost abstraction, the compiler won't always be able to entirely eliminate the overhead: ▶ this video
> Rust makes it possible to safely manage memory without using a garbage collector, probably one of the biggest pain points of using low-level languages like C and C++. In C++, memory management has not been a pain point for many years, and you basically don't need to do it at all if you don't want to. The standard library takes care of it well enough - with owning containers and smart pointers. > And Rust is famous for its optimizations in the style of "zero cost abstractions". No, it isn't that famous for those. The safety and no-UB constraints prevent a lot of that. By the way, C++, which is more famous for them, still struggles in some cases. For example, ABI restrictions prevent passing unique_ptr's via single registers, see: link (or see Chandler Carrouth's CppCon 2019 talk: "There are no zero-cost abstractions" ▶ this video )
What is often meant by that is "This is a zero run-time cost abstraction". That's a very different thing from a zero cost abstraction. Even if the abstraction actually has a zero run-time cost, which is absolutely not a give, it still has a compile-time cost and a human-time cost. See for example: ▶ this video
> So you're (primarily?) referring to "cognitive overhead" rather than more usual meaning of overhead as in "runtime overhead" I'm referring to both. The issue is that all that complexity makes it very difficult for programmers to write code that the compiler is consistently able to optimise to (or close to) zero overhead. It can be done with enough work and care but for your average C++ developer, they aren't going to be seeing zero overhead for a lot of the things that C++ generally leads us to assume are zero overhead. > Can you given an example? To copy the link that TwentyPosts provided (which is coincidentally also the first thing that came to my mind, albeit at a different timestamp) ▶ this video . The issues with `unique_ptr` in particular are that: - They force the compiler to generate exception code where maybe there isn't any necessary if you pass them into a function. You can elide this by going through and annotating things that you know can't except with noexcept however most people don't even care about noexcept in the first place and consider it line-noise. - Using move references to unique_ptr instead of pointers adds overhead by forcing the compiler to generate extra memory reads. - Passing by value should be zero cost but it'd require breaking basically every aspect of the C++ ABI to do so. Because of this you still get a double indirection which adds overhead. So your average C++ programmer probably won't do #1 which adds the largest overhead and if you need to
But the problem with zero-cost abstractions is that even if the runtime cost is 0, the compile time (and the human time) can become quiet large. E.g. ▶ this video