r/cpp • • Jan 11 '26

CppCon Making C++ Safe, Healthy, and Efficient - CppCon 2025

https://youtu.be/p52mNWsh-qs?si=Bz6pyW6d0SMLCZxV

Now with some updated content since the ACCU talk, and the Q&A is nonetheless interesting.

54 Upvotes

103 comments sorted by

View all comments

Show parent comment

4

u/trailingunderscore_ Jan 11 '26

Its going to be extremely expensive to check

What are you basing this on? Google enabled a lot of bounds checking in their codebase, and the cost was a 0.30% performance penalty.

21

u/SophisticatedAdults Jan 11 '26

It's a bit more complicated than that. Yes, bounds checking for e.g. arrays is cheap. Fully featured checks that aim to prevent any access to wrong memory (e.g. use after free) would be much more expensive.

IIRC Google's work only applies to spatial bound checks, not temporal ones. If you want full temporal bound checks you're probably going to reinvent something like Fil-C and it's going to get much more complicated (and expensive).

1

u/trailingunderscore_ Jan 11 '26

would be much more expensive.

How so?

6

u/SophisticatedAdults Jan 11 '26

Because this requires some sort of runtime (or error correction/parity/pointer tagging scheme) to track whether the lifetime of an object in memory is valid or not.

Bounds checking for arrays is easy, just check if the index is within bounds.

-2

u/[deleted] Jan 11 '26

Bounds checking for arrays is easy, just check if the index is within bounds.

that only requires an extra couple bytes of overhead so that the program can know the bounds. not a big deal.

some sort of runtime (or error correction/parity/pointer tagging scheme) to track whether the lifetime of an object in memory is valid or not.

that would require a single extra byte set during construction/move/dtor so the program can know if the pointer is valid. Totally undoable.

9

u/ts826848 Jan 11 '26 edited Jan 11 '26

that would require a single extra byte set during construction/move/dtor so the program can know if the pointer is valid.

I'm not sure a single byte would be sufficient in general (or at least without that "single byte" being present so frequently as to make for quite a bit of overhead). For instance, consider pointers/views into subspans/subobjects (e.g., std::span, std::string_view, references to elements in a container, pointers to subobjects, etc.). Those don't necessarily know where the boundaries of the object they're pointing to are, so they can't easily check your proposed liveness byte. If you want to use liveness bits/bytes I think you'll need much finer granularity a la ASan shadow memory, and that is quite a bit more expensive.

-1

u/[deleted] Jan 11 '26

so you're suggesting that because it doesn't solve all problems that it shouldn't be used to solve any problems?

I mean just go use rust if you want to use rust. why bother mucking up a perfectly good C++?

If you deference a pointer in rust that's unsafe anyway so why should it be any different in c++? Why would you need to dereference any pointers if you can just copy them and know if they're alive or not? the point of references in C++ is they are always valid (unless they aren't and its not enforced) so if you can just test any pointer for validness and always know for 100% certainty whether its valid, references become pointless.

8

u/ts826848 Jan 12 '26

so you're suggesting that because it doesn't solve all problems that it shouldn't be used to solve any problems?

Talking about tradeoffs of different approaches is fine if you're open about that being your approach. Chiming in to a subthread about the expense of "[f]ully featured checks that aim to prevent any access to wrong memory (e.g. use after free)" with an ostensibly inexpensive approach and saying nothing else would tend to imply that that alternative approach is comparable in terms of capabilities. If not, that probably would have been a nice thing to mention.

Furthermore, a solution that addresses a subset of a larger problem space needs to have some kind of consistent rules/approach/algorithm/etc. for how it works, but it's not clear to me what those would be for your approach. For instance, consider the following function:

void set_int(int& i) {
    i = 42;
}

How would you propose this be checked with your "single extra byte", especially if/when the function isn't inlined? If this solves some problems as you imply, which ones does it solve/doesn't solve and how does the compiler/runtime know how to differentiate between the cases it does solve and the ones it doesn't?

If you deference a pointer in rust that's unsafe anyway so why should it be any different in c++?

Obviously it doesn't need to be, but the entire reason these kinds of conversations are taking place is because Rust provides safe and generally-usable alternatives while C++ doesn't and people are interested in adding similar/comparable capabilities for C++.

the point of references in C++ is they are always valid (unless they aren't and its not enforced)

Well yes, that's the rub, isn't it? In this context of this subthread (i.e., discussing the expense of temporal memory safety), references and pointers suffer from the same temporal issues.

so if you can just test any pointer for validness and always know for 100% certainty whether its valid, references become pointless.

I disagree. Even if you ignore validity issues, references and pointers still have different properties/capabilities (e.g., no pointer arithmetic on references, nullptr is a valid value for pointers, etc.), so if you want reference semantics without the other capabilities that pointers offer then references will still have a use.

-8

u/germandiago Jan 11 '26

What are you basing this on?

On being a Rust fan beyond the metrics I guess by lots of comments I see around :)

20

u/James20k P2005R0 Jan 11 '26

I wish we could have a discussion about safety in C++ without people disagreeing with the status quo being immediately accused of being Rust fanboys, just because Rust is an ok language with some good ideas. Its both unproductive and unprofessional - some of us have been C++ programmers for decades

-6

u/germandiago Jan 11 '26

This comment is not an isolated comment tbh. I have seen lots of comments and I can understand your preferences.

But sometimes you are just not objective. For you the paradigm of safety seems to be Rust. Which is ok.

But, as pointed out in another comment around here: you disregard some run-time checks as disastrous without metrics, yet mentioned in the past Google-only metrics for safety being conclusive about codebases.

Namely, you give selective credit (or not) to safety depending on the side of the story.

That is where my comment comes from. It is also obviated the fact that systems programming has lots of unsafe patterns and that Rust just helps by using unsafe blocks but now maintaining the invariants by hand is even more difficult than in C or C++, which do not have a borrow checker.

So in this context, Rust might be the worse solution given a properly modern C++ toolchain bc it gets a lot in the way.

Just to name a few things from the top of my head.

I am now reading Effective Rust, and so far I think the language is very reasonable and well structured. But I still think that in some ways it is more rigid. I need to go to the unsafe part in detail, but I already read and I think I mostly understand Rust traits, enums, iterators and most common traits.

16

u/James20k P2005R0 Jan 11 '26 edited Jan 11 '26

But, as pointed out in another comment around here: you disregard some run-time checks as disastrous without metrics, yet mentioned in the past Google-only metrics for safety being conclusive about codebases.

Some runtime checks are more expensive than others /shrug. Array bounds checks are cheap. Thread safety runtime checks are expensive. None of this is new information, check out the overhead of running ub/thread/asan, which is the closest thing to an implementation of the OP

The safety checks I think should be enabled to be checked at runtime are dictated by how well they map to the hardware on a low level. Modern hardware makes some checks cheap to the point of free, and others are extremely expensive

If you want a list:

  1. The google performance numbers you reference do indeed show that array bounds checks are basically free
  2. Similarly, 0-init is also free, usually. Nonzero init has a much higher performance overhead. Therefore I think we should 0-init. Because its only usually free, we need an opt-out, which we're getting. This is largely all a good move
  3. Signed integer overflow being defined is virtually free, modulo compiler optimisations. Therefore I think it should be defined, with opt-outs for compiler optimisations
  4. Runtime checks for memory errors, which is being proposed in the talk we're watching, have a ~2x performance overhead in what seems like a comparable implementation. I think this is unacceptably high, and don't think this should be enabled by default. There are alternative models with 0 performance overhead that provide more safety, therefore I think they are a better model
  5. Runtime checks for thread safety issues have a 2-20x performance overhead, and a 5-10x memory overhead. I think this is unacceptably high, and think we should explore proven alternatives with no performance overhead

I'll be happy to update my figures here if new information crops up. Fil-C provides memory safety in a similar vein (and in many ways is exactly what is being described in the OP), but it also has a high performance overhead vs alternative techniques. It has a 100% memory overhead for all pointers. Its performance overall is a mixed bag vs asan, eg check out here which seems like the most direct comparison I can find

If you have any further information on runtime safe C++ implementations and their performance please let me know, but this is why I advocate for only certain classes of runtime checks and claim that others will likely be disastrous

It is also obviated the fact that systems programming has lots of unsafe patterns and that Rust just helps by using unsafe blocks but now maintaining the invariants by hand is even more difficult than in C or C++, which do not have a borrow checker.

Only a small minority of Rust is unsafe, even in systems programming applications. Given that many major companies which need systems programming languages are moving to Rust from C++, its clearly providing a major benefit for them