CppCon 2018: Mateusz Pusz “Effective replacement of dynamic polymorphism with std::variant”

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


This short talk presents how easy it is to replace some cases of dynamic polymorphism with std::variant. During the lecture, we will analyze and compare 2 implementations of the same simple Finite State Machine. It turns up that variant-based code is not only much faster but also it gives us the opportunity to define our interfaces and program flow much better. The talk will end up with the discussion of pros and cons of each approach and will try to give guidelines on when to use them.

Mateusz Pusz, Epam Systems
Chief Software Engineer

Software architect, chief developer, and security champion with more than 14 years of experience in designing, writing and maintaining C++ code for fun and living. C++ consultant, trainer, and evangelist focused on Modern C++. His main areas of interest and expertise are code performance, low latency, stability, and security.

Mateusz worked at Intel for 13 years and now he leads C++ Community at EPAM Systems. He is also a founder of Train IT that provides C++ trainings to corporations. Mateusz is an active voting member of the ISO C++ Committee (WG21) where, together with the best C++ experts in the world, he shapes the next official version of the C++ language. He is also a member of WG21 Study Group 14 (SG14) responsible for driving performance and low latency subjects in the Committee. In 2013 Mateusz won “Bench Games 2013” – worldwide competition in the C++ language knowledge.

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

This is an interesting approach of FSM, regardless if the performance testing result was fair, this talk shows several important techniqued we may use:
1. dynamic_cast with polymophism
2. double(multiple) dispatch implemented with visitor pattern
3. double dispatch implemented with CTTP, say, static polymophism
4. new technique of std::variant wothout polymophism

Any way, it's a good talk, thanks!

gavinchou
Автор

An very interesting technique. Thank you very much for the talk! I usually implement FSM so states and events do not contain data members and I can create them only once or make them static. That way dynamic memory allocation is not an issue. But I like the variant + visitor technique a lot, especially performance. Will try to use it when I have a chance. Awesome!

ruslany
Автор

At 9:23, why can't you use "this" as a pointer of the base event type in the 'dispatch' function so you don't have to overload in the state class?

shuojinbecool
Автор

I'm currently writing a program that will use a similar technique using variant. I hope it brings those benefits

OperationDarkside
Автор

It would have been nice to compare code size as well.

YourCRTube
Автор

What about iteration using range for loop. Is that possible using something like this? I like this idea as I have been considering something similar by creating a class called Polymorph and anything of that type can be derived from and used like virtuals but the idea of for(auto& T: vector<t>) I am not sure will carry over using something like this. The syntax of this looks terrible and it needs wrapped up if it is going to be widely used for public consumption.

seditt
Автор

hmmm, union is problem. That means memory is reserved for the LARGEST type. So you cannot use it when you have limited memory and you require it all.

jhbonarius