经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++17剖析(1):string_view的实现,以及性能
来源:cnblogs  作者:猴子顶呱呱  时间:2019/1/23 9:27:01  对本文有异议

主要内容

C++17标准发布,string_view是标准新增的内容。这篇文章主要分析string_view的适用范围、注意事项,并分析string_view带来的性能提升,最后从gcc 8.2的libstdc++库源码级别分析性能提升的原因。
C++标准演变

背景知识:静态字符串的处理

所谓静态字符串,就是编译时已经固定的字符串,他们存储在二进制文件的静态存储区,而且程序只能读取,不能改动。
一个例子:

  1. //指针指向静态字符串
  2. const char* str_ptr = "this is a static string";
  3. //字符串数组
  4. char str_array[] = "this is a static string";
  5. //std::string
  6. std::string str = "this is a static string";
  7. //std::string_view
  8. std::string_view sv = "this is a static string";

反汇编:

  1. g++ -O0 -o static_str static_str.cc -std=c++17 -g && objdump -S -t -D static_str > static_str.s

汇编代码如下:

  1. int main()
  2. {
  3. 4013b8: 55 push %rbp
  4. 4013b9: 48 89 e5 mov %rsp,%rbp
  5. 4013bc: 53 push %rbx
  6. 4013bd: 48 83 ec 68 sub $0x68,%rsp
  7. //指针指向静态字符串
  8. const char* str_ptr = "this is a static string!";
  9. ##直接设置字符串指针
  10. 4013c1: 48 c7 45 e8 30 1e 40 movq $0x401e30,-0x18(%rbp)
  11. 4013c8: 00
  12. //字符串数组
  13. char str_array[] = "this is a static string!";
  14. ##这里使用一个很取巧的办法,不使用循环,而是使用多个mov语句把字符串设置到堆栈
  15. 4013c9: 48 b8 74 68 69 73 20 mov $0x2073692073696874,%rax
  16. 4013d0: 69 73 20
  17. 4013d3: 48 ba 61 20 73 74 61 mov $0x6369746174732061,%rdx
  18. 4013da: 74 69 63
  19. 4013dd: 48 89 45 c0 mov %rax,-0x40(%rbp)
  20. 4013e1: 48 89 55 c8 mov %rdx,-0x38(%rbp)
  21. 4013e5: 48 b8 20 73 74 72 69 mov $0x21676e6972747320,%rax
  22. 4013ec: 6e 67 21
  23. 4013ef: 48 89 45 d0 mov %rax,-0x30(%rbp)
  24. 4013f3: c6 45 d8 00 movb $0x0,-0x28(%rbp)
  25. //std::string
  26. std::string str = "this is a static string!";
  27. #esi保存了字符串开始地址$0x401e30,调用std::string的构造函数
  28. 4013f7: 48 8d 45 e7 lea -0x19(%rbp),%rax
  29. 4013fb: 48 89 c7 mov %rax,%rdi
  30. 4013fe: e8 15 fe ff ff callq 401218 <_ZNSaIcEC1Ev@plt>
  31. 401403: 48 8d 55 e7 lea -0x19(%rbp),%rdx
  32. 401407: 48 8d 45 a0 lea -0x60(%rbp),%rax
  33. 40140b: be 30 1e 40 00 mov $0x401e30,%esi
  34. 401410: 48 89 c7 mov %rax,%rdi
  35. 401413: e8 fe 01 00 00 callq 401616 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1IS3_EEPKcRKS3_>
  36. 401418: 48 8d 45 e7 lea -0x19(%rbp),%rax
  37. 40141c: 48 89 c7 mov %rax,%rdi
  38. 40141f: e8 c4 fd ff ff callq 4011e8 <_ZNSaIcED1Ev@plt>
  39. //std::string_view
  40. std::string_view sv = "this is a static string!";
  41. #直接设置字符串的长度0x18,也就是24Bytes,还有字符串的起始指针$0x401e30,没有堆内存分配
  42. 401424: 48 c7 45 90 18 00 00 movq $0x18,-0x70(%rbp)
  43. 40142b: 00
  44. 40142c: 48 c7 45 98 30 1e 40 movq $0x401e30,-0x68(%rbp)
  45. 401433: 00
  46. return 0;
  47. 401434: bb 00 00 00 00 mov $0x0,%ebx
  48. //字符串数组
  49. ## 对象析构:字符串数组分配在栈上,无需析构
  50. char str_array[] = "this is a static string!";
  51. //std::string
  52. ## 对象析构:调用析构函数
  53. std::string str = "this is a static string!";
  54. 401439: 48 8d 45 a0 lea -0x60(%rbp),%rax
  55. 40143d: 48 89 c7 mov %rax,%rdi
  56. 401440: e8 a9 01 00 00 callq 4015ee <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev>
  57. 401445: 89 d8 mov %ebx,%eax
  58. 401447: eb 1a jmp 401463 <main+0xab>
  59. 401449: 48 89 c3 mov %rax,%rbx
  60. 40144c: 48 8d 45 e7 lea -0x19(%rbp),%rax
  61. 401450: 48 89 c7 mov %rax,%rdi
  62. 401453: e8 90 fd ff ff callq 4011e8 <_ZNSaIcED1Ev@plt>
  63. 401458: 48 89 d8 mov %rbx,%rax
  64. 40145b: 48 89 c7 mov %rax,%rdi
  65. 40145e: e8 e5 fd ff ff callq 401248 <_Unwind_Resume@plt>
  66. //std::string_view
  67. ## 对象析构:std::string_view分配在栈上,无需析构
  68. std::string_view sv = "this is a static string!";
  69. return 0;
  70. }
  • 静态字符串:会把指针指向静态存储区,字符串只读。如果尝试修改,会导致段错误(segment fault)。
  • 字符串数组:在栈上分配一块空间,长度等于字符串的长度+1(因为还需要包括末尾的'\0'字符),然后把字符串拷贝到缓冲区。上述代码,我之前一直以为会使用循环(类似memmove),但是一直找不到循环的语句,却找到一堆莫名其妙的数字($0x2073692073696874,$0x6369746174732061)仔细观察发现,原来编译器把一个长字符串分开为几个64bit的长整数,逐次mov到栈缓冲区中, 那几个长长的整数其实是: 0x2073692073696874=[ si siht],$0x6369746174732061=[citats a],0x21676e6972747320=[!gnirts],刚好就是字符串的反序,编译器是用这种方式来提高运行效率的。我觉得其实末尾的0是可以和字符串一起写在同一个mov指令中的,这样执行的指令就可以少一个了,不知道为什么不这样做。
  • std::string:只在寄存器设置了字符串的起始指针,调用了basic_string( const CharT* s,const Allocator& alloc = Allocator() )构造函数,中间涉及各种检测和字符串拷贝,后面会在另一篇讲述std::string原理的文章中详细分析,总之动态内存分配与字符串拷贝是肯定会发生的事情。值得一提的是,如果在构造函数里面至少会有如下操作:确定字符串长度(如strlen,遍历一遍字符串),按字符串长度(或者预留更多的长度)新建一块内存空间,拷贝字符串到新建的内存空间(第二次遍历字符串)。
  • std::string_view:上面的汇编代码很简单,只是单纯设置静态字符串的起始指针和长度,没有其他调用,连内存分配都是栈上的!跟std::string相比,在创建std::string_view对象的时候,没有任何动态内存分配,没有对字符串多余的遍历。一直以来,对于C字符串而言,如果需要获取它的长度,至少需要strlen之类的函数。但是我们似乎忽略了一点,那就是,如果是静态字符串,编译器其实是知道它的长度的,也就是,静态字符串的长度可以在编译期间确定,那就可以减少了很多问题。
  • 题外话:编译期确定字符串长度、对象大小,这种并不是什么奇技淫巧,因为早在operator new运算符重载的时候,就有一个size_t参数,这个就是编译器传入的对象大小,而std::string_view,则是在编译期间传入字符串的指针和长度,构建对象。但是,std::string和std::string_view这两个类同时提供了只带字符串指针同时带字符串指针和字符串长度两个版本的构造函数,默认的情况下,std::string str = "this is a static string!"会调用basic_string( const CharT* s,const Allocator& alloc = Allocator() )构造,但是std::string_view sv = "this is a static string!"会调用带长度的basic_string_view(const _CharT* __str, size_type __len) noexcept版本,这一点我一直没弄明白(TODO)。但是,标准库提供了一个方法,可以让编译器选择带长度的std::string构造函数,下一小节讲述。

std::string_view的实现(GCC 8.2)

std::string_view类的成员变量只包含两个:字符串指针和字符串长度。字符串指针可能是某个连续字符串的其中一段的指针,而字符串长度也不一定是整个字符串长度,也有可能是某个字符串的一部分长度。std::string_view所实现的接口中,完全包含了std::string的所有只读的接口,所以在很多场合可以轻易使用std::string_view代替std::string。一个通常的用法是,生成一个std::string后,如果后续的操作不再对其进行修改,那么可以考虑把std::string转换成为std::string_view,后续操作全部使用std::string_view来进行,这样字符串的传递变得轻量级。虽然在很多实现上,std::string都使用引用计数进行COW方式管理,但是引用计数也会涉及锁和原子计数器,而std::string_view的拷贝只是单纯拷贝两个数值类型变量(字符串指针及其长度),效率上前者是远远无法相比的。std::string_view高效的地方在于,它不管理内存,只保存指针和长度,所以对于只读字符串而言,查找和拷贝是相当简单的。下面主要以笔记的形式,了解std::string_view的实现。

  • 只读操作:没有std::string的c_str()函数。因为std::string_view管理的字符串可能只是一串长字符串中的一段,而c_str()函数的语义在于返回一个C风格的字符串,这会引起二义性,可能这就是设计者不提供这个接口的原因。但是与std::string一样提供了data()接口。对于std::string而言,data()与c_str()接口是一样的。std::string_view提供的data()接口只返回它所保存的数据指针,语义上是正确的。在使用std::string_view的data()接口的时候,需要注意长度限制,例如cout<<sv.data();cout<<sv;的输出结果很可能是不一样的,前者会多输出一部分字符。
  • std::string_view与std::string的生成:C++17新增了operator""sv(const char* __str, size_t __len)operator""s(const char* __str, size_t __len)操作符重载,因此,生成字符串的方法可以使用这两个操作符。令人惊奇的是,使用这种方法,生成std::string调用的是basic_string_view(const _CharT* __str, size_type __len) noexcept版本的构造函数,这就意味着免去了构造时再一次获取字符串长度的开销(实际上是编译器在帮忙)
  1. //std::string
  2. std::string str = "this is a static string!"s;
  3. //std::string_view
  4. std::string_view sv = "this is a static string!"sv;

反汇编如下(其实读者可以使用gdb调试,查看实际调用的构造函数):
```cpp

  1. //std::string
  2. std::string str = "this is a static string!"s;
  3. ## esi存放字符串起始地址,edx存放字符串长度,0x18就是字符串长度24字节
  4. 4014b7: 48 8d 45 a0 lea -0x60(%rbp),%rax
  5. 4014bb: ba 18 00 00 00 mov $0x18,%edx
  6. 4014c0: be 50 1e 40 00 mov $0x401e50,%esi
  7. 4014c5: 48 89 c7 mov %rax,%rdi
  8. 4014c8: e8 da 00 00 00 callq 4015a7 <_ZNSt8literals15string_literalsli1sB5cxx11EPKcm>

```

  • 修改操作:如前所述,std::string_view并不提供修改接口,因为它保存的数据指针是const _CharT*类型的,无法运行时修改。
  • 字符串截取substr():这部分特别提出。因为使用std::string::substr()函数,会对所截取的部分生成一个新的字符串返回(中间又涉及内存动态分配以及拷贝),而std::string_view::substr(),也是返回一个std::string_view,但是依旧不涉及内存的动态分配。只是简单地用改变后的指针和长度生成一个新的std::string_view对象,O(1)操作。代码如下:
  1. constexpr basic_string_view
  2. substr(size_type __pos, size_type __n = npos) const noexcept(false)
  3. {
  4. __pos = _M_check(__pos, "basic_string_view::substr");
  5. const size_type __rlen = std::min(__n, _M_len - __pos);
  6. return basic_string_view{_M_str + __pos, __rlen};
  7. }
  • 关于字符串截取,引用一下其他人的测试结果,性能提高不是一星半点。(来自这里
    std::string_view与std::string的字符串截取substr性能对比

使用注意事项

std::string_view/std::string用于项目中,我认为有下面几点需要注意的:

  • std::string_view管理的只是指针,试用期间应该注意指针所指向的内存是可访问的;
  • 如果使用静态字符串初始化std::string,建议使用operator s()重载,但是使用这个运算符重载需要使用std::literals,反正我经常会忘记。
  • 如果在项目中需要使用下面这种方式生成字符串的:
  1. int num = 100;
  2. //process @num
  3. std::string err_message = "Invalid Number: " + std::to_string(num);

在c++11有可能会报错,因为 "Invalid Number: " 是一个const char*,无法使用operator +(const std::string&),或者改为

  1. std::string err_message = std::string("Invalid Number: ") + std::to_string(num);

在C++17中,可以使用如下方法:

  1. using namespace std::literals;
  2. std::string err_message = "Invalid Number: "s + std::to_string(num);

这样,可以让编译器在构造时调用带长度的构造函数,免去一次使用strlen获取长度的开销。

上古时代的std::string_view及其类似实现

所谓“上古时代”,指的是C++11之前的C++98时代,当时标准库还没有这么充实,开发时需要用到的一些库需要自己实现。那时候一些注重效率的程序就提供了这类的库作为附属实现。如:

我的项目中用到的std::string_view的类似实现:针对libhiredis

在上古时代,我的项目中也用到类似std::string_view这种“轻量级字符串”的功能,下面晒晒代码,说说使用这种设计的初衷。
在项目中,我需要用到redis库hiredis,经常需要从库里面取得字符串。比如这样的操作:从redis中scan出一堆key,然后从redis中取出这些key,这些key-value有可能用于输出,有可能用于返回。hiredis是一个C库,快速而简单,然而我不希望在我的应用层库中处理太多细节(诸如分析返回数据的类型,然后又进行错误处理,等等),因为那样会造成大量重复代码(对返回数据的处理),而且会让应用层代码变得很臃肿。于是我自己写了一个简单的adaptor,实现了使用C++的string、vector等类作为参数对hiredis的调用。那么redis返回的字符串,如果封装成std::string,字符串的拷贝会成为瓶颈(因为项目中的value部分是一些稍长的字符串),而且这些来自redis的value返回到应用层只会做一些json解析、protobuf解析之类的操作就被释放掉,所以这就考虑到字符串的拷贝和释放完全是重复劳动,于是自己设计了一个基于RedisReply的Slice实现。
下面只贴出头文件,实现部分就不多贴出来占地方了(代码其实是使用C++11开发的,但是类似的实现可以在C++98中轻易做到,在这里作为一个例子并不过分=_=):

  1. //字符串
  2. //创建这个类,是因为在性能调优的时候发现,生成字符串太多,影响性能
  3. class Slice
  4. {
  5. public:
  6. Slice() = default;
  7. ~Slice() = default;
  8. Slice(const char* str, size_t len,
  9. const std::shared_ptr<const redisReply>& reply): str_(str), len_(len), reply_(reply) {}
  10. Slice(const char* str, size_t len):str_(str), len_(len) {}
  11. //下面几个接口,兼容std::string
  12. const char* c_str() const {return str_;}
  13. const char* data() const {return str_;}
  14. size_t length() const {return len_;}
  15. bool empty() const {return str_ == NULL || len_ == 0;}
  16. bool begin_with(const std::string& str) const;
  17. std::string to_string() const;
  18. bool operator==(const char* right) const;
  19. bool operator==(const Slice& right) const;
  20. bool operator!=(const char* right) const;
  21. bool operator!=(const Slice& right) const;
  22. private:
  23. //字符串
  24. const char* str_{NULL};
  25. size_t len_{0};
  26. //字符串所属的Redis返回报文
  27. std::shared_ptr<const redisReply> reply_;
  28. };

之所以不重用LevelDB的Slice,是因为这些字符串都是struct redisReply中分配的,所以使用shared_ptr管理struct redisReply对象,这样就可以不需要担心struct redisReply的释放问题了。
为了这个类的使用方式兼容std::stringSlice,我使用模板实现,下面是我的Redis适配层的实现(局部):

  1. /**********头文件************/
  2. class CustomizedRedisClient
  3. {
  4. public:
  5. //GET
  6. template<class StringType>
  7. std::pair<Status, Slice> get(const StringType& key)
  8. {
  9. return this->get_impl(key.data(), key.length());
  10. }
  11. //....
  12. };
  13. /***********这部分在代码部分实现***********/
  14. //GET实现
  15. //CustomizedRedisClient::Status是另外实现的一个状态码,不在这里讲述
  16. std::pair<CustomizedRedisClient::Status, CustomizedRedisClient::Slice>
  17. CustomizedRedisClient::get_impl(const char* key, size_t key_len)
  18. {
  19. constexpr size_t command_item_count = 2;
  20. const char* command_str[command_item_count];
  21. size_t command_len[command_item_count];
  22. command_str[0] = "GET";
  23. command_len[0] = 3;
  24. command_str[1] = key;
  25. command_len[1] = key_len;
  26. //reply
  27. //get_reply()函数使用redisAppendCommandArgv()和redisGetReply()函数实现,参考libhiredis文档,这样做是为了兼顾key/value中可能有二进制字符
  28. const auto& reply_status = this->get_reply(command_str, command_len, command_item_count);
  29. const redisReply* reply = reply_status.first.get();
  30. if(reply == NULL)
  31. {
  32. return std::make_pair(reply_status.second,
  33. CustomizedRedisClient::Slice());
  34. }
  35. else if(reply->type == REDIS_REPLY_STATUS
  36. || reply->type == REDIS_REPLY_ERROR)
  37. {
  38. return std::make_pair(CustomizedRedisClient::Status(std::string(reply->str, reply->len)),
  39. CustomizedRedisClient::Slice());
  40. }
  41. else if(reply->type == REDIS_REPLY_NIL)
  42. {
  43. return std::make_pair(CustomizedRedisClient::Status(STATUS_NOT_FOUND),
  44. CustomizedRedisClient::Slice());
  45. }
  46. else if(reply->type != REDIS_REPLY_STRING)
  47. {
  48. return std::make_pair(CustomizedRedisClient::Status(STATUS_INVALID_MESSAGE),
  49. CustomizedRedisClient::Slice());
  50. }
  51. return std::make_pair(CustomizedRedisClient::Status(),
  52. CustomizedRedisClient::Slice(reply->str, reply->len, reply_status.first));
  53. }

后记

追本溯源,是一个极客的优秀素质。
作为C++17文章的第一篇,略显啰嗦,希望以后有恒心把自己的研究成果一直进行下去。

原文链接:http://www.cnblogs.com/monkeyteng/p/10304610.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号