From: Ville Voutilainen Date: Tue, 26 Mar 2019 15:00:05 +0000 (+0200) Subject: re PR libstdc++/89825 (Jump table for variant visitation could be shortened for never... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=da97b98ad341457aa46afc5c20d4ec95af5c1f91;p=gcc.git re PR libstdc++/89825 (Jump table for variant visitation could be shortened for never empty variants) PR libstdc++/89825 Fix based on a suggestion by Antony Polukhin. * include/std/variant (_Extra_visit_slot_needed): New. (_Multi_array): Use it. (_S_apply_all_alts): Likewise. From-SVN: r269947 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 62242607674..2e595b3eca8 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,13 @@ +2019-03-26 Ville Voutilainen + + PR libstdc++/89825 + Fix based on a suggestion by Antony Polukhin. + * include/std/variant (__never_valueless): New. + (_M_valid): Use it. + (_Extra_visit_slot_needed): New. + (_Multi_array): Use it. + (_S_apply_all_alts): Likewise. + 2019-03-26 Ville Voutilainen PR libstdc++/89824 diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 36314638e63..3932a8a0d14 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -323,6 +323,12 @@ namespace __variant _Variadic_union<_Rest...> _M_rest; }; + template + constexpr bool __never_valueless() + { + return (is_trivially_copyable_v<_Types> && ...); + } + // Defines index and the dtor, possibly trivial. template struct _Variant_storage; @@ -408,7 +414,7 @@ namespace __variant constexpr bool _M_valid() const noexcept { - if constexpr ((is_trivially_copyable_v<_Types> && ...)) + if constexpr (__never_valueless<_Types...>()) return true; return this->_M_index != __index_type(variant_npos); } @@ -747,6 +753,20 @@ namespace __variant void* __get_storage(_Variant&& __v) { return __v._M_storage(); } + template + struct _Extra_visit_slot_needed + { + template struct _Variant_never_valueless; + + template + struct _Variant_never_valueless> + : bool_constant<__never_valueless<_Types...>()> {}; + + static constexpr bool value = + is_same_v<_Maybe_variant_cookie, __variant_cookie> + && !_Variant_never_valueless<__remove_cvref_t<_Variant>>::value; + }; + // Used for storing multi-dimensional vtable. template struct _Multi_array @@ -764,8 +784,11 @@ namespace __variant size_t __first, size_t... __rest> struct _Multi_array<_Ret(*)(_Visitor, _Variants...), __first, __rest...> { + static constexpr size_t __index = + sizeof...(_Variants) - sizeof...(__rest) - 1; + using _Variant = typename _Nth_type<__index, _Variants...>::type; static constexpr int __do_cookie = - is_same_v<_Ret, __variant_cookie> ? 1 : 0; + _Extra_visit_slot_needed<_Ret, _Variant>::value ? 1 : 0; using _Tp = _Ret(*)(_Visitor, _Variants...); template constexpr const _Tp& @@ -832,7 +855,7 @@ namespace __variant _S_apply_all_alts(_Array_type& __vtable, std::index_sequence<__var_indices...>) { - if constexpr (is_same_v<_Result_type, __variant_cookie>) + if constexpr (_Extra_visit_slot_needed<_Result_type, _Next>::value) (_S_apply_single_alt( __vtable._M_arr[__var_indices + 1], &(__vtable._M_arr[0])), ...);