CppCon 2017: Piotr Padlewski “Undefined Behaviour is awesome!”

preview_player
Показать описание


Undefined behavior (UB) is one of the features of C++ that is both loved and hated. Every C++ developer cares about performance, which is why it is very important to understand what the compiler can optimize and what are the language guarantees. Many times programmers are too optimistic about what the compiler can optimize, or they waste time optimizing code by hand.

In this talk you will learn:
- what is the “as-if” rule
- why compilers know less than the programmer — the main problem with Translation Units
- why compilers optimize based on UB, but don't warn about it
- why Undefined Behavior can transcend time, removing your whole code without running 88mph
- why having a more constrained language is better — optimizations that you can’t do in C

Piotr Padlewski: masters student, University of Warsaw


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

Its pretty obvious “awesome” is meant literally, as in “inspiring an overwhelming feeling of reverence, admiration, or fear”

DPGrupa
Автор

Reminded me of one anti-pattern that I've seen in at least two projects in their legacy code: `if (this == NULL) return;`.
Obviously, the compiler has the right to throw it all away, because `this` is not allowed to be null.

MaceUA
Автор

It's ridiculous how much code compiler can remove but you don't notice it in any way.

doBobro
Автор

Useful part: 16:17, 20:23-21:09, 29:51, 35:00-35:37, 43:34.
1. Your if statement about checking nullptr wil be considered by Compiler to check undefined behaviour.
2. Don't forget return statement at every branch.
3. Undefined Behaviour means assuming not happen, to speed up.

In short: don't write smart (short) code then you will be fine.

王甯-hx
Автор

49:00 Regarding the memory allocation thing: You know, you can just turn on strict accounting (by turning off overcommit). That has other problems, though.

nullplan
Автор

@ 33:00 when i compile that with clang 10, it emits an ud2 instruction, or 0F0B. "Raise invalid opcode exception". When run, this will trigger a segfault with "illegal hardware instruction (core dumped)"

FalcoGer
Автор

51:47 - was that Niko Matsakis(sounded like him) at CppCon 2017? :)

aliancemd
Автор

25:00 if it would not be static then that means, there could be somewhere extern in another compilation unit and initialization before main and it could not do this.

michalmarszaleck
Автор

Is "Behaviour" in the title of the video a typo or something else?

kamilziemian