From: Vsevolod Stakhov Date: Wed, 9 Jun 2021 10:44:09 +0000 (+0100) Subject: [Minor] Reduce size of the css value X-Git-Tag: 3.0~331 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11cca7a60c8a9bc87439a2027619ae90561357c5;p=thirdparty%2Frspamd.git [Minor] Reduce size of the css value --- diff --git a/src/libserver/css/css_rule.cxx b/src/libserver/css/css_rule.cxx index 417770a08c..238998009f 100644 --- a/src/libserver/css/css_rule.cxx +++ b/src/libserver/css/css_rule.cxx @@ -28,15 +28,15 @@ void css_rule::override_values(const css_rule &other) { int bits = 0; /* Ensure that our bitset is large enough */ - static_assert(static_cast(css_value::css_value_type::CSS_VALUE_NYI) << 1 < + static_assert(1 << std::variant_size_v < std::numeric_limits::max()); for (const auto &v : values) { - bits |= static_cast(v.type); + bits |= static_cast(1 << v.value.index()); } for (const auto &ov : other.values) { - if (isset(&bits, static_cast(ov.type))) { + if (isset(&bits, static_cast(1 << ov.value.index()))) { /* We need to override the existing value */ /* * The algorithm is not very efficient, @@ -46,7 +46,7 @@ void css_rule::override_values(const css_rule &other) * is probably ok here */ for (auto &v : values) { - if (v.type == ov.type) { + if (v.value.index() == ov.value.index()) { v = ov; } } @@ -56,25 +56,22 @@ void css_rule::override_values(const css_rule &other) /* Copy only not set values */ std::copy_if(other.values.begin(), other.values.end(), std::back_inserter(values), [&bits](const auto &elt) -> bool { - return (bits & static_cast(elt.type)) == 0; + return (bits & (1 << static_cast(elt.value.index()))) == 0; }); } void css_rule::merge_values(const css_rule &other) { unsigned int bits = 0; - /* Ensure that our bitset is large enough */ - static_assert(static_cast(css_value::css_value_type::CSS_VALUE_NYI) << 1 < - std::numeric_limits::max()); for (const auto &v : values) { - bits |= static_cast(v.type); + bits |= 1 << v.value.index(); } /* Copy only not set values */ std::copy_if(other.values.begin(), other.values.end(), std::back_inserter(values), [&bits](const auto &elt) -> bool { - return (bits & static_cast(elt.type)) == 0; + return (bits & (1 << elt.value.index())) == 0; }); } diff --git a/src/libserver/css/css_value.hxx b/src/libserver/css/css_value.hxx index 9e192acb6f..82f65e3e9c 100644 --- a/src/libserver/css/css_value.hxx +++ b/src/libserver/css/css_value.hxx @@ -69,66 +69,40 @@ enum class css_display_value { * for simplicity */ struct css_value { - /* Bitset of known types */ - enum class css_value_type { - CSS_VALUE_COLOR = 1 << 0, - CSS_VALUE_NUMBER = 1 << 1, - CSS_VALUE_DISPLAY = 1 << 2, - CSS_VALUE_DIMENSION = 1 << 3, - CSS_VALUE_NYI = 1 << 4, - }; - - css_value_type type; std::variant value; - css_value() : type(css_value_type::CSS_VALUE_NYI) {} + css_value() {} css_value(const css_color &color) : - type(css_value_type::CSS_VALUE_COLOR), value(color) {} + value(color) {} css_value(double num) : - type(css_value_type::CSS_VALUE_NUMBER), value(num) {} + value(num) {} css_value(css_dimension dim) : - type(css_value_type::CSS_VALUE_DIMENSION), value(dim) {} + value(dim) {} css_value(css_display_value d) : - type(css_value_type::CSS_VALUE_DISPLAY), value(d) {} + value(d) {} auto to_color(void) const -> std::optional { - if (type == css_value_type::CSS_VALUE_COLOR) { - return std::get(value); - } - - return std::nullopt; + return extract_value_maybe(); } auto to_number(void) const -> std::optional { - if (type == css_value_type::CSS_VALUE_NUMBER) { - return std::get(value); - } - - return std::nullopt; + return extract_value_maybe(); } auto to_dimension(void) const -> std::optional { - if (type == css_value_type::CSS_VALUE_DIMENSION) { - return std::get(value); - } - - return std::nullopt; + return extract_value_maybe(); } auto to_display(void) const -> std::optional { - if (type == css_value_type::CSS_VALUE_DISPLAY) { - return std::get(value); - } - - return std::nullopt; + return extract_value_maybe(); } auto is_valid(void) const -> bool { - return (type != css_value_type::CSS_VALUE_NYI); + return !(std::holds_alternative(value)); } auto debug_str() const -> std::string; @@ -143,6 +117,15 @@ struct css_value { -> std::optional; static auto maybe_display_from_string(const std::string_view &input) -> std::optional; +private: + template + auto extract_value_maybe(void) const -> std::optional { + if (std::holds_alternative(value)) { + return std::get(value); + } + + return std::nullopt; + } }; }