Skip to content
0:00 / 0:00

#c++#zero-costabstractions#unique_ptr

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

CppCon

189K subscribers • 103 videos on Hallway • Conference channel

YouTube
  • Recommended by 7 people: 7 on Hacker News.
  • A classic. People kept recommending it from 2020 to 2023.
  • Presented by Chandler Carruth, C++ compiler lead at Google
  • Linked in the descriptionCppCon/CppCon2019
  • An opinion. It argues one side of a question engineers disagree on.

Advanced. Deep dive for people who know the field.

147K views7 years ago7 recommendations

Programming languagesSystems programmingC++Performance and optimizationCompilers and interpreters

Chandler Carruth argues that C++ abstractions always carry costs in runtime, compile time, or human effort, using unique_ptr as a case study.

CppCon.org — Discussion & Comments: reddit.com/r/cpp/ — Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/CppCon/CppCon2019 — C++ is often described as providing zero-cost abstractions. Libraries offer up facilities documented as such. And of course, users read all of these advertisements and believe that the abstractions they are using are truly zero-cost. Sadly, there is no truth in advertising here, and there are no zero-cost abstractions. This talk will dive into what we mean by "zero-cost abstractions", and explain why it is at best misleading and at worst completely wrong to describe libraries this way. It will show case studies of where this has led to significant problems in practice as libraries are designed or used in unscalable and unsustainable ways. Finally, it will suggest a different framing and approach for discussing abstraction costs in modern C++ software. — Chandler Carruth Google — Videos Filmed & Edited by Bash Films: BashFilms.com Work at Hudson River Trading (HRT): tinyurl.com/safxfctf

Why it's here

7 people recommend this

What people say. Highly recommended as a deep dive into the hidden costs of C++ abstractions and ABI constraints.

Moments people linked to

metalrainon Hacker News • Recommends

On “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" link to this video Chandler presents his point clearly and goes deep enough into the subject.

bluxon Hacker News • Recommends

On “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 link to this video for a nice expose on this.

mehrdadnon Hacker News • Recommends

On “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: link to 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

tylerhouon Hacker News • Recommends

On “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: link to 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…

CountHackuluson Hacker News • Recommends

On “When zero cost abstractions aren't zero cost”

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

kccqzyon Hacker News • Recommends

On “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. link to this video

TwentyPostson Hacker News • Recommends

On “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: link to 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 di…

u/tedbradlyin r/ProgrammingLanguages • Mentions

On “Mojo 🔥: A programming language for all AI developers”

Seems like a pipedream. You can't have tons of great abstractions while performing the same as a highly tuned solution in a low-level language. That's like asking C++ to be as fast as C even if you extensively use C++ abstractions. Take a look at this cool talk if you want concrete examples of how C++ is slower if you use all its features. It might be a design goal of C++ to have zero-cost abstractions, but it's just not possible. Everyone would use this language for everything if it were like Python and as fast as C. It should say enough that Linus Torvalds hard banned C++ in the Linux kernel due to performance concerns. (Some Rust is being used now though) The fact is if you need maximu…
3View on Reddit

u/BobSanchez47in r/Zig • Mentions

On “New to Zig. I *love* what I see.”

And Rust satisfies both (1) and (2) for most of its zero-cost abstractions. For arrays, if you wish to avoid the bounds check, you can do so using unsafe code. However, in almost all cases, this is unnecessary; if it is obvious to the programmer that you can avoid the bounds check, it’s usually obvious to the compiler as well. And arrays are almost always used via their iterators, which are zero-cost. Options are also generally zero-cost when used as control flow. In most cases, the compiler optimises away the creation of the option using inlining. There is thus nothing to unwrap - instead, at the time the code is written return a `None` that is to be unwrapped, the compiled code will pani…

leni536on Hacker News • Mentions

On “ABI – Now or Never [pdf]”

The video: link to 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

aw1621107on Hacker News • Mentions

On “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: link to this video

yesbutt11on Hacker News • Mentions

On “Lithium ranked the highest performance web application platform”

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

Ruskyon Hacker News • Mentions

On “C++20 has been approved”

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

tylerhouon Hacker News • Mentions

On “What if you knew how computers work? (2015)”

…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. link to 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.…

kinjba11on Hacker News • Mentions

On “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 link to this video

cjfdon Hacker News • Mentions

On “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: link to 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.