Тёмный

Better Code: Runtime Polymorphism - Sean Parent 

NDC Conferences
Подписаться 194 тыс.
Просмотров 71 тыс.
50% 1

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

Наука

Опубликовано:

 

26 фев 2017

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 72   
@gulagz1549
@gulagz1549 5 лет назад
"My class inherits from nothing." This is my favorite dev video of all time.
@kristupasantanavicius9093
@kristupasantanavicius9093 5 лет назад
A java programming would create a base class "Object" and would make sure that all of his other classes inherit from it 😂
@theevilcottonball
@theevilcottonball Месяц назад
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.
@johnappleseed8839
@johnappleseed8839 6 лет назад
You must execute epic feats of complex and subtle language kung fu to beat C++ into semantic submission.
@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.
@spacechild2
@spacechild2 5 месяцев назад
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.
@tedchirvasiu
@tedchirvasiu 3 года назад
His voice is very calming
@ChristianBrugger
@ChristianBrugger Год назад
Amazing how similar the talk is to the one in 2011 and still it's more relevant that ever!
@Yupppi
@Yupppi 7 месяцев назад
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"
@mywtfmp3
@mywtfmp3 6 лет назад
I could watch this talk every day.
@gulagzulka2188
@gulagzulka2188 6 лет назад
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 4 года назад
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.
@mathiaskohler383
@mathiaskohler383 4 года назад
Excellent! I will watch this again. Thanks.
@steveneckhoff8632
@steveneckhoff8632 5 лет назад
This guy codes! Great talk!
@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.
@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".
@debashishdeka7698
@debashishdeka7698 4 года назад
Great Talk!
@gulagzulka2188
@gulagzulka2188 6 лет назад
@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.
@hardenedapple
@hardenedapple 7 лет назад
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.
@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.
@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!
@monsterpda
@monsterpda 6 лет назад
I wanna like this more than once
@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.
@perfectionbox
@perfectionbox 4 года назад
If you resize a photoshop document, does that reset the history? Wouldn't the sparse tile system get confused?
@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?
@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
@RishabhRD
@RishabhRD 3 года назад
He is a wizard
@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.
@apivovarov2
@apivovarov2 7 месяцев назад
@25:52 It looks like a copy-and-swap idiom. Why we use move (and move assignment) here instead of using swap for selfs?
@lapinrigolo
@lapinrigolo 7 лет назад
Any textual description of the talk?
@vasiliigulevich9202
@vasiliigulevich9202 5 месяцев назад
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.
@perfectionbox
@perfectionbox Год назад
What happens to Photoshop's undo history if I use a filter globally on a huge document? Sparse tiling won't help.
@SamWhitlock
@SamWhitlock 2 года назад
Here are the slides: sean-parent.stlab.cc/presentations/2017-01-18-runtime-polymorphism/2017-01-18-runtime-polymorphism.pdf
@sunipmukherjee2130
@sunipmukherjee2130 Месяц назад
Basically Rust traits but more boilerplate. Gotchu.
@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 6 лет назад
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 6 лет назад
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
@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.
@kormisha
@kormisha 4 года назад
Explanation of incidental data structure did not make any sense to me.
@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.
@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 🙂
@H2CO3Szifon
@H2CO3Szifon 6 лет назад
52:18 - neither Go or Swift goes anywhere near or as far as Rust when it comes to polymorphic value types.
@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.
@Fleshbits1
@Fleshbits1 5 месяцев назад
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?
@ruixue6955
@ruixue6955 6 лет назад
2:36 what is inheritance
@usx95
@usx95 5 лет назад
lol
@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.
@남세2
@남세2 2 года назад
6:11 "how many people see the bug?" ... Me: WHAT!? BUG?!?!?!?!!?!?
@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 10 месяцев назад
write a better alternative with even half the functionality ? ;D
@TechnologyRules
@TechnologyRules 10 месяцев назад
@@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.
@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
Просмотров 27 тыс.
Two Ways To Do Dynamic Dispatch
19:54
Просмотров 72 тыс.
Лайфхак для дачников
00:13
Просмотров 17 тыс.
Pacific++ 2018: Sean Parent "Generic Programming"
1:19:57
Cursed C++ Casts
17:41
Просмотров 70 тыс.
The Flaws of Inheritance
10:01
Просмотров 911 тыс.
Where Have All the Cycles Gone? by Sean Parent
59:20
Просмотров 2,4 тыс.
AlexFest: Sean Parent - Meeting Alex
15:12
Просмотров 2 тыс.
How Senior Programmers ACTUALLY Write Code
13:37
Просмотров 1,4 млн
Choose a phone for your mom
0:20
Просмотров 7 млн