gcc7中std::String的小字符串优化
我运行了一个小测试程序,以验证我们的工具是否受益于SSO:gcc7中std::String的小字符串优化,string,gcc,memory,optimization,libstdc++,String,Gcc,Memory,Optimization,Libstdc++,我运行了一个小测试程序,以验证我们的工具是否受益于SSO: // Example program #include <iostream> #include <string> int main() { std::cout << sizeof(std::string) << " " << std::string().capacity() << std::endl; } //示例程序 #包括 #包括 int main(
// Example program
#include <iostream>
#include <string>
int main()
{
std::cout << sizeof(std::string) << " " << std::string().capacity() << std::endl;
}
//示例程序
#包括
#包括
int main()
{
std::cout这个答案可能有用实际上这个答案只确认我的pov,除非我们假设所有这些额外的字节都用于大小字段。当您读取字符串时,您可以访问_M_指针,您不需要测试您是在小字符串还是大字符串的情况下。各种这样的折衷导致了libs中的不同策略tdc++与libc++的对比。
struct _Alloc_hider : allocator_type // TODO check __is_final
{
#if __cplusplus < 201103L
_Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
: allocator_type(__a), _M_p(__dat) { }
#else
_Alloc_hider(pointer __dat, const _Alloc& __a)
: allocator_type(__a), _M_p(__dat) { }
_Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
: allocator_type(std::move(__a)), _M_p(__dat) { }
#endif
pointer _M_p; // The actual data.
};
_Alloc_hider _M_dataplus;
size_type _M_string_length;
enum { _S_local_capacity = 15 / sizeof(_CharT) };
union
{
_CharT _M_local_buf[_S_local_capacity + 1];
size_type _M_allocated_capacity;
};