HackerTube
HomeLongsLikedHistoryWatch laterCategoriesPlaylistsSettings

CppCon 2019: Chandler Carruth “There Are No Zero-cost Abstractions”

CppCon7 HN recommenders · recommended in 7 months, 2020-2026
YouTube
147K views · 7 years ago · talk · evergreen

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.

C, C++, Rust, Go & systems languagesSoftware performance & optimization#c++#zero-costabstractions#unique_ptr#abi#compilers#performance

20 comments from Hacker News

@RossBencina2026 · in “Building a Fast Lock-Free Queue in Modern C++ from Scratch”

"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

CitesView on HN
@einpoklum2025 · in “The Compiler Is Your Best Friend, Stop Lying to It”

> 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 )

CitesView on HN
@_aavaa_2024 · in “The Fundamentals of C/C++ Game Programming (2017)”

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

CitesView on HN
@jacoblambda2023 · in “Scope-based resource management for the Linux kernel”

> 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

CitesView on HN
@TwentyPosts2023 · in “Scope-based resource management for the Linux kernel”

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>?

RecommendsView on HN
@_aavaa_2023 · in “Swift Ownership Manifesto”

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

CitesView on HN
@twic2023 · in “Kobold, a new web UI crate with zero-cost static DOM”

The term comes from C++, where it does have a fairly rigorous definition: link Although: ▶ this video I'm not sure whether Rust people consistently use it with precisely the same sense.

CitesView on HN
@kccqzy2022 · in “C++20, How Hard Could It Be”

> 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

RecommendsView on HN
@metalrain2022 · in “Tech talks don’t have to be boring”

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.

Strongly recommendsView on HN
@cjfd2022 · in “Writing New System Software”

This appears to be incorrect. I have seen a cppcon talk about that. I think it may have been this one: ▶ this video . On the other hand, it is a very, very tiny minority of programmers who have to care about an overhead as low as this. Most of us should not waste one microsecond of our thinking time on this.

CitesView on HN
@kinjba112021 · in “LLVM 13's new pass manager improves Rust's compile times by up to 20%”

I wonder if it's time to move past that original meaning, or perhaps invent a new catchy term to describe the problem. I'm reminded of this CppCon talk ▶ this video

CitesView on HN
@CountHackulus2021 · in “When zero cost abstractions aren't zero cost”

There are no zero cost abstractions. (According to Chandler Carruth, and he makes a convincing point). ▶ this video

RecommendsView on HN
@tylerhou2020 · in “The AMD Radeon Graphics Driver Makes Up Roughly 10.5% of the Linux Kernel”

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.

RecommendsView on HN
@tylerhou2020 · in “What if you knew how computers work? (2015)”

Not sure if this is sarcastic, but... 1) In dynamic languages, it's not possible to detect whether a function is used or not in the general case. For example, consider string accesses on objects. If the compiler is not sophisticated enough to resolve the set of possible strings at compile time (or such analysis would unacceptably increase compile times), then you can't shake out unused methods on that object. [1] 2) For languages like C and C++, the compiler cannot tree shake because it only knows about a single file at a time (translation unit, to be precise). You would have to rely on link-time optimizations to effectively tree shake, but LTO is not well-supported by all toolchains. Tree shaking also has a cost that I mentioned earlier -- it increases compile times. Both LTO and tree shaking in dynamic languages increase compile times superlinearly [citation needed] wrt. the size of your application. As other commenters have mentioned, it's better to avoid including unnecessary libraries in the first place. There are no cost free abstractions, and that applies to tree shaking as well. ▶ this video [1] For the pedants: yes, I know resolving the possible set of values (stricter than "all possible values for that type") for a variable is undecidable in the general case.

CitesView on HN
@Rusky2020 · in “C++20 has been approved”

This is probably not what was meant, but interestingly unique_ptr actually isn't zero-cost on some ABIs: ▶ this video

CitesView on HN
@mehrdadn2020 · in “The problem with C”

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

RecommendsView on HN
@yesbutt112020 · in “Lithium ranked the highest performance web application platform”

Re: metamap being zero cost I thought there were no zero cost abstractions in C++? ▶ this video Still, it isn’t RoR or anything JS so it has that going for it.

CitesView on HN
@aw16211072020 · in “Comparison of C++17, Go, and Java for a next-generation sequencing tool”

> However, do you have any bench / source for std::unique_ptr ? You are the first one I hear giving critics on it. Chandler Carruth talks about this at CppCon 2019 [0]. From a quick review he says part of the reason std::unique_ptr isn't zero-cost right now is: - Objects passed by value (like std::unique_ptr) are passed on the stack instead of in registers, and changing that will be an ABI break - No destructive move means extra temporaries will be created/retained [0]: ▶ this video

CitesView on HN
@blux2020 · in “Move, simply”

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.

RecommendsView on HN
@leni5362020 · in “ABI – Now or Never [pdf]”

The video: ▶ this video I wrote up a reddit post for a possible workaround for removing the overhead. It's standard C++, no ABI break is required. It's not without caveats though: link

CitesView on HN
2013 Keynote: Chandler Carruth: Optimizing the Emergent Structures of C++1:33:02

2013 Keynote: Chandler Carruth: Optimizing the Emergent Structures of C++

CppNow
34K views • 13 years ago
CppCon 2015: Chandler Carruth "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!"1:29:54

CppCon 2015: Chandler Carruth "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!"

CppCon
188K views • 11 years ago
CppCon 2016: Chandler Carruth “High Performance Code 201: Hybrid Data Structures"55:49

CppCon 2016: Chandler Carruth “High Performance Code 201: Hybrid Data Structures"

CppCon
119K views • 10 years ago
CppCon 2014: Chandler Carruth "Efficiency with Algorithms, Performance with Data Structures"1:13:41

CppCon 2014: Chandler Carruth "Efficiency with Algorithms, Performance with Data Structures"

CppCon
235K views • 12 years ago
CppCon 2017: Chandler Carruth “Going Nowhere Faster”1:00:58

CppCon 2017: Chandler Carruth “Going Nowhere Faster”

CppCon
113K views • 9 years ago
CppCon 2016: Chandler Carruth “Garbage In, Garbage Out: Arguing about Undefined Behavior..."57:39

CppCon 2016: Chandler Carruth “Garbage In, Garbage Out: Arguing about Undefined Behavior..."

CppCon
94K views • 10 years ago
Modernizing Compiler Design for Carbon Toolchain - Chandler Carruth - CppNow 20231:35:12

Modernizing Compiler Design for Carbon Toolchain - Chandler Carruth - CppNow 2023

CppNow
71K views • 3 years ago
CppCon 2018: G. Nishanov “Nano-coroutines to the Rescue! (Using Coroutines TS, of Course)”1:04:26

CppCon 2018: G. Nishanov “Nano-coroutines to the Rescue! (Using Coroutines TS, of Course)”

CppCon
40K views • 8 years ago
CppCon 2014: Mike Acton "Data-Oriented Design and C++"1:27:46

CppCon 2014: Mike Acton "Data-Oriented Design and C++"

CppCon
862K views • 12 years ago
CppCon 2016: Herb Sutter “Leak-Freedom in C++... By Default.”1:39:25

CppCon 2016: Herb Sutter “Leak-Freedom in C++... By Default.”

CppCon
149K views • 10 years ago
CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17”1:19:52

CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17”

CppCon
264K views • 10 years ago
CppCon 2017: Fedor Pikus “C++ atomics, from basic to advanced.  What do they really do?”1:14:22

CppCon 2017: Fedor Pikus “C++ atomics, from basic to advanced. What do they really do?”

CppCon
288K views • 9 years ago
CppCon 2019: Matt Godbolt “Path Tracing Three Ways: A Study of C++ Style”55:42

CppCon 2019: Matt Godbolt “Path Tracing Three Ways: A Study of C++ Style”

CppCon
77K views • 7 years ago
CppCon 2015: Herb Sutter "Writing Good C++14... By Default"1:29:06

CppCon 2015: Herb Sutter "Writing Good C++14... By Default"

CppCon
193K views • 11 years ago
CppCon 2014: Jeff Preshing "How Ubisoft Develops Games for Multicore - Before and After C++11"1:04:33

CppCon 2014: Jeff Preshing "How Ubisoft Develops Games for Multicore - Before and After C++11"

CppCon
53K views • 12 years ago
The One-Decade Task: Putting std::atomic in CUDA. - Olivier Giroux - CppCon 20191:05:39

The One-Decade Task: Putting std::atomic in CUDA. - Olivier Giroux - CppCon 2019

CppCon
11K views • 7 years ago
Plenary: Performance Matters - Emery Berger - CppCon 20201:01:09

Plenary: Performance Matters - Emery Berger - CppCon 2020

CppCon
32K views • 6 years ago
CppCon 2016: Dan Saks “extern c: Talking to C Programmers about C++”1:36:27

CppCon 2016: Dan Saks “extern c: Talking to C Programmers about C++”

CppCon
175K views • 10 years ago
CppCon 2017: Carl Cook “When a Microsecond Is an Eternity: High Performance Trading Systems in C++”1:00:07

CppCon 2017: Carl Cook “When a Microsecond Is an Eternity: High Performance Trading Systems in C++”

CppCon
357K views • 9 years ago
Can C++ be 10x Simpler & Safer?  - Herb Sutter - CppCon 20221:54:00

Can C++ be 10x Simpler & Safer? - Herb Sutter - CppCon 2022

CppCon
277K views • 3 years ago