Embracing (and also Destroying) Variant Types Safely - Andrei Alexandrescu - CppCon 2021

  Рет қаралды 28,797

CppCon

CppCon

Күн бұрын

cppcon.org/
github.com/CppCon/CppCon2021
---
Designing and implementing a variant type (such as `std::variant` itself) in C++, at first, seems a rather inane topic. It turns out, however, that a variant type's performance or lack thereof, can have an inordinately large impact on software that directly impacts the bottom-line performance of important applications, such as interpreters. database clients, and communication protocols.
In this talk we will delve into the gory details of designing and implementing variant types, with an emphasis on layout, construction, and destruction. Along the way we'll figure out how to optimize not only the run time of the produced program, but also the time and memory consumed during compilation. These aspects, although seldom discussed, are important for provisioning and deploying continuous integration systems.
---
Andrei Alexandrescu
Andrei Alexandrescu is a researcher, software engineer, and author. He wrote three best-selling books on programming (Modern C++ Design, C++ Coding Standards, and The D Programming Language) and numerous articles and papers on wide-ranging topics from programming to language design to Machine Learning to Natural Language Processing. Andrei holds a PhD in Computer Science from the University of Washington and a BSc in Electrical Engineering from University "Politehnica" Bucharest. He is the Vice President of the D Language Foundation.
---
Videos Filmed & Edited by Bash Films: www.BashFilms.com
KZbin Channel Managed by Digital Medium Ltd events.digital-medium.co.uk
*--*

Пікірлер: 54
@Cookipolation
@Cookipolation 2 жыл бұрын
Alexandrescu is one of my favorite stand up comedians, in part because he mixes in some of the best c++ content out there
@shoulderstack5527
@shoulderstack5527 2 жыл бұрын
Have you seen Stroustrup? He uses a dead-pan delivery, never laughs at all, like he is dead serious. Even off-stage he never drops the illusion... genius.
@a314
@a314 2 жыл бұрын
@@shoulderstack5527 That's being 'narcistic', not 'genius'. Modesty and humility is equally important to be a genius. Einstein was a 'genius'.
@yunlongliu9726
@yunlongliu9726 2 жыл бұрын
well narrated.
@vimfrw
@vimfrw 2 жыл бұрын
I (re)watch his talks whenever I need a doze of intellectual laughter ;)
@origamibulldoser1618
@origamibulldoser1618 Жыл бұрын
That's an odd requirement for genius. I thought it was simply doing ingenious things.
@a314
@a314 2 жыл бұрын
Brilliant talk. I have watched all the talks by Andrei online over the last 2 decades or so. Brilliant, fun filled man. It's kinda nostalgic to see him in grey hair. Wish him all health and long life!
@embeddor3023
@embeddor3023 2 жыл бұрын
Andrei always makes the best talks at cppcon
@Snarwin
@Snarwin Жыл бұрын
10:47 I think this is the most important takeaway from this talk-"static for needed for simple implementation". All of the clever implementation techniques discussed in the latter part of the talk are, essentially, workarounds for the fact that C++ does not have a "static for" loop.
@rhubarbjin
@rhubarbjin 2 жыл бұрын
16:11 "Voldemort types" is my new favorite way to refer to lambda expressions. XD
@victornoagbodji
@victornoagbodji 2 жыл бұрын
Always a pleasure to listen to Andrei talk. Thanks for sharing 😊
@CppCon
@CppCon 2 жыл бұрын
Glad you enjoyed it!
@LogicEu
@LogicEu 2 жыл бұрын
This guy just keeps impressing me, great talk!
@ShtacketT
@ShtacketT 2 жыл бұрын
There is a 3rd thing one can do with packs: get it's length with sizeof...(Ts)
@Lalasoth
@Lalasoth 2 жыл бұрын
He always has such interesting videos. Always learn from him.
@CppCon
@CppCon 2 жыл бұрын
Glad you like them!
@robertjmccabe
@robertjmccabe 2 жыл бұрын
Very intelligent and enjoyable talk
@userrand4900
@userrand4900 Жыл бұрын
The example in slide 24 is broken. This was asked by a member of the audience and a non explanation was provided by the presenter. int b = f(1,2.5) Given that Ts have to be explicitly specified it will be empty in this case, and Us will always be empty due to the way matching rules work, the only way to make this work is by explicitly specifying Ts to capture all of the arguments except the last one (or not specifying it if it's empty as is the case with the first example). So int b = f(1, 2.5) would work and deduce as int f(Ts ..., Us ..., T) [with Ts = {int}; Us = {}; T = double]
@mr_vazovski
@mr_vazovski 2 жыл бұрын
This talk is outstanding!
@yoscot629
@yoscot629 2 жыл бұрын
first(...)! great talk! perhaps instead of the member unsigned d_active, a call to variant.set sets the pointer to the destructor (so no array lookup at destruciton time, but still a function indirection overhead) and d_active can be inferred from that. not a good bargain if set/get used often though.
@FruchteisMitErdbeer
@FruchteisMitErdbeer Жыл бұрын
Nice, I actually got directly to use the ideas from this talk at work. I didn't know you could split explicitly specified and deduced arguments into two distinct packs. That makes implementing something I thought impossible actually quite easy.
@AminAramoon
@AminAramoon 2 жыл бұрын
great talk!
@mujjingun
@mujjingun 2 жыл бұрын
This man is a comedian!
@bryankreinhart
@bryankreinhart 5 ай бұрын
Right at the beginning, when comparing the elements of arrays to see if they are equal, before even looking at the indices of the arrays to compare, it is more efficient to compare the _sizes_ of the arrays (or vectors). If the arrays/vectors are 10 ,12 and 2 elements, then there is no point comparing the elements vertically as you know they are not equal before comparing the values at the indices. If the arrays/vectors have the same number of elements, then you can go and iterate over the index and compare the values. ~(EDIT: corrected typos)
@Bolpat
@Bolpat 2 жыл бұрын
There’s an error on slide 19, also the explanation is wrong. On the last line, there is  f(1, 2, 3, "4"); _// Ts = _ The comment is correct, the instantiation of _f_ makes _Ts_ as mentioned. But _f_ “reverses” _U_ and _Ts._ The argument 2 binds to _U_ (which, correctly stated, must always be deduced), i.e. _U_ = *int.* Arguments 3 and "4" go to *const* _Ts_ & … which is ( *const int* &, *const double* & ). And "4" cannot bind to *double.*
@XiongZou
@XiongZou Жыл бұрын
I assume the f() should be corrected as: template void f(T, TS..., U)
@user-wl1sn8qr5f
@user-wl1sn8qr5f 8 ай бұрын
yes
@user-wl1sn8qr5f
@user-wl1sn8qr5f 8 ай бұрын
Great talk, I like watching such subtleties are being described. By the way it seems there is a mistake about function parameters order, it can be detected at 27:41. In fact, the "3" will go to "Ts". In order to put it to U (and "2" to "Ts"), the function parameters should be declared as follows: f(T, Ts& ... , U). And this cooresponds to that whai is being said about the third line (the presented example can not be compiled because "4" is being bound to double) upd: found this issue in older comments
@userrand4900
@userrand4900 Жыл бұрын
One slide 37: constexpr size_t m = lo + (hi - lo) / 2; if (n < m) { destroyLog(n, p); } else if (n > m) { If n < m is indeed true, the check for (lo ==m) should technically not be needed? Is it there just because the expansion is done at compile time ? In general can someone explain why we need the checks as part of the template parameters?
@johnmcleodvii
@johnmcleodvii 3 ай бұрын
While hash rable lookups can be lineaer (depending on implementation), creating the hash table can get expensive. All is fine until you have a collision. At that point, you have some choices in implementation. Option 1 (bad) is to scan to the next hole. This makes lookups as long as linear as well as inserts being linear. Ehen spaces run out, you will need to increase the size of yhe hash table anyway. Option 2. Buckets. Create a bucket to hold multiple values that hash the same. Now, you have options on how to implement this bucket. But be earned, for large enough n the runtime devolves to ehatever the bucket algorithm is. Why add the extra complexity? Option 3. Increase the size of the hash table. This will cause a rehash of every rntry already in the hash table. Be warned that only by doubling the size of the hash table at each increase can the amortized insert time be kept to ln(n) per insert. Constant size increases amortize to linear time per insert. Hash tables are also typically quite sparse when the first collision occurs. My takeaway is that hash tables work well for moderate size data sets where the size of the data set is fairly well known in advance.
@wotcherfaz
@wotcherfaz 2 жыл бұрын
So, Andrei, what's your code going to DO when the next compiler obeys subtley different semantics about 'what expansions are allowed', and where/when the expansions are allowed... ? Scary. (Re. approx 12:45).
@AG-ld6rv
@AG-ld6rv 2 жыл бұрын
This guy was supposed to be a comedian. Good technical side too.
@skyeplus
@skyeplus Жыл бұрын
28:00 - is there a error in slide, was it meant to be f(T, const Ts&..., U)?
@thelatestartosrs
@thelatestartosrs 6 ай бұрын
it seems so, the shown code does not compile and T, Ts, u compiles
@weekendwarrior3420
@weekendwarrior3420 11 ай бұрын
Finally someone is making sense.
@AllothTian
@AllothTian 2 жыл бұрын
Not that C++ makes this easy but wouldn't the ideal solution at the end be to sort all types according to trivial destructibility so their indices are all grouped, then do one of 3 things at compile time: 1. Eliminate the destructor if all types are trivially destructible. 2. If you have mixed destructors, emit a single branch with a constant value and branch into the jump table if the value index is inside the range of types with destructors. 3. Eliminate the branch from (2) if all types have non-trivial destructors.
@mijstonen
@mijstonen 2 жыл бұрын
I deeply admire Andrei, for his C++ insights and his humor ;-) , thanks Andrei , you are (also) a great speaker. @0:20:00 you Andrei talk about adding type sequence to the standard, but .... couldn''t we consider std::tuple being such "type sequence" and shouldn't we consider lips alike operations (head,.cons,....) to be applied to a tuple ? What are your (or anyone else) thoughts about it ? Please comment.
@DreyriRS
@DreyriRS 2 жыл бұрын
the way tuple is implemented involves a lot of template instantiations, either O(n) or O(logn) depending on the implementation, so it's a very heavy solution. On the other hand a type_sequence is only a single instantiation
@ultradude5410
@ultradude5410 Жыл бұрын
@@DreyriRS Plus, you can construct a type_sequence, and pass it around as a value. Comes in handy if a pack *has* to be deduced.
@skyeplus
@skyeplus Жыл бұрын
Tuple is too heavy. A lot of people, unfortunately use uninstantiated tuples to manipulate sequences of types. Yeah, there is also a necessity for type_container for a single type.
@thelatestartosrs
@thelatestartosrs 6 ай бұрын
type_sequence has no storage it is a compile time construct, tuple is meant to be instanciated as an object eventually, it wouldnt respect the SRP to use tuple instead of type_sequence for this task.
@q_rsqrt5140
@q_rsqrt5140 Жыл бұрын
Can someone elaborate on this kinds idea?
@akshay.in.ception
@akshay.in.ception Жыл бұрын
around 17:30 who was the person who answered, where Andrei said he knows it because he wrote his own compiler?
@per.nordlow
@per.nordlow Жыл бұрын
Walter Bright?
@TheOnlyAndreySotnikov
@TheOnlyAndreySotnikov Жыл бұрын
Slide 19 is incorrect. It won't even compile. It should be "int f(T, const Ts&..., U);"
@userrand4900
@userrand4900 2 жыл бұрын
There is a typo in slide 19, "U" should be the last function argument, i.e. it should be int f(T, const Ts&..., U)
@daniilgavrilikhin34
@daniilgavrilikhin34 Жыл бұрын
It's not a typo. U can't be deduced, if its the parameter after pack. Actually, compiler will give you an error for this declaration
@userrand4900
@userrand4900 Жыл бұрын
The github link points to 2020, should point to 2021 instead to get the slides
@johndubchak
@johndubchak 3 ай бұрын
Inverse Factory Method = Converse Factory Method...just a wording substitution that seems more clear and intuitive.
@mark_fi
@mark_fi 11 ай бұрын
Damn. Wish I could just write everything in Rust. :D
@themeeman
@themeeman 2 жыл бұрын
Decent talk, took quite a while to get to the actual point though
@tatoute1
@tatoute1 2 жыл бұрын
After 30+y of C++ practice, I'm forced to says that C++ is going to crumble under it's own weight.
@zxuiji
@zxuiji Жыл бұрын
What a convoluted mess, just stick to C where everything is already properly defined, changes to expected behaviour are extremely rare, if you don't understand C then you don't know how to programme to begin with, you only understand the wrappers
¡Puaj! No comas piruleta sucia, usa un gadget 😱 #herramienta
00:30
JOON Spanish
Рет қаралды 22 МЛН
Sigma Girl Education #sigma #viral #comedy
00:16
CRAZY GREAPA
Рет қаралды 71 МЛН
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 26 МЛН
Маленькая и средняя фанта
00:56
Multi DO Smile Russian
Рет қаралды 5 МЛН
CppCon 2018: Andrei Alexandrescu “Expect the expected”
58:58
CppCon 2015: Andrei Alexandrescu “std::allocator...”
1:12:27
Faster, Easier, Simpler Vectors - David Stone - CppCon 2021
1:00:56
Why Is Lua So Popular?
8:03
Awesome
Рет қаралды 82 М.
Fastware - Andrei Alexandrescu
1:00:00
NDC Conferences
Рет қаралды 21 М.
cool watercooled mobile phone radiator #tech #cooler #ytfeed
0:14
Stark Edition
Рет қаралды 6 МЛН
⌨️ Сколько всего у меня клавиатур? #обзор
0:41
Гранатка — про VR и девайсы
Рет қаралды 653 М.
Fiber kablo
0:15
Elektrik-Elektronik
Рет қаралды 8 МЛН
😱НОУТБУК СОСЕДКИ😱
0:30
OMG DEN
Рет қаралды 2,4 МЛН
3.5.A Solar Mobile 📱 Charger
0:39
Gaming zone
Рет қаралды 319 М.