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

libc++ 中短字符串優化的機制是什么?

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

問題描述

這個答案 對短字符串優化 (SSO) 進行了很好的高級概述.但是,我想更詳細地了解它在實踐中是如何工作的,特別是在 libc++ 實現中:

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 的條件?這是否取決于目標架構?

  • How short does the string have to be in order to qualify for SSO? Does this depend on the target architecture?

實現上是如何區分short和long的訪問字符串數據時的字符串?它是像 m_size <= 16 一樣簡單還是作為其他成員變量的一部分的標志?(一世想象一下 m_size 或它的一部分也可能用于存儲字符串數據).

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).

我專門針對 libc++ 提出了這個問題,因為我知道它使用 SSO,甚至在 libc++ 主頁上也提到了這一點.

I asked this question specifically for libc++ because I know that it uses SSO, this is even mentioned on the libc++ home page.

以下是查看來源后的一些觀察結果:

Here are some observations after looking at the source:

libc++ 可以為字符串類使用兩種稍微不同的內存布局進行編譯,這由 _LIBCPP_ALTERNATE_STRING_LAYOUT 標志控制.這兩種布局還區分了小端和大端機器,這給我們留下了總共 4 種不同的變體.我將在下面假設正常"布局和小端.

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.

進一步假設 size_type 是 4 個字節,value_type 是 1 個字節,這就是字符串的前 4 個字節在內存中的樣子:

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位,訪問時需要移位:

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.

我仍在尋找我的第一個問題的答案,即__min_cap,短字符串的容量,對于不同的架構有什么價值?

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?

其他標準庫實現

這個答案 很好地概述了其他標準中的 std::string 內存布局庫實現.

This answer gives a nice overview of std::string memory layouts in other standard library implementations.

推薦答案

libc++ basic_string 被設計為在所有架構上都有 sizeof 3 個字,其中 sizeof(word) == sizeof(void*).您已經正確剖析了多頭/空頭標志和短格式中的大小字段.

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.

對于不同的架構,__min_cap(短字符串的容量)會取什么值?

what value would __min_cap, the capacity of short strings, take for different architectures?

在簡短的形式中,有 3 個詞可以使用:

In the short form, there are 3 words to work with:

  • 1 位進入多頭/空頭標志.
  • 大小為 7 位.
  • 假設 char,1 個字節進入尾隨空值(libc++ 將始終在數據后面存儲尾隨空值).
  • 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 個字減去 2 個字節來存儲一個短字符串(即最大的 capacity() 沒有分配).

This leaves 3 words minus 2 bytes to store a short string (i.e. largest capacity() without an allocation).

在 32 位機器上,10 個字符將適合短字符串.sizeof(string) 是 12.

On a 32 bit machine, 10 chars will fit in the short string. sizeof(string) is 12.

在 64 位機器上,22 個字符將適合短字符串.sizeof(string) 是 24.

On a 64 bit machine, 22 chars will fit in the short string. sizeof(string) is 24.

一個主要的設計目標是最小化sizeof(string),同時使內部緩沖區盡可能大.其基本原理是加快移動構建和移動分配.sizeof 越大,在移動構造或移動分配期間必須移動的單詞就越多.

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個字來存儲數據指針、大小和容量.因此,我將簡短形式限制為相同的 3 個單詞.有人建議 4 個字的 sizeof 可能有更好的性能.我還沒有測試過這種設計選擇.

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

有一個名為 _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT 的配置標志,它重新排列數據成員,使長布局"從:

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_;
};

此更改的動機是相信將 __data_ 放在首位將由于更好的對齊而具有一些性能優勢.試圖衡量性能優勢,但很難衡量.不會使性能變差,可能會稍微好一點.

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.

應謹慎使用該標志.它是一個不同的 ABI,如果不小心與使用不同設置的 _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT 編譯的 libc++ std::string 混合在一起,將產生運行時錯誤.

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++ 供應商更改此標志.

I recommend this flag only be changed by a vendor of libc++.

這篇關于libc++ 中短字符串優化的機制是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 欧美成人自拍 | 国产精品夜间视频香蕉 | 国产一级一片免费播放 | 国产综合av | 羞羞视频网| av不卡一区 | 99精品国自产在线 | 欧美一区二区免费 | 精品国产18久久久久久二百 | 欧美视频1区 | 天堂色综合 | 精品国产乱码 | www日本在线播放 | 亚洲一区二区不卡在线观看 | 成人毛片在线观看 | 视频在线观看一区 | 91porn成人精品 | 成人免费观看男女羞羞视频 | 亚洲高清三级 | 欧美美女二区 | 日韩视频在线播放 | 日韩中文字幕av | 最近日韩中文字幕 | 中文字幕一区二区三区日韩精品 | 一区二区三区在线播放视频 | 中文字幕欧美一区 | 人人插人人 | 久久国产亚洲 | 午夜天堂精品久久久久 | 久久99国产精品久久99果冻传媒 | 亚洲成av人影片在线观看 | 亚洲人成在线播放 | 中文字幕在线精品 | 国产在线精品一区二区三区 | 中文字幕亚洲欧美日韩在线不卡 | 国产农村妇女精品一二区 | 日韩视频在线一区二区 | 91se在线 | 久久精品国产亚洲a | 欧美日韩国产一区二区三区 | 亚洲 欧美 另类 综合 偷拍 |