C ++ - raise the question

Does anyone know if boost::get for boost::variant operator with high demand or not.

At the moment, I am refactoring old code in a performance-critical part where the “variant” is implemented by containers for each of the possible types and corresponding enum .

Obviously, this is fast, but ugly, and now, when I have to reorganize the code to work with another type , I want to get rid of this old part of the code and replace it with boost::variant .

In addition, I can’t just “profile both options and compare,” because this refactoring is a pain in the ass and will be quite laborious.

So, if someone knows how boost::get<x> performs a comparison with typical dispatching like enum-based , I would appreciate it if you share this knowledge.

There is another use case for boost::variant<types> with a user visitor (as described in the documentation of boost::variant ) - could it be faster than boost::get in my case?

Thanks.

+7
c ++ performance boost variant
source share
2 answers

You can still write a simple test application to compare the two, this should not be a production environment.

My colleague recently had a similar problem. There are objects of different types in his script, but he always knew in advance what type he expected. Also its data structure was huge, so memory was a problem. He solved the problem using void * and reinterpret_cast . This prevents a lack of memory for polymorphism and is very fast. You must be absolutely sure of what you are doing, although otherwise everything will explode.

+4
source share

Looking at the code, get<> is implemented using the boost::variant internal viewer. In turn, the visitor mechanism relies on the mpl sequence, and it is performed step by step. This means no more than n steps according to a variant of the nth type, but there is a loop (recursive call). Again, as Space Cowboy suggests, a small performance test will be helpful.

+3
source share

All Articles