std :: string SSO调整 [英] std::string SSO tuning
问题描述
是否可以调整 例如,如果我需要工作很多相当的短字符串,但长度超过15个字符(如 更新: 根据这看起来像答案没有。但是当我打开 现在不清楚为什么 短字符串优化的整个想法是,它不需要额外的空间计算大小,使得当字符串较长时使用的类中的其他变量覆盖了本地缓冲区。 修改系统头是一个坏主意,因为它们通常是编译器版本依赖的,并且有实现细节,使它二进制不兼容。 如评论说,确保这真的是一个问题记住,如果你做了类似的事情: 您将在堆栈上传递 我认为正确的解决方案,一旦你做出决定,你自己的字符串类。 Is it possible to tune For example, if I have to work with a lot of quite short strings but longer than 15 chars (like Upd: According to this looks like the answer is no. But when I opened So now it's not clear why The whole idea with "short string optimisation" is that it "takes no extra space. So the size is calculated such that the local buffer overlays other variables in the class that are used when the string is longer. It is a bad idea to modify system headers, since they are often compiler version dependent, and there are implementation details that make it "binary incompatible". As the comment says, make sure this really is a problem (performance or otherwise) before doing anything about it. And then consider carefully what you should do about it. What problem are you trying to fix, and are you sure it's worth it. Remember that if you do something like: you will copy more bytes on passing And I think the right solution, once you have made a decision, is to implement your own string class. 这篇关于std :: string SSO调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! std :: string
lastname +,+ firstname +,+ middlename
,其长度通常在 [20 ; 40]
)。
basic_string.h
文件时,我发现:
template< typename _CharT,typename _Traits,typename _Alloc>
class basic_string
{
...
enum {_S_local_capacity = 15 / sizeof(_CharT)};
union
{
_CharT _M_local_buf [_S_local_capacity + 1];
size_type _M_allocated_capacity;
...
};
_S_local_capacity
hardcoded this way ...
std :: string func(std :: string arg)
{
...
}
arg
时复制更多字节。没有,它不真正帮助使它 const std :: string& arg
如果你的调用代码产生一个临时字符串,例如。 func(Name:+ name);
。如果你做 vector< std :: string>
,每个的大小会更大,所以向量将占用更多的空间 - 即使对于字符串STILL don适合,因此当您增加/收缩向量时将花费更多的时间。
std :: string
是一个标准的模板库类,它们是不可扩展的,你不应该修改标准库头文件,正如我前面所说,它高度依赖于编译器。这将是相当一些工作,使它完全兼容 std :: string
,但你当然可以骗,并使转换函数运算符std :: string()
为你的字符串类,所以你只需要生成更基本的函数 std :: string
p> std::string
Small/Short String Optimization somehow?lastname + ", " + firstname + ", " + middlename
which length is usually in range of [20; 40]
).basic_string.h
file I've found this:template<typename _CharT, typename _Traits, typename _Alloc>
class basic_string
{
...
enum { _S_local_capacity = 15 / sizeof(_CharT) };
union
{
_CharT _M_local_buf[_S_local_capacity + 1];
size_type _M_allocated_capacity;
...
};
_S_local_capacity
is hardcoded this way... std::string func(std::string arg)
{
...
}
arg
on the stack. And no, it doesn't really help making it const std::string& arg
if your calling code makes a temporary string, e.g. func("Name: " + name);
. And if you do vector<std::string>
, the size of each will be bigger, so the vector will take more space - even for the cases where the string STILL don't fit, so more time will be taken when you grow/shrink the vector.std::string
is a standard template library class, they are not extendable, and you aren't supposed to modify the standard library header files, as, like I said earlier, it's highly compiler dependent. It will be quite some work to make it completely compatible with std::string
, but you could of course "cheat" and make a converter function operator std::string()
for your string class, so you only need to produce the more basic functions that std::string
offers.