std::string 的两种实现方式

本文详细解析了C++字符串实现中的两种主要技术:Copy-On-Write(写时复制)与短字符串优化,并阐述了在多核机器上使用短字符串优化作为替代方案的性能优势,同时介绍了新libc++0x项目对此的采用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有两种:

1. COW(copy on wirte):

是当前主流的实现,不过在多线程环境下会有性能问题

参考:

How is std::string implemented?

http://stackoverflow.com/questions/1466073/how-is-stdstring-implemented

 

Copy-On-Write的原理及具体实现


Copy On Write(写时复制)

2.Short string optimization

为解决cow暴露的问题而出现另一种实现方案,llvm子项目“新libc++ 0x"采用这种实现。

 

From years of experience (including having implemented the standard library before), we've learned many things about implementing the standard containers which require ABI breakage and fundamental changes to how they are implemented. For example, it is generally accepted that building std::string using the "short string optimization" instead of using Copy On Write (COW) is a superior approach for multicore machines (particularly in C++'0x, which has rvalue references). Breaking ABI compatibility with old versions of the library was determined to be critical to achieving the performance goals of libc++.
 

参考

libc++ 0x 项目

http://libcxx.llvm.org/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值