小型缓冲液优化和对齐
Small Buffer Optimization and alignment
我在遗留API上编写A C 包装器。此API为我提供了保留额外数据的指针值,我想用它来实现小型缓冲区优化。
我已经实现了is_small_pod
metafunction 检查给定类型是否为 pod ,并且适合void*
:
template< typename Type >
struct is_small_pod
: std::integral_constant<
bool
, std::is_pod< Type >::type::value
&& sizeof( Type ) <= sizeof( void* )
>
{};
我要设置这样的值:
// void*& param;
if( detail::is_small_pod< Type >() )
{
*static_cast< Type* >( ¶m ) = value;
} else {
param = new Type( value );
}
我是否正确实施了此优化?我相信,当价值对齐与指针的对齐不兼容时(奇数角案例)。这种情况甚至可能,还是我只是想过它?我应该如何扩展 metafunction 还可以检查兼容对齐?
类型的对齐不可能大于该类型的大小。
3.11对齐[basic.align]
[...] AN ANIGNMENT 是一个实现限制的整数值,代表可以分配给定对象的连续地址之间的字节数。
5.3.3 sizeof [expr.sizeof]
2 - [...] n 元素的数组的大小是 n 乘以元素的大小。
因此,只有在alignof(void *) < sizeof(void *)
时,您的代码只能在大多数平台上破裂。
为了安全,您可以写:
template< typename Type >
struct is_small_pod
: std::integral_constant<
bool
, std::is_pod< Type >::type::value
&& sizeof( Type ) <= sizeof( void* )
&& alignof( Type ) <= alignof( void* )
>
{};
作为一种一般方法,您可以随时尝试测试理论。我想你会做这样的事情:
template< class Type >
bool TestAlignmentSanity( Type value )
{
// This function is only valid for small POD types
if( !detail::is_small_pod< Type >() )
return false;
// Temporary space covering alignments spanning the size of a void*
const int nBytes = sizeof(void*);
char buffer[sizeof(Type) + nBytes - 1];
// For each target alignment, test that a copy is successful.
for( int i = 0; i < nBytes; i++ )
{
Type * target = static_cast< Type* >( &buffer[i] );
// Sanity-check that the pointer was actually aligned as we requested
if( (char*)target != &buffer[i] ) return false;
// Copy and test that the result is as expected. Assumes '==' operator
// is defined... Otherwise, implement with byte comparisons.
*target = value;
if( !(*target == value) ) return false;
}
return true;
}
在这里,我测试了数据类型可以复制为任何跨越void*
大小的对齐。这只是一个想法。=)
相关文章:
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 我可以检测和更改 gcc/g++ 中结构的当前数据对齐设置吗?
- 64位机器上的C++内存对齐
- 为什么我可以将变量存储在不是其最小对齐方式的倍数的地址?
- 使 std::vector 分配对齐内存的现代方法
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 在 64 位边界上对齐C++结构数组?
- 使用 g++7 构建的代码在访问未对齐的内存时崩溃
- 在 capnp FlatArrayMessageReader 的对齐内存缓冲区中接收 zmq 消息
- 是否值得对齐变量?
- 初始化派生结构的基部分/意外打包派生结构字段以对齐基结构的间隙
- 对齐和对齐的实际用例C++关键字
- 对于堆上的页面对齐内存分配是否有任何优化或不同的 API?
- gcc 是否在 2 条短裤的过度对齐结构的比较中错过了优化机会?
- 基于对齐要求的优化
- 小型缓冲液优化和对齐
- 内存对齐优化不仅性能,而且内存大小
- 是否有任何优化选项,可以改变结构的对齐在VS2012
- 矢量化/优化循环,用于宽寄存器(特别是Xeon Phi)的未对齐数据访问