lap*_*apk 6 c++ templates class datamember memory-layout
我想知道一种可能的方法,使类的内存布局在模板化代码中更有效.据我所知,标准要求一个类的数据成员按照其声明的顺序在内存中进行布局.编译器可能会执行填充以使数据成员对齐,从而不必要地增加类的大小.我们的想法是在编译时重新安排数据成员声明,以尽量减少这种填充.我做了一些搜索,但找不到任何信息(大多数时候人们讨论打包编译器指令,这与我看到的不完全相同).
首先,请考虑以下(琐碎,但重复和丑陋)代码(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);
}
Run Code Online (Sandbox Code Playgroud)
节目输出:
8 12
1 : 2 : 3
1 : 2 : 3
Run Code Online (Sandbox Code Playgroud)
问题:
__atribute__((packed))
)?提前致谢!
我相信我有一个相对简单的可变参数模板解决方案.
实现将需要几个帮助器,所以我将向后呈现它,以便您可以首先获得它的要点.
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
Run Code Online (Sandbox Code Playgroud)
这里的主要好处是更改如何打包元素只会影响Indexed
定义方式,因此您可以轻松改进算法.现在,我将提供相当于您的模板.
免责声明:以下代码未经测试,甚至可能无法编译,更不用说产生正确的结果了.
I.生成指数.
可以在休息室找到解释,我们可以重复使用它来生成一对对(类型,索引).为了对它进行排序,我们将使用MPL算法,因此将包作为MPL向量生成更简单.
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>;
Run Code Online (Sandbox Code Playgroud)
II.排序
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
>;
Run Code Online (Sandbox Code Playgroud)
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...>;
};
Run Code Online (Sandbox Code Playgroud)
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> {};
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
提示:我建议使用专门的命名空间来保存所有帮助程序.虽然可以在模板中定义它们,但更容易在外部定义它们,因为它们不依赖于它们Args...
,但是您需要将它们隔离以避免与程序的其他部分发生冲突.
归档时间: |
|
查看次数: |
220 次 |
最近记录: |