繁体   English   中英

优化 ODR 使用的空类

[英]optimization for ODR-used empty classes

当今的许多 C++ 代码都倾向于最大程度地加载模板。 它们是库:STL、Boost.Spirit、Boost.MPL 等。 他们鼓励用户以struct S { /* presence of non-virtual member functions and operators, but absense of non-static data members or non-empty base classes */ }; S const s{}; struct S { /* presence of non-virtual member functions and operators, but absense of non-static data members or non-empty base classes */ }; S const s{}; . 它们中的大多数是无状态的(即static_assert(std::is_empty< S >{});持有)。 对于其中使用 ODR 的那些,无论它们的文件增长 1 字节的空data部分如何sizeof(S) == 1对于空类型S sizeof(S) == 1因为随后分配的对象的所有地址都应该不同)。 即使在 Boost.Spirit 的简单语法中,也有很多这样的 ODR 使用的空类。 但是为他们保留空间是绝对没有意义的。

我尝试使用以下代码( -Ofast )在coliru上测试clang

#include <utility>
#include <type_traits>

#include <cstdlib>
#include <cassert>

template< std::size_t index >
struct S {};

namespace
{

template< std::size_t index >
S< index > value = {};

}

template< typename lhs, typename rhs >
std::ptrdiff_t
diff(lhs & l, rhs & r)
{
    return (static_cast< char * >(static_cast< void * >(&r)) - static_cast< char * >(static_cast< void * >(&l)));
}

template< std::size_t base, std::size_t ...indices >
std::ptrdiff_t
bss_check(std::index_sequence< indices... >)
{
    return (diff(value< (base + indices) >, value< (base + indices + 1) >) + ...); 
}

template< std::size_t size, std::size_t base >
bool
enumerate()
{
    return (bss_check< base >(std::make_index_sequence< size >{}) + 1 == size);
}

template< std::size_t size, std::size_t ...bases >
bool
expand(std::index_sequence< bases... >)
{
    return (enumerate< size, (bases * size) >() && ...);
}

template< std::size_t size = 100, std::size_t count = size >
bool
check()
{
    return expand< size >(std::make_index_sequence< count >{});
}

int
main()
{
    static_assert(std::is_empty< S< 0 > >{});
    assert((check< DIM >()));
    return EXIT_SUCCESS;
}

并获得结果( DIM == 100size实用程序的输出,即 100 * 100 个类):

   text    data     bss     dec     hex filename
 112724   10612       4  123340   1e1cc ./a.out

如果我将diff(lhs & l, rhs & r)签名更改为diff(lhs l, rhs r)以抑制 ODR 使用,则结果为:

  text     data     bss     dec     hex filename
  69140     608       8   69756   1107c ./a.out

几乎等于(仅对data部分感兴趣) assert((check< DIM >()));的简单注释的情况assert((check< DIM >())); 行( text部分的主要部分是可预测的 DCE 优化):

   text    data     bss     dec     hex filename
   1451     600       8    2059     80b ./a.out

因此我得出结论,没有对 ODR 使用的空类进行优化。

对于明确指定的模板参数,可以使用简单的类型过滤器:

template< typename type >
using ref_or_value = std::conditional_t< std::is_empty< std::decay_t< type > >{}, std::decay_t< type >, type && >;

但是在我看来,推导出的模板类型没有简单的解决方法。

在现代编译器中是否隐含了上述优化? 如果是,如何启用它? 如果没有,目前是否有一种技术可以实现所需的行为?

我知道有时对象的地址会嘀咕很多,但在上述情况下情况并非如此。

我认为像变量或类型的属性(例如[[immaterial]] )会很方便。 也许这样的属性(用于类)应该拒绝的可能性得到归因类(编译时的硬错误)或地址运算符的情况下的地址&无感值768,16返回(实现定义)。

C++17 将添加内联变量以帮助解决其中一些问题,如N4424 中所述 它还解释了一些解决方法。 对于全局函数对象,您可以像这样定义它们:

// Sum function object
struct sum_f
{
    template<class T, class U>
    auto operator()(T x, U y) const
    {
        return x+y;
    }  
};

template<class T>
struct static_const_storage
{
    static constexpr T value = T();
};

template<class T>
constexpr T static_const_storage<T>::value;


template<class T>
constexpr const T& static_const()
{
    return static_const_storage<T>::value;
}

static constexpr auto& sum = static_const<sum_f>();

这使得sum函数对象在翻译单元中是唯一的,从而避免膨胀和 ODR 违规。 但是,这种变通方法对模板变量并不真正有效,最好避免它们(如果您担心可执行文件膨胀),直到我们在 C++17 中获得内联变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM