gpt4 book ai didi

c++ - 数据成员的编译时重新安排?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:42 25 4
gpt4 key购买 nike

我想知道一种可能的方法使类的内存布局在模板代码中更有效。据我所知,标准要求类的数据成员按照声明的顺序排列在内存中。编译器可能会进行填充,以对齐不必要地添加到类大小的数据成员。这个想法是在编译时重新安排数据成员声明,​​以尽量减少这种填充。我做了一些搜索,但找不到任何信息(大多数时候人们讨论打包编译器指令,这与我看到的不太一样)。

首先,请考虑以下(琐碎但重复且丑陋的)代码 ( same code on ideone.com )(问题在代码下方,请随时跳到它们):

#include <iostream>
#include <cstdint>

namespace so
{
template <typename Ta, typename Tb, typename Tc, std::size_t =
((sizeof(Ta) >= sizeof(Tb)) && (sizeof(Tb) >= sizeof(Tc))) ? 10 :
((sizeof(Ta) >= sizeof(Tc)) && (sizeof(Tc) >= sizeof(Tb))) ? 11 :
((sizeof(Tb) >= sizeof(Ta)) && (sizeof(Ta) >= sizeof(Tc))) ? 20 :
((sizeof(Tb) >= sizeof(Tc)) && (sizeof(Tc) >= sizeof(Ta))) ? 21 :
((sizeof(Tc) >= sizeof(Ta)) && (sizeof(Ta) >= sizeof(Tb))) ? 30 :
((sizeof(Tc) >= sizeof(Tb)) && (sizeof(Tb) >= sizeof(Ta))) ? 31 : 0>
struct foo {};

template <typename Ta, typename Tb, typename Tc>
struct foo<Ta, Tb, Tc, 10>
{
Ta a;
Tb b;
Tc c;
foo(Ta _a, Tb _b, Tc _c) : a{_a}, b{_b}, c{_c} {}
};

template <typename Ta, typename Tb, typename Tc>
struct foo<Ta, Tb, Tc, 11>
{
Ta a;
Tc c;
Tb b;
foo(Ta _a, Tb _b, Tc _c) : a{_a}, c{_c}, b{_b} {}
};

template <typename Ta, typename Tb, typename Tc>
struct foo<Ta, Tb, Tc, 20>
{
Tb b;
Ta a;
Tc c;
foo(Ta _a, Tb _b, Tc _c) : b{_b}, a{_a}, c{_c} {}
};

template <typename Ta, typename Tb, typename Tc>
struct foo<Ta, Tb, Tc, 21>
{
Tb b;
Tc c;
Ta a;
foo(Ta _a, Tb _b, Tc _c) : b{_b}, c{_c}, a{_a} {}
};

template <typename Ta, typename Tb, typename Tc>
struct foo<Ta, Tb, Tc, 30>
{
Tc c;
Ta a;
Tb b;
foo(Ta _a, Tb _b, Tc _c) : c{_c}, a{_a}, b{_b} {}
};

template <typename Ta, typename Tb, typename Tc>
struct foo<Ta, Tb, Tc, 31>
{
Tc c;
Tb b;
Ta a;
foo(Ta _a, Tb _b, Tc _c) : c{_c}, b{_b}, a{_a} {}
};

template <typename Ta, typename Tb, typename Tc>
struct bar: public foo<Ta, Tb, Tc>
{
private:
using base = foo<Ta, Tb, Tc>;
public:
bar() = default;
using base::base;
};

template <typename Ta, typename Tb, typename Tc>
struct foobar
{
Ta a;
Tb b;
Tc c;
foobar() = default;
foobar(Ta _a, Tb _b, Tc _c) : a{_a}, b{_b}, c{_c} {}
};
} //namespace so

int main()
{
so::bar<std::uint16_t, std::uint32_t, std::uint16_t> bar{1, 2, 3};
so::foobar<std::uint16_t, std::uint32_t, std::uint16_t> foobar{1, 2, 3};

std::cout << sizeof(bar) << "\t" << sizeof(foobar) << std::endl;

std::cout << bar.a << " : " << bar.b << " : " << bar.c << std::endl;
std::cout << foobar.a << " : " << foobar.b << " : " << foobar.c << std::endl;

return (0);
}

程序输出:

8   12
1 : 2 : 3
1 : 2 : 3

问题:

  1. 是否有一些众所周知的、独立于编译器的方法来解决此类问题(可能是 Boost)?
  2. 如果不是,是否有一些特定于编译器的指令可以自动执行此类操作(不会像 GCC 的 __atribute__((packed)) 那样发生数据错位)?
  3. 能否以更通用的方式完成(可能使用可变参数模板)?

提前致谢!

最佳答案

我相信我有一个相对简单的可变参数模板解决方案。

虽然实现需要几个助手,所以我将向后展示它,以便您首先了解它的要点。

template <typename... Args>
class OptimizedLayout {
public:
template <size_t I>
auto at() -> decltype(std::get<Index<I>::value>(_storage)) {
return std::get<Index<I>::value>(_storage);
}

template <size_t I>
auto at() const -> decltype(std::get<Index<I>::value>(_storage)) {
return std::get<Index<I>::value>(_storage);
}

private:
using Indexed = /**/; // pairs of sorted Args (by decreasing size)
// and their original index in the argument list

using Storage = /*std::tuple<Indexed.first ...>*/;

template <size_t I>
using Index = /*index of element of Indexed whose .second is I*/;

Storage _storage;
}; // class OptimizedLayout

这里的主要好处是更改元素打包方式只会影响 Indexed 的定义方式,因此您可以轻松改进算法。现在,我将只提供与您的模板等效的内容。


免责声明:以下代码未经测试,甚至可能无法编译,更不用说产生正确的结果了。

我。生成索引。

解释可以在the lounge上找到,我们可以重用它来生成一对(类型,索引)。为了对其进行排序,我们将使用 MPL 算法,因此将包生成为 MPL vector 会更简单。 .

template <std::size_t... Is>
struct indices {};

template <std::size_t N, std::size_t... Is>
struct build_indices
: build_indices<N-1, N-1, Is...> {};

template <std::size_t... Is>
struct build_indices<0, Is...> { using type = indices<Is...>; };

template <typename Tuple, typename Indices>
struct IndexedImpl;

template <typename... T, size_t... I>
struct IndexedImpl< std::tuple<T...>, indices<I...> > {
using type = boost::mpl::vector< std::pair<T, I>... >;
};

template <typename Tuple>
using Indexed =
IndexedImpl<Tuple, typename build_indices<std::tuple_size<Tuple>::value>::type>;

二。排序

为了排序,我们将使用 MPL sort algorithm ,它对类型进行操作。

struct GreaterSize {
template <typename T, typename U>
struct apply {
using type = boost::mpl::bool_<sizeof(T) > sizeof(U)>;
};
};

template <typename T>
struct TupleInserter {
using state = T;

template <typename Seq, typename E>
struct apply;
};

template <typename T>
template <typename... Args, typename E>
struct TupleInserter<T>::apply<std::tuple<Args...>, E> {
using type = std::tuple<Args..., E>;
};

template <typename Tuple>
using SortedSequence = boost::mpl::sort<
typename Indexed<Tuple>::type,
GreaterSize,
TupleInserter
>;

III.计算存储类

现在,我们只需要计算存储类,这是通过提取每对的第一个元素来完成的。有趣的是,模式匹配在这里确实可以提供帮助。

template <typename T>
struct TupleFirstExtractor;

template <typename... T, size_t... I>
struct TupleFirstExtractor<std::tuple<std::pair<T, I>...>> {
using type = std::tuple<T...>;
};

IV.计算索引求解器

template <typename Tuple, size_t Needle, size_t Acc>
struct IndexFinderImpl;

template <typename H, size_t h, typename... Tail, size_t Needle, size_t Acc>
struct IndexFinderImpl<std::tuple<std::pair<H,h>, Tail...>, Needle, Acc>:
IndexFinderImpl<std::tuple<Tail...>, Needle, Acc+1> {};

template <typename H, typename... Tail, size_t Needle, size_t Acc>
struct IndexFinderImpl<std::tuple<std::pair<H, Needle>, Tail...>, Needle, Acc>:
std::integral_constant<size_t, Acc> {};

V.把它们放在一起

现在我们连接一切:

template <typename... Args>
class OptimizedLayout {
public:
template <size_t I>
auto at() -> decltype(std::get<Index<I>::value>(_storage)) {
return std::get<Index<I>::value>(_storage);
}

template <size_t I>
auto at() const -> decltype(std::get<Index<I>::value>(_storage)) {
return std::get<Index<I>::value>(_storage);
}

private:
using Indexed = typename SortedSequence<std::tuple<Args...>>::type;

using Storage = typename TupleFirstExtractor<Indexed>::type;

template <size_t I>
using Index = IndexFinderImpl<Indexed, I, 0>;

Storage _storage;
}; // class OptimizedLayout

提示:我建议使用专门的 namespace 来容纳所有助手。虽然它们可以在模板内定义,但在外部定义它们更容易,因为它们不依赖于 Args...,但是您需要将它们隔离以避免与程序的其他部分发生冲突.

关于c++ - 数据成员的编译时重新安排?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com