久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

libc++ 中短字符串優(yōu)化的機(jī)制是什么?

What are the mechanics of short string optimization in libc++?(libc++ 中短字符串優(yōu)化的機(jī)制是什么?)
本文介紹了libc++ 中短字符串優(yōu)化的機(jī)制是什么?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

這個(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺(tái)輸出?)
How do I calculate the week number given a date?(如何計(jì)算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡(luò)攝像機(jī)的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時(shí)導(dǎo)出所有符號(hào))
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 在线a| 这里只有精品视频 | 亚洲黄色成人 | 久久新视频 | 日本在线一区二区三区 | 天天干网站 | 深夜福利视频在线观看 | 国产精品毛片一区视频播 | 欧美成人免费视频 | 日韩一区二区三区在线 | 亚洲激情偷拍 | 国产一区二区三区视频 | 欧美一区二 | 91亚洲国产成人精品性色 | 中文在线观看免费视频 | 欧美日韩亚洲综合 | 国产亚洲精品码 | 91欧美大片 | 国产日韩亚洲 | 天天躁日日躁bbbbb | 福利片在线 | 亚洲一级在线 | 黄色片一区二区 | 18视频在线观看 | 黄色一级大片 | 欧美日韩一区二区三区 | 成人深夜福利视频 | 国产又粗又黄又爽又硬的视频 | 欧美日韩高清 | 91蝌蚪91九色白浆 | 免费理论片 | 国产老头视频 | 久久国产精品免费 | 九九视频在线免费观看 | 一级片在线播放 | 久久精品一区二区三区不卡牛牛 | 欧美色图一区二区三区 | 国产美女网站 | 丰满少妇av | 狠狠草视频 | 欧美级毛片 |