Feature/optimize fixed_variant index size (#29)

* optimized sizeof(fixed_variant::index_)

* refactored to work around msvc-14.0 SFINAE bug

* added narrow cast

* updated variant perf plots
This commit is contained in:
joaquintides
2025-01-01 23:27:39 +01:00
committed by GitHub
parent adc8658c3f
commit c21f0b28ee
7 changed files with 14 additions and 3 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 33 KiB

@@ -1,4 +1,4 @@
/* Copyright 2024 Joaquin M Lopez Munoz.
/* Copyright 2024-2025 Joaquin M Lopez Munoz.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
@@ -20,10 +20,12 @@
#include <boost/mp11/list.hpp>
#include <boost/mp11/set.hpp>
#include <boost/mp11/tuple.hpp>
#include <boost/mp11/utility.hpp>
#include <boost/poly_collection/detail/is_equality_comparable.hpp>
#include <boost/poly_collection/detail/is_nothrow_eq_comparable.hpp>
#include <boost/type_traits/is_constructible.hpp>
#include <cstddef>
#include <limits>
#include <memory>
#include <stdexcept>
#include <type_traits>
@@ -50,6 +52,15 @@ class fixed_variant
mp11::mp_is_set<fixed_variant>::value,
"all types in the variant must be distinct");
static constexpr std::size_t N=sizeof...(Ts);
using index_type=mp11::mp_cond<
mp11::mp_bool<
(N<=(std::numeric_limits<unsigned char>::max)())>,unsigned char,
mp11::mp_bool<
(N<=(std::numeric_limits<unsigned short>::max)())>,unsigned short,
mp11::mp_true, std::size_t
>;
public:
template<
typename T,
@@ -57,13 +68,13 @@ public:
typename std::enable_if<
(I<mp11::mp_size<fixed_variant>::value)>::type* =nullptr
>
explicit fixed_variant(const T&):index_{I}{}
explicit fixed_variant(const T&):index_{static_cast<index_type>(I)}{}
std::size_t index()const noexcept{return index_;}
bool valueless_by_exception()const noexcept{return false;}
private:
std::size_t index_;
index_type index_;
};
template<typename T>