Better Code: Runtime Polymorphism - Sean Parent

  Рет қаралды 70,630

NDC Conferences

NDC Conferences

7 жыл бұрын

This talk explains why (and how) to implement polymorphism without inheritance in C++.
The talk contains many C++ tips and techniques, including many new features from C++11. During the course of that talk a key feature from Photoshop will be demonstrated and implemented.
NDC Conferences
ndc-london.com
ndcconferences.com

Пікірлер: 71
@gulagz1549
@gulagz1549 5 жыл бұрын
"My class inherits from nothing." This is my favorite dev video of all time.
@kristupasantanavicius9093
@kristupasantanavicius9093 4 жыл бұрын
A java programming would create a base class "Object" and would make sure that all of his other classes inherit from it 😂
@theevilcottonball
@theevilcottonball 17 күн бұрын
As a C programmer this is nothing special.
@minimatamou8369
@minimatamou8369 6 жыл бұрын
42:20 JUST DO IT ! Don't let your dreams be dreams ! Love this talk, Sean Parent is awesome as always.
@spacechild2
@spacechild2 4 ай бұрын
Great talk! The only thing I'd disagree with is that mutable polymorphic objects are an extreme exception. In my experience, they are rather common. For example, imagine an abstract base class for an audio plugin with a process() method. Each concrete plugin would override the process() method to implement their DSP algorithm. This method must be non-const because some plugins need to maintain and mutate state, e.g. a filter or delay line. The host application, however, doesn't care about the implementation details, it just wants to process audio buffers by passing them to the process() method of each plugin in a particular order.
@nidefawl2552
@nidefawl2552 2 жыл бұрын
I have been using this pattern for the past 5 years now. It uses so little boilerplate for what it achieves. I learned to apply similar techniques all over my code.
@ChristianBrugger
@ChristianBrugger Жыл бұрын
Amazing how similar the talk is to the one in 2011 and still it's more relevant that ever!
@johnappleseed8839
@johnappleseed8839 5 жыл бұрын
You must execute epic feats of complex and subtle language kung fu to beat C++ into semantic submission.
@mathiaskohler383
@mathiaskohler383 4 жыл бұрын
Excellent! I will watch this again. Thanks.
@tedchirvasiu
@tedchirvasiu 3 жыл бұрын
His voice is very calming
@kruel9669
@kruel9669 7 жыл бұрын
Thanks for the great talk! Just wanted to add that the description is a bit misleading: there is inheritance involved. It is just hidden from the library user and done automatically "on use" through a template.
@kwanarchive
@kwanarchive 7 жыл бұрын
Yes. It should be "without requiring inheritance from the library user".
@mywtfmp3
@mywtfmp3 6 жыл бұрын
I could watch this talk every day.
@gulagzulka2188
@gulagzulka2188 5 жыл бұрын
I know... I feel the same way. He presents complex topics in a way that doesn't freak you out. And it's not that he talks down to his audience. Not at all. He's just calm and assured and it rubs off.
@llothar68
@llothar68 5 жыл бұрын
@@gulagzulka2188 Well i did freak out the first time, but in a way of "i want to understand this C++ kung fu" not in a "go away you ugly orc" freak out.
@kormisha
@kormisha 3 жыл бұрын
This is probably the first talk which is not clear. May be on second go, it will become more clear. You loose the thread after a while and not really clear what is real benefit. Despite all this evil things about inheritance, it is a better option in 99% of cases than the final implementation in this talk.
@steveneckhoff8632
@steveneckhoff8632 5 жыл бұрын
This guy codes! Great talk!
@gulagzulka2188
@gulagzulka2188 5 жыл бұрын
@29:44 -- I'm running the same code with C++17 gcc 5.4.0 and I don't get the copy constructor call. I only get the two ctor calls. So that issue with the compiler deleting the default copy/move assignment for classes with non-static data members or direct base classes NOT defining a move assignment operator appears to be mitigated.
@Xeverous
@Xeverous 3 жыл бұрын
Great talk and a great pattern. Small code improvement: instead of assert(x.size()) use assert(!x.empty()) - it is more clear about the intent and does not require the container to know or compute its size (linked lists size() can be O(n) but empty() is O(1)).
@ChristianBrugger
@ChristianBrugger Жыл бұрын
Just a small addition, since c++11 the size operation of any standard container needs to be O(1), including list.
@perfectionbox
@perfectionbox 4 жыл бұрын
If you resize a photoshop document, does that reset the history? Wouldn't the sparse tile system get confused?
@Saschaborg
@Saschaborg 5 жыл бұрын
WOW!! I'd say this is a *very big* part of a concept to solve "modern problems with OOP" regarding multithreading performance - since polymorphism (as one of THE most valuable features of OOP!) comes with all different kinds of drawbacks, usually. :-) Thanks for this inspirational presentation!
@hardenedapple
@hardenedapple 6 жыл бұрын
Reminds me of common lisp's generic functions -- (defmethod draw (obj) ... ) -- nice talk!
@llothar68
@llothar68 5 жыл бұрын
It's exactly this. Both break down polymorphism (the virtual method function table in C++ implementations) from class to function level. But Lisp is hidding all this and with its non static typed nature has a lot of ways to make it easy.
@kronek9371
@kronek9371 2 жыл бұрын
I think I'm missing something obvious at 46:57, there is 2 objects in the document when he commit(), but there's 4 copies. Why is there 4 copies not 2?
@debashishdeka7698
@debashishdeka7698 3 жыл бұрын
Great Talk!
@User-cv4ee
@User-cv4ee Жыл бұрын
Didn't we lose the equality semantics of a regular type for the `int` wrapper class at 21:00? If I copy an object and check for equality it would return false cause the `unique_ptr`s would point to different objects.
@JiveDadson
@JiveDadson 6 жыл бұрын
Is there any place on the web where I can find the final code? There is no slide in this vid that shows the whole thing on one screen.
@767Steel
@767Steel 6 жыл бұрын
There's link at the of the presentation. sean-parent.stlab.cc/papers-and-presentations/#better-code-runtime-polymorphism
@Yupppi
@Yupppi 6 ай бұрын
At some point this started to remind of the old meme "yo dawg I heard you liked documents so I put your document inside your document"
@apivovarov2
@apivovarov2 5 ай бұрын
@25:52 It looks like a copy-and-swap idiom. Why we use move (and move assignment) here instead of using swap for selfs?
@monsterpda
@monsterpda 5 жыл бұрын
I wanna like this more than once
@tux1968
@tux1968 Жыл бұрын
@49:22 This is not a demonstration that the copy constructor isn't still being called many times. Since you deleted the explicit copy constructor instrumented to print "Copy", you didn't need to make any of the other changes to have the same output. There is no way to see if the implicitly compiler-generated copy constructor is still being called. Not sure how to make an actual, proper demonstration of success; when I add an instrumented copy back in, it is indeed still disappointingly being called many times.
@vladimiro3059
@vladimiro3059 Жыл бұрын
I rewatched several time this. Shame that no one else noticed that. "Whach my hands I remove the print {copy} and remove the copy/move ctors and you see there is no copy" )))) lol that's epic fail. What about default ones. Try to expicitly delete the ctors and code won't compile.
@perfectionbox
@perfectionbox Жыл бұрын
What happens to Photoshop's undo history if I use a filter globally on a huge document? Sparse tiling won't help.
@lapinrigolo
@lapinrigolo 7 жыл бұрын
Any textual description of the talk?
@vasiliigulevich9202
@vasiliigulevich9202 4 ай бұрын
Sure! Promote value semantics. Hide polymorphism. To do so, make polymorphic wrappers of values at the point of type erasure. The guy literally spend an hour saying two sentences.
@sabetaytoros4123
@sabetaytoros4123 6 жыл бұрын
Can someone explain me why do we need the abstract virtual base struct concept_t ? Concept struct is used by the Type Copy Assignment function. Since the compiler would write the copy assignment function for every type that we use do we need another indirection.
@oliviercorrio4566
@oliviercorrio4566 5 жыл бұрын
I think it is because all type in the vector must be of the same type. A vector of shared_ptr is not the same as shared_ptr even if it is a pointer inside. So with that all elements in vector are of type concept_t.
@alexsmart2612
@alexsmart2612 3 жыл бұрын
@@oliviercorrio4566 You also need it to be virtual for having polymorphic behavior of draw and the destructor.
@robbie_
@robbie_ 6 жыл бұрын
Trying to understand this. How is it different from the pimpl idiom? ...
@gulagzulka2188
@gulagzulka2188 5 жыл бұрын
Basically it isn't. He's using containment instead of inheritance to achieve polymorphism, and Pimpl is based on containment too. This just takes it a little further.
@puyadaravi3109
@puyadaravi3109 4 жыл бұрын
21:45
@WarHero56
@WarHero56 2 жыл бұрын
at around 7:20, if emplace_back fails to allocate memory, doesn't that mean we're out of RAM and we have bigger problems than leaking memory because the program is about to crash anyways.
@SamWhitlock
@SamWhitlock 2 жыл бұрын
Here are the slides: sean-parent.stlab.cc/presentations/2017-01-18-runtime-polymorphism/2017-01-18-runtime-polymorphism.pdf
@RishabhRD
@RishabhRD 2 жыл бұрын
He is a wizard
@CR3271
@CR3271 2 жыл бұрын
38:08 he says there are potential performance gains because you don't have to wrap strings or ints in a class. Yet that's exactly what he's doing under the hood. Seems to me if I wrote a wrapper class for int or string with all his little copy/move optimizations, it would be the same code for all intents and purposes. Maybe more convenient for a library consumer to code against, but no different performance-wise. What am I missing? I'd love to see some benchmarks on the two coding styles.
@dzarek1039
@dzarek1039 2 жыл бұрын
Yes and no. There is a wrapper, but it is in a class that actually needs polymorphism. So, you only have the overhead where it is actually needed, in a polymorphic scenario.You can still use int and std::string directly (without wrapper) and still use non-virtual draw() on them in non-polymorphic scenarios.
@ashrasmun1
@ashrasmun1 5 жыл бұрын
I'm sorry, but I'm not seeing it. You say at 38:00 that you don't need to wrap your ints or strings into objects, but don't you wrap them anyway (model)? You just hide it from the client.
@JanuarAndaria
@JanuarAndaria 5 жыл бұрын
he talking about the Deep Problem #3, 13:59.
@H2CO3Szifon
@H2CO3Szifon 5 жыл бұрын
52:18 - neither Go or Swift goes anywhere near or as far as Rust when it comes to polymorphic value types.
@gongfei
@gongfei 6 жыл бұрын
5 people audience?
@WalderFrey
@WalderFrey 6 жыл бұрын
Apparently, for one the most interesting talkers you could 'give up' an hour listening to.
@llothar68
@llothar68 5 жыл бұрын
25000 viewers. Nobody who get recorded at that events (with lots of overlapping sessions - i guess) is talking to the physical audience.
@pmcgee003
@pmcgee003 2 жыл бұрын
@@llothar68 now 55k 🙂
@kormisha
@kormisha 3 жыл бұрын
Explanation of incidental data structure did not make any sense to me.
@ruixue6955
@ruixue6955 5 жыл бұрын
2:36 what is inheritance
@usx95
@usx95 5 жыл бұрын
lol
@GeatMasta
@GeatMasta 3 жыл бұрын
Why not delete the copy constructor and define the move constructor? Isn’t it kind of dishonest to say that this is a copy when its a move.
@noxmetus
@noxmetus 5 жыл бұрын
At 38:06. It's not true. You do have to wrap integers and strings into objects. This is what object_t is. Btw, it's the same technique std::shared_ptr uses to store custom deleters. The basic idea of the rest of the code is how to use function overloading for polymorphism.
@Fleshbits1
@Fleshbits1 3 ай бұрын
Quantify "indirection, heap allocation, and virtualization impact my performance"... by how much? 1 nanosecond? Is it more than adding two integers together? Do we analyze the performance of addition and subtraction? I don't understand why everyone is so dogmatic about these things unless they actually have constraints where they are doing something that has to run in 100ns or on 16k of memory. .. and sure those situations exist, but I am getting really tired of talking about a few nanoseconds of performance while we are waiting a full second for a user to click a button. How many cpu cycles does it really take to allocate an object? and don't you only allocate it once? How many cpu cycles does indirection take up? one? five?
@TechnologyRules
@TechnologyRules 4 жыл бұрын
Yeah, Photoshop is just so slow that I can't even see how can someone use it as an example of optimized software.
@none_of_your_business
@none_of_your_business 9 ай бұрын
write a better alternative with even half the functionality ? ;D
@TechnologyRules
@TechnologyRules 9 ай бұрын
@@none_of_your_business Not sure if you actually read it, but the point is not functionality in what I said. I was talking about *performance*. Why are you licking his b4lls? Shut your mouth.
@namse-back-account
@namse-back-account 2 жыл бұрын
6:11 "how many people see the bug?" ... Me: WHAT!? BUG?!?!?!?!!?!?
@llothar68
@llothar68 5 жыл бұрын
C++ could be so much easier if the language would implement free function overload and polymorphism on more then just the first argument (the implicit this argument) like CLOS (the lisp object system). This is exactly what this concept classes do by foot. But hey maybe we get this in C++44. Glaciers move faster then this language comittee.
@greenfloatingtoad
@greenfloatingtoad 5 жыл бұрын
Andrei Alexandrescu goes over different techniques to implement this with templates in chapter 11 of Modern C++ Design, if you're willing to put up with template magic to use multimethods before 2044!
@juaxix
@juaxix 5 жыл бұрын
This class document looks like xml ...but it would be better if it was protobuf :D
Better Code: Concurrency - Sean Parent
1:07:36
NDC Conferences
Рет қаралды 26 М.
Pacific++ 2018: Sean Parent "Generic Programming"
1:19:57
Pacific++
Рет қаралды 18 М.
Bro be careful where you drop the ball  #learnfromkhaby  #comedy
00:19
Khaby. Lame
Рет қаралды 48 МЛН
UFC 302 : Махачев VS Порье
02:54
Setanta Sports UFC
Рет қаралды 1,2 МЛН
FOOTBALL WITH PLAY BUTTONS ▶️ #roadto100m
00:29
Celine Dept
Рет қаралды 75 МЛН
КАКОЙ ВАШ ЛЮБИМЫЙ ЦВЕТ?😍 #game #shorts
00:17
GoingNative 2013 Inheritance Is The Base Class of Evil
24:20
C++ Code Smells - Jason Turner
56:11
NDC Conferences
Рет қаралды 75 М.
C++ Polymorphism and Virtual Member Functions [6]
12:13
Professor Hank Stalica
Рет қаралды 9 М.
code::dive conference 2014 - Scott Meyers: Cpu Caches and Why You Care
1:16:58
NOKIA Technology Center Wrocław
Рет қаралды 183 М.
Warning: std::find() is Broken! - Sean Parent - CppCon 2021
1:41:35
Where Have All the Cycles Gone? by Sean Parent
59:20
Performance Summit
Рет қаралды 2,3 М.
Power up all cell phones.
0:17
JL FUNNY SHORTS
Рет қаралды 50 МЛН
Дени против умной колонки😁
0:40
Deni & Mani
Рет қаралды 9 МЛН
Apple, как вас уделал Тюменский бренд CaseGuru? Конец удивил #caseguru #кейсгуру #наушники
0:54
CaseGuru / Наушники / Пылесосы / Смарт-часы /
Рет қаралды 4,6 МЛН