Boost logo

Boost :

Subject: [boost] [variant2] Review (comparison with other variants)
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2019-04-14 12:22:12


Hi,

> - What is your evaluation of the design?

Variant2 is very close to the boost::variant and std::variant. The
major difference is in treating very rare border case.

Let me put it very clear:
* I really like the valueless_by_exception() approach of the
std::variant. It gives perfectly determinated result in case of an
exception during variant assignments, does not dynamically allocate
storage and does not double the size of the variant variable.

* The approach of existing boost::variant is not perfect. Exception
during assignment could lead to the default construction of some type
that was not previously held in the variant. This is actually a very
creepy behavior:

variant<foo, foo2, locked_mutex> v;
v = foo2{}; // may suddenly lock a mutex via locked_mutex{}

But users do not complain about this. Exceptions during assignments
are so rare, that they just don't get hit by the issue.

You have to try really really hard to get into the
valueless_by_exception() state.

The other drawback of boost::variant is that it dynamically allocates
memory in some cases that are also quite rare.

* Variant2 fixes the second drawback of boost::variant (dynamic
allocations). But the first one is still there - in exceptional cases
it may default construct something unexpected.

Variant2 also may get performance issues because of the doubled size.
Smaller data structures better fit the CPU caches. Some platforms
allow returning small variables in registers (not on the stack). On
some platforms addressing the first ~127 bytes of the structure is
more efficient than addressing the other bytes.

Increasing the size of a variant2 for dealing with cases that mostly
never happen seems unreasonable to me.

I'm not saying that the current boost::variant is perfect in border
cases (it is not!). I'm saying that std::variant has no such drawbacks
at all. Variant2 keeps some issues of the boost::variant and adds new
ones.

> - What is your evaluation of the implementation?

Looks great. Enjoyed it.

> - What is your evaluation of the documentation?

Did not read very carefully.

> - What is your evaluation of the potential usefulness of the library?

Useful only if it will get as good as, or better than std::variant.

> - Did you try to use the library? With which compiler(s)? Did you
> have any problems?

Have not used it.

> - How much effort did you put into your evaluation? A glance? A quick
> reading? In-depth study?

Quick reading.

> - Are you knowledgeable about the problem domain?

Yes. I maintain the boost::variant and keep an eye on std::variant
implementations.

Conclusion: I would recommend to REJECT the Variant2 library in it's
current state. My opinion would change if the underlying design
changes in favor of explicit valueless_by_exception() state.

-- 
Best regards,
Antony Polukhin

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk