r/cpp • • Jan 08 '21

With std::variant, you choose either performance or sanity

https://www.youtube.com/watch?v=GRuX31P4Ric mentioned std::variant being an outstanding type. Previously, I had been using untagged unions for the same purpose, manually setting a tag field.

My conclusion is that std::variant has little benefit if performance is important. "Performance", in my case, means my main real-world benchmark takes 70% longer to complete (audio). So std::variant's overhead is approximately as expensive as everything else in my program together.

The reason is that you cannot do dynamic dispatching in a simultaneously reasonable and performant way. Untagged unions suck, but std::variant doesn't solve the problems with untagged unions it wants to solve. Here's how dynamic dispatching is done:

if (auto* p = std::get_if<0>(&a))
    return p->run();
else if (auto* p = std::get_if<1>(&a))
    return p->run();
else if (auto* p = std::get_if<2>(&a))
...

You copy and paste, incrementing the number each branch. Any time you add or remove a type from your variant, you must also adjust the number of else if(), and this must be done for each dispatch type. This is the very stupid stuff I've been doing with untagged unions. If we try to use std::variant's tools to avoid this, we get https://stackoverflow.com/questions/57726401/stdvariant-vs-inheritance-vs-other-ways-performance

At the bottom of that post, you'll see that std::get_if() and std::holds_alternative() are the only options that work well. std::visit is especially bad. This mirrors my experience. But what if we use templates to manually generate the if-else chain? Can we avoid copy-paste programming?

template <int I>
struct holder {
	static float r(variant_type& f, int argument) {
		if (auto pval = std::get_if<I - 1>(&f))
			return pval->run(argument);
		else
			return holder<I - 1>::r(f, argument);
	}
};
template <>
struct holder<0> {
	static float r(variant_type& f, int argument) {
		__builtin_unreachable();
		return 0;
	}
};

holder<std::variant_size_v<variant_type>>::r(my_variant, argument);

That looks ugly, but at least we only have to write it once per dispatch type. We expect the compiler will spot "argument" being passed through and optimize the copies away. Our code will be much less brittle to changes and we'll still get great performance.

Result: Nope, that was wishful thinking. This template also increases the benchmark time by 70%.

mpark::variant claims to have a better std::visit, what about that?

  1. It's annoying converting std::variant to mpark::variant. You must manually find-and-replace all functions related to std::variant. For example, if get() touches a variant, you change it to mpark::get(), but otherwise you leave it as std::get(). There's no way to dump the mpark functions into the std namespace even if ignoring standards compliance, because when some random header includes <variant>, you get a collision. You can't redefine individual functions from std::get_if() to mpark::get_if(), because function templates can't be aliased.
  2. Base performance: mpark::variant is 1-3% slower than std::variant when using if-else, and always slightly loses. I don't know why. But 3% slower is tolerable.
  3. mpark::visit is still 60% slower than a copy-pasted if-else chain. So it solves nothing.

Overall, I don't see a sane way to use std::variant. Either you write incredibly brittle code by copy-pasting everywhere, or you accept a gigantic performance penalty.

Compiler used: gcc 10.2, mingw-w64

152 Upvotes

121 comments sorted by

View all comments

Show parent comment

2

u/zfgOof Jan 08 '21

I'm aware of the issues and think it's acceptable that variant2 doesn't catch it, which is why I didn't complain too hard in the beginning. The semantics around being exception-free are a general C++ problem, not a variant2 problem. A policy-based variant2 would be more suitable for my case, since I know that I also avoid all those issues. The original boost::variant was cognizant of this problem and mentioned it, but did not get around to finishing it.

10

u/MrPotatoFingers Jan 08 '21

There is nothing for boost.variant2 to catch. Even if you had another way to get it to avoid double storage besides the obvious noexcept qualifications, you're likely still throwing away performance elsewhere - think a vector where you do a push- or emplace_back that now takes the slow path to be exception-safe.

If you want exception-free programming, just mark your functions noexcept.

4

u/zfgOof Jan 08 '21

I just assumed you were right about failure to catch it being my responsibility, since I know that catching noexcept is hard and I didn't bother being careful about it.

However, it turns out boost.variant2 just doesn't catch it. My move and copy constructors are all deleted or noexcept. My constructors are all noexcept. There is nothing wrong with my types.

If I feed variant2 with a struct with a deleted move constructor, it doubles the storage, because its check is mp11::mp_all<std::is_nothrow_move_constructible<T>...>::value and it ignores when the move and copy constructors doesn't exist. So it's very easy to catch in this case, and it's variant2's fault.

4

u/dodheim Jan 08 '21

What do you propose it check instead of std::is_nothrow_move_constructible?

2

u/zfgOof Jan 08 '21

If my move constructor is deleted, it can't be called, so it doesn't matter that it's not noexcept. std::is_move_constructible captures this information.

1

u/dodheim Jan 08 '21

std::is_nothrow_move_constructible is mandated to be the same as std::is_move_constructible except to add the additional requirement that the constructor be noexcept. So, if std::is_move_constructible captures the information then std::is_nothrow_move_constructible necessarily does too; conversely, if it's not working with std::is_nothrow_move_constructible then it won't (/shouldn't) with std::is_move_constructible either.

When you define your move constructor as deleted, are you not marking that definition as noexcept? If not, why not? This is exactly what u/cdglove was referring to.

1

u/zfgOof Jan 08 '21

I don't understand, because this looks very simple from my side. If none of my types have move or copy constructors, and the variant tries to move, what do you think should happen? It should produce a compiler error or throw an exception?

1

u/dodheim Jan 08 '21 edited Jan 08 '21

What are you doing to inhibit the implicitly-defined ones? It was implied (I thought) that you were deleting them explicitly; if not, what are you doing to prevent them?

EDIT: Nevermind – either way, it's just falling back to the copy constructor. Does explicitly making the copy constructor noexcept help, even if it's still just defaulted?

2

u/zfgOof Jan 08 '21 edited Jan 08 '21

I am explicitly deleting the move and copy constructors for my types. I expect that if I try to move a variant of these types, it should simply fail to compile. Therefore, there is no issue if the move and copy constructors fail to exist.

EDIT: in response to your edit, the copy constructor has been deleted too. No copying allowed at all.

1

u/dodheim Jan 08 '21

I see, I misunderstood; you're proposing a check for is_nothrow_move_constructible || !is_move_constructible? I think I agree.

→ More replies

4

u/pdimov2 Jan 09 '21

Please open an issue.