問題描述
這個(gè)答案 對(duì)短字符串優(yōu)化 (SSO) 進(jìn)行了很好的高級(jí)概述.但是,我想更詳細(xì)地了解它在實(shí)踐中是如何工作的,特別是在 libc++ 實(shí)現(xiàn)中:
This answer gives a nice high-level overview of short string optimization (SSO). However, I would like to know in more detail how it works in practice, specifically in the libc++ implementation:
字符串必須有多短才能符合 SSO 的條件?這是否取決于目標(biāo)架構(gòu)?
How short does the string have to be in order to qualify for SSO? Does this depend on the target architecture?
實(shí)現(xiàn)上是如何區(qū)分short和long的訪問字符串?dāng)?shù)據(jù)時(shí)的字符串?它是像 m_size <= 16
一樣簡(jiǎn)單還是作為其他成員變量的一部分的標(biāo)志?(一世想象一下 m_size
或它的一部分也可能用于存儲(chǔ)字符串?dāng)?shù)據(jù)).
How does the implementation distinguish between short and long
strings when accessing the string data? Is it as simple as m_size <= 16
or is it a flag that is part of some other member variable? (I
imagine that m_size
or part of it might also be used to store
string data).
我專門針對(duì) libc++ 提出了這個(gè)問題,因?yàn)槲抑浪褂?SSO,甚至在 libc++ 主頁上也提到了這一點(diǎn).
I asked this question specifically for libc++ because I know that it uses SSO, this is even mentioned on the libc++ home page.
以下是查看來源后的一些觀察結(jié)果:
Here are some observations after looking at the source:
libc++ 可以為字符串類使用兩種稍微不同的內(nèi)存布局進(jìn)行編譯,這由 _LIBCPP_ALTERNATE_STRING_LAYOUT
標(biāo)志控制.這兩種布局還區(qū)分了小端和大端機(jī)器,這給我們留下了總共 4 種不同的變體.我將在下面假設(shè)正常"布局和小端.
libc++ can be compiled with two slightly different memory layouts for the string class, this is governed by the _LIBCPP_ALTERNATE_STRING_LAYOUT
flag. Both of the layouts also distinguish between little-endian and big-endian machines which leaves us with a total of 4 different variants. I will assume the "normal" layout and little-endian in what follows.
進(jìn)一步假設(shè) size_type
是 4 個(gè)字節(jié),value_type
是 1 個(gè)字節(jié),這就是字符串的前 4 個(gè)字節(jié)在內(nèi)存中的樣子:
Assuming further that size_type
is 4 bytes and that value_type
is 1 byte, this is what the first 4 bytes of a string would look like in memory:
// short string: (s)ize and 3 bytes of char (d)ata
sssssss0;dddddddd;dddddddd;dddddddd
^- is_long = 0
// long string: (c)apacity
ccccccc1;cccccccc;cccccccc;cccccccc
^- is_long = 1
由于短字符串的大小在高7位,訪問時(shí)需要移位:
Since the size of the short string is in the upper 7 bits, it needs to be shifted when accessing it:
size_type __get_short_size() const {
return __r_.first().__s.__size_ >> 1;
}
類似地,長字符串容量的 getter 和 setter 使用 __long_mask
來繞過 is_long
位.
Similarly, the getter and setter for the capacity of a long string uses __long_mask
to work around the is_long
bit.
我仍在尋找我的第一個(gè)問題的答案,即__min_cap
,短字符串的容量,對(duì)于不同的架構(gòu)有什么價(jià)值?
I am still looking for an answer to my first question, i.e. what value would __min_cap
, the capacity of short strings, take for different architectures?
其他標(biāo)準(zhǔn)庫實(shí)現(xiàn)
這個(gè)答案 很好地概述了其他標(biāo)準(zhǔn)中的 std::string
內(nèi)存布局庫實(shí)現(xiàn).
This answer gives a nice overview of std::string
memory layouts in other standard library implementations.
推薦答案
libc++ basic_string
被設(shè)計(jì)為在所有架構(gòu)上都有 sizeof
3 個(gè)字,其中 sizeof(word) == sizeof(void*)
.您已經(jīng)正確剖析了多頭/空頭標(biāo)志和短格式中的大小字段.
The libc++ basic_string
is designed to have a sizeof
3 words on all architectures, where sizeof(word) == sizeof(void*)
. You have correctly dissected the long/short flag, and the size field in the short form.
對(duì)于不同的架構(gòu),__min_cap(短字符串的容量)會(huì)取什么值?
what value would __min_cap, the capacity of short strings, take for different architectures?
在簡(jiǎn)短的形式中,有 3 個(gè)詞可以使用:
In the short form, there are 3 words to work with:
- 1 位進(jìn)入多頭/空頭標(biāo)志.
- 大小為 7 位.
- 假設(shè)
char
,1 個(gè)字節(jié)進(jìn)入尾隨空值(libc++ 將始終在數(shù)據(jù)后面存儲(chǔ)尾隨空值).
- 1 bit goes to the long/short flag.
- 7 bits goes to the size.
- Assuming
char
, 1 byte goes to the trailing null (libc++ will always store a trailing null behind the data).
這留下了 3 個(gè)字減去 2 個(gè)字節(jié)來存儲(chǔ)一個(gè)短字符串(即最大的 capacity()
沒有分配).
This leaves 3 words minus 2 bytes to store a short string (i.e. largest capacity()
without an allocation).
在 32 位機(jī)器上,10 個(gè)字符將適合短字符串.sizeof(string) 是 12.
On a 32 bit machine, 10 chars will fit in the short string. sizeof(string) is 12.
在 64 位機(jī)器上,22 個(gè)字符將適合短字符串.sizeof(string) 是 24.
On a 64 bit machine, 22 chars will fit in the short string. sizeof(string) is 24.
一個(gè)主要的設(shè)計(jì)目標(biāo)是最小化sizeof(string)
,同時(shí)使內(nèi)部緩沖區(qū)盡可能大.其基本原理是加快移動(dòng)構(gòu)建和移動(dòng)分配.sizeof
越大,在移動(dòng)構(gòu)造或移動(dòng)分配期間必須移動(dòng)的單詞就越多.
A major design goal was to minimize sizeof(string)
, while making the internal buffer as large as possible. The rationale is to speed move construction and move assignment. The larger the sizeof
, the more words you have to move during a move construction or move assignment.
長格式最少需要3個(gè)字來存儲(chǔ)數(shù)據(jù)指針、大小和容量.因此,我將簡(jiǎn)短形式限制為相同的 3 個(gè)單詞.有人建議 4 個(gè)字的 sizeof 可能有更好的性能.我還沒有測(cè)試過這種設(shè)計(jì)選擇.
The long form needs a minimum of 3 words to store the data pointer, size and capacity. Therefore I restricted the short form to those same 3 words. It has been suggested that a 4 word sizeof might have better performance. I have not tested that design choice.
_LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
有一個(gè)名為 _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
的配置標(biāo)志,它重新排列數(shù)據(jù)成員,使長布局"從:
There is a configuration flag called _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
which rearranges the data members such that the "long layout" changes from:
struct __long
{
size_type __cap_;
size_type __size_;
pointer __data_;
};
到:
struct __long
{
pointer __data_;
size_type __size_;
size_type __cap_;
};
此更改的動(dòng)機(jī)是相信將 __data_
放在首位將由于更好的對(duì)齊而具有一些性能優(yōu)勢(shì).試圖衡量性能優(yōu)勢(shì),但很難衡量.不會(huì)使性能變差,可能會(huì)稍微好一點(diǎn).
The motivation for this change is the belief that putting __data_
first will have some performance advantages due to better alignment. An attempt was made to measure the performance advantages, and it was difficult to measure. It won't make the performance worse, and it may make it slightly better.
應(yīng)謹(jǐn)慎使用該標(biāo)志.它是一個(gè)不同的 ABI,如果不小心與使用不同設(shè)置的 _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
編譯的 libc++ std::string
混合在一起,將產(chǎn)生運(yùn)行時(shí)錯(cuò)誤.
The flag should be used with care. It is a different ABI, and if accidentally mixed with a libc++ std::string
compiled with a different setting of _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
will create run time errors.
我建議僅由 libc++ 供應(yīng)商更改此標(biāo)志.
I recommend this flag only be changed by a vendor of libc++.
這篇關(guān)于libc++ 中短字符串優(yōu)化的機(jī)制是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!