pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

這個答案 對短字符串優(yōu)化 (SSO) 進行了很好的高級概述.但是,我想更詳細地了解它在實踐中是如何工作的,特別是在 libc++ 實現(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 的條件?這是否取決于目標架構?

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

實現(xiàn)上是如何區(qū)分short和long的訪問字符串數(shù)據(jù)時的字符串?它是像 m_size <= 16 一樣簡單還是作為其他成員變量的一部分的標志?(一世想象一下 m_size 或它的一部分也可能用于存儲字符串數(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).

我專門針對 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++ 可以為字符串類使用兩種稍微不同的內(nèi)存布局進行編譯,這由 _LIBCPP_ALTERNATE_STRING_LAYOUT 標志控制.這兩種布局還區(qū)分了小端和大端機器,這給我們留下了總共 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 個字節(jié),value_type 是 1 個字節(jié),這就是字符串的前 4 個字節(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位,訪問時需要移位:

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?

其他標準庫實現(xiàn)

這個答案 很好地概述了其他標準中的 std::string 內(nèi)存布局庫實現(xiàn).

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*).您已經(jīng)正確剖析了多頭/空頭標志和短格式中的大小字段.

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 個字節(jié)進入尾隨空值(libc++ 將始終在數(shù)據(jù)后面存儲尾隨空值).
  • 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 個字節(jié)來存儲一個短字符串(即最大的 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),同時使內(nèi)部緩沖區(qū)盡可能大.其基本原理是加快移動構建和移動分配.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個字來存儲數(shù)據(jù)指針、大小和容量.因此,我將簡短形式限制為相同的 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 的配置標志,它重新排列數(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_;
};

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

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 混合在一起,將產(chǎn)生運行時錯誤.

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++ 中短字符串優(yōu)化的機制是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(liá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++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 成都LED显示屏丨室内户外全彩led屏厂家方案报价_四川诺显科技 | 冷却塔改造厂家_不锈钢冷却塔_玻璃钢冷却塔改造维修-广东特菱节能空调设备有限公司 | 软膜天花_软膜灯箱_首选乐创品牌_一站式天花软膜材料供应商! | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 | 色谱柱-淋洗液罐-巴罗克试剂槽-巴氏吸管-5ml样品瓶-SBS液氮冻存管-上海希言科学仪器有限公司 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 | 充气膜专家-气膜馆-PTFE膜结构-ETFE膜结构-商业街膜结构-奥克金鼎 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 天津云仓-天津仓储物流-天津云仓一件代发-顺东云仓 | 光伏家 - 太阳能光伏发电_分布式光伏发电_太阳能光伏网 | 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵 | POS机办理_个人POS机免费领取 - 银联POS机申请首页 | 市政路灯_厂家-淄博信达电力科技有限公司 | 刑事律师_深圳著名刑事辩护律师_王平聚【清华博士|刑法教授】 | 大型冰雕-景区冰雕展制作公司,3D创意设计源头厂家-[赛北冰雕] | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 最新范文网_实用的精品范文美文网| 挖掘机挖斗和铲斗生产厂家选择徐州崛起机械制造有限公司 | 气动隔膜泵-电动隔膜泵-循环热水泵-液下排污/螺杆/管道/化工泵「厂家」浙江绿邦 | 美名宝起名网-在线宝宝、公司、起名平台 | 干粉砂浆设备-干粉砂浆生产线-干混-石膏-保温砂浆设备生产线-腻子粉设备厂家-国恒机械 | 乙炔气体报警装置|固定式氯化氢检测仪|河南驰诚电气百科 | 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | YT保温材料_YT无机保温砂浆_外墙保温材料_南阳银通节能建材高新技术开发有限公司 | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 解放卡车|出口|济南重汽|报价大全|山东三维商贸有限公司 | 臭氧灭菌箱-油桶加热箱-原料桶加热融化烘箱-南京腾阳干燥设备厂 臭氧发生器_臭氧消毒机 - 【同林品牌 实力厂家】 | 我爱古诗词_古诗词名句赏析学习平台 | 私人别墅家庭影院系统_家庭影院音响_家庭影院装修设计公司-邦牛影音 | 管理会计网-PCMA初级管理会计,中级管理会计考试网站 | 西宁装修_西宁装修公司-西宁业之峰装饰-青海业之峰墅级装饰设计公司【官网】 | NMRV减速机|铝合金减速机|蜗轮蜗杆减速机|NMRV减速机厂家-东莞市台机减速机有限公司 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 工业风机_环保空调_冷风机_工厂车间厂房通风降温设备旺成服务平台 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 内六角扳手「厂家」-温州市威豪五金工具有限公司 |