【问题标题】:What does "in-situ without memory allocation" mean for folly implementation of String?“原位没有内存分配”对于 String 的愚蠢实现意味着什么?
【发布时间】:2020-09-12 18:31:42
【问题描述】:

来自 Folly 文档

Small strings (<= 23 chars) are stored in-situ without memory allocation.
Medium strings (24 - 255 chars) are stored in malloc-allocated memory and copied eagerly.
Large strings (> 255 chars) are stored in malloc-allocated memory and copied lazily.

这些“小字符串”存储在哪里?

【问题讨论】:

  • 不能肯定地说,但很可能他们的意思是他们使用对象本身的胆量,比如实际字符串成员和 char 数组的联合,并且他们使用 char 数组直到字符串增长,然后切换到使用字符串成员并分配。
  • 听起来像是 Folly 版本的小字符串优化(又名 SSO):stackoverflow.com/questions/10315041/…

标签: c++ string folly


【解决方案1】:

这意味着std::string 的成本已经包含 23 个字符。更多内容需要额外分配。

这意味着内部结构大致如下:

struct string {
  // ...
  char internal[23];
  char* external;
};

这大概是为了让复制短字符串变得非常便宜,因为不必执行堆操作,而且它不需要任何delete 调用来清除。

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2015-01-26
    相关资源
    最近更新 更多