Modern C++ 内存篇1 - allocator

news2024/9/23 3:19:12

1. 前言

从今天起我们开始内存相关的话题,内存是个很大的话题,一时不知从何说起。内存离不开allocator,我们就从allocator开始吧。allocator目前有两种:std::allocator, std::pmr::polymorphic_allocator,各有优缺点。
上来就长篇大论容易显得枯燥,我们还是抛出一个例子然后提出问题,通过问题慢慢深入。

2. 分配器例子

下面这个例子是我很久以前从一个网站上copy下来的。是个不错的用来快速学习的例子。作者当时留了个疑问没解决:为什么预分配内存的pmr反而效率更低哪?
这也是本节我们要解决的问题,从中也可以学到allocator和polymorphic_allocator的优缺点对比。

#include<iostream>
#include<memory_resource>
#include<vector>
#include "../PerfSum.hpp"
using namespace std;

void TestPmrVec(){
    char buffer[1000000*4] = {0};
    std::pmr::monotonic_buffer_resource mbr{ std::data(buffer), std::size(buffer) };
    std::pmr::polymorphic_allocator<int> pa{&mbr};
    std::pmr::vector<int> vec{pa};
    //vec.push_back(0);
    //vec.push_back(1);
    PerfSum t;
     for(int i=0;i<1000000;i++){
         vec.push_back(i);
     }
     std::cout<<"End"<<std::endl;
 }

 void TestStdVec(){
     std::vector<int> vec ;
     PerfSum t;
         //vec.push_back(0);
         //vec.push_back(1);
     for(int i=0;i<1000000;i++){
         vec.push_back(i);
     }
     std::cout<<"End"<<std::endl;
 }

int main() {
    std::cout<<"std vector cost:"<<std::endl;
    TestStdVec();
    std::cout<<"pmr vector cost:"<<std::endl;
    TestPmrVec();
}

其中PerfSum.hpp在《Modern C++ idiom3:RAII》中有提到。编译运行结果:

[mzhai@std_polymorphic_pmr]$ g++ compare_speed.cpp -std=c++17 -g
[mzhai@std_polymorphic_pmr]$ ./a.out
std vector cost:
End
 took 19171 microseconds.
pmr vector cost:
End
 took 56134 microseconds.

可见pmr反而比普通的vector慢了大约3倍。

注意:pmr是c++17开始支持的standard library features, gcc9.1开始支持。

3. pmr慢的原因

启动perf, 查热点:

[mzhai@std_polymorphic_pmr]$ sudo sysctl -w kernel.kptr_restrict=0
sudo sysctl -w kernel.perf_event_paranoid=0
[sudo] password for mzhai:
kernel.kptr_restrict = 0
kernel.perf_event_paranoid = 0
[mzhai@std_polymorphic_pmr]$ perf record -a -g ./a.out
std vector cost:
End
 took 17302 microseconds.
pmr vector cost:
End
 took 58350 microseconds.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.100 MB perf.data (369 samples) ]
[mzhai@std_polymorphic_pmr]$ perf report

在这里插入图片描述
找到__uninitialized_copy_a的实现,我的机器在目录/usr/include/c++/11/bits/stl_uninitialized.h中:

请添加图片描述
从perf report能隐约看出调用栈,__uninitialized_copy_a是从push_back -> _M_realloc_insert 调过来的,从名字猜也能猜到是vector旧的分配的空间不够了需要reallocate, 分配完新的空间后需要调用__uninitialized_copy_a把旧的数据copy或move过来,但是重点是:这里竟然是for循环,是一个个copy或move过来的!

4. std::allocator快的原因

作为对比,我们查下std::vector 空间不够是怎么做的?
读者可自行调试TestStdVec,我这里直接上代码:

#0  std::__relocate_a_1<int, int> (__first=0x41b2e8, __last=0x41b2e8, __result=0x41b2cc) at /usr/include/c++/11/bits/stl_uninitialized.h:1012
#1  0x000000000040451f in std::__relocate_a<int*, int*, std::allocator<int> > (__first=0x41b2e8, __last=0x41b2e8, __result=0x41b2cc, __alloc=...)
    at /usr/include/c++/11/bits/stl_uninitialized.h:1046
#2  0x000000000040423f in std::vector<int, std::allocator<int> >::_S_do_relocate (__first=0x41b2e8, __last=0x41b2e8, __result=0x41b2cc, __alloc=...)
    at /usr/include/c++/11/bits/stl_vector.h:456
#3  0x0000000000403e5d in std::vector<int, std::allocator<int> >::_S_relocate (__first=0x41b2e8, __last=0x41b2e8, __result=0x41b2cc, __alloc=...)
    at /usr/include/c++/11/bits/stl_vector.h:469
#4  0x000000000040376a in std::vector<int, std::allocator<int> >::_M_realloc_insert<int const&> (this=0x7fffffffdb70, __position=0)
    at /usr/include/c++/11/bits/vector.tcc:468
#5  0x0000000000402f24 in std::vector<int, std::allocator<int> >::push_back (this=0x7fffffffdb70, __x=@0x7fffffffdb3c: 2)

请添加图片描述
直接调用__builtin_memmove把旧数据一股脑memmove过去,能不快吗?!
可能有读者有一点点疑问:想__builtin_memmove真的调用memmove吗?简单看下汇编就知道啦。请添加图片描述

何时调用memmove何时调用for循环

通过上面的分析,我们现在知道了pmr慢而普通allocator快的原因了,接着新的问题来了:为什么pmr不走memmove? 什么条件下走memmove哪?
在这里插入图片描述

/usr/include/c++/11/bits/vector.tcc
423   template<typename _Tp, typename _Alloc>
 424     template<typename... _Args>
 425       void
 426       vector<_Tp, _Alloc>::
 427       _M_realloc_insert(iterator __position, _Args&&... __args)
 
 434     {

 458 #if __cplusplus >= 201103L
 459       if _GLIBCXX17_CONSTEXPR (_S_use_relocate())
 460         {
 461           __new_finish = _S_relocate(__old_start, __position.base(),
 462                      __new_start, _M_get_Tp_allocator());
 463
 464           ++__new_finish;
 465
 466           __new_finish = _S_relocate(__position.base(), __old_finish,
 467                      __new_finish, _M_get_Tp_allocator());
 468         }
 469       else
 470 #endif
 471         {
 472           __new_finish
 473         = std::__uninitialized_move_if_noexcept_a
 474         (__old_start, __position.base(),
 475          __new_start, _M_get_Tp_allocator());
 476
 477           ++__new_finish;
 478
 479           __new_finish
 480         = std::__uninitialized_move_if_noexcept_a
 481         (__position.base(), __old_finish,
 482          __new_finish, _M_get_Tp_allocator());
 483         }

关键点在_S_use_relocate()的值,此函数的定义如下:

/usr/include/c++/11/bits/stl_vector.h
 430       static constexpr bool
 431       _S_nothrow_relocate(true_type)
 432       {
 433     return noexcept(std::__relocate_a(std::declval<pointer>(),
 434                       std::declval<pointer>(),
 435                       std::declval<pointer>(),
 436                       std::declval<_Tp_alloc_type&>()));
 437       }
 438
 439       static constexpr bool
 440       _S_nothrow_relocate(false_type)
 441       { return false; }
 442
 443       static constexpr bool
 444       _S_use_relocate()
 445       {
 446     // Instantiating std::__relocate_a might cause an error outside the
 447     // immediate context (in __relocate_object_a's noexcept-specifier),
 448     // so only do it if we know the type can be move-inserted into *this.
 449     return _S_nothrow_relocate(__is_move_insertable<_Tp_alloc_type>{});
 450       }
  1. 首先看__is_move_insertable<_Tp_alloc_type>{},无论_Tp_alloc_type是std::allocator 还是std::pmr::polymorphic_allocator,结果是true.
785   template<typename _Alloc>
786     struct __is_move_insertable
787     : __is_alloc_insertable_impl<_Alloc, typename _Alloc::value_type>::type
788     { };
789
790   // std::allocator<_Tp> just requires MoveConstructible
791   template<typename _Tp>
792     struct __is_move_insertable<allocator<_Tp>>
793     : is_move_constructible<_Tp>
794     { };

std::allocator匹配后者(791行),is_move_constructible为true;
pmr匹配前者(785行), 匹配下面的两者之一。
此处用了SFINAE思想,如果_Alloc能用_Tp做参数类型构造一个_ValueT*对象,则匹配true的这个模板,否则false, 分别对应__is_move_insertable的结果此处用了SFINAE思想,如果_Alloc能用_Tp做参数类型构造一个_ValueT*对象,则匹配true的这个模板,否则false, 分别对应__is_move_insertable的结果。std::allocator及polymorphic_allocator都有construct函数,构造int对象没问题。
2. 看std::__relocate_a是否抛出异常,__relocate_a会看__relocate_a_1是否抛出异常,而__relocate_a_1会看__relocate_object_a是否抛出异常,__relocate_object_a是否抛出异常取决于:

984   template<typename _Tp, typename _Up, typename _Allocator>
 985     inline void
 986     __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig,
 987             _Allocator& __alloc)
 988     noexcept(noexcept(std::allocator_traits<_Allocator>::construct(__alloc,
 989              __dest, std::move(*__orig)))
 990          && noexcept(std::allocator_traits<_Allocator>::destroy(
 991                 __alloc, std::__addressof(*__orig))))

std::allocator_traits<_Allocator>::construct 取决于 std::is_nothrow_constructible<_Up, _Args…>::value
std::allocator_traits<_Allocator>::destroy 取决于 is_nothrow_destructible<_Up>::value

以上仅当所有情况都是noexcept为true才会走_S_relocate的分支(不走__uninitialized_move_if_noexcept_a)。

不过除此之外__relocate_a_1还有一个特例:

1000   template<typename _Tp, typename = void>
1001     struct __is_bitwise_relocatable
1002     : is_trivial<_Tp> { };
1003
1004   template <typename _Tp, typename _Up>
1005     inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
1006     __relocate_a_1(_Tp* __first, _Tp* __last,
1007            _Tp* __result, allocator<_Up>&) noexcept
1008     {
1009       ptrdiff_t __count = __last - __first;
1010       if (__count > 0)
1011     __builtin_memmove(__result, __first, __count * sizeof(_Tp));
1012       return __result + __count;
1013     }

如果_Tp(我的例子里是int)是trivial的 且 分配器是std::allocator,则__relocate_a_1是noexcept的,则走_S_relocate的分支(不走__uninitialized_move_if_noexcept_a)

草草的画了一个流程图(大家凑合看):
在这里插入图片描述

上面两条捋了一遍_S_use_relocate()的结果, 但并不是它是true就一定用memmove,

/usr/include/c++/11/bits/stl_vector.h
 452       static pointer
 453       _S_do_relocate(pointer __first, pointer __last, pointer __result,
 454              _Tp_alloc_type& __alloc, true_type) noexcept
 455       {
 456     return std::__relocate_a(__first, __last, __result, __alloc);
 457       }
 458
 459       static pointer
 460       _S_do_relocate(pointer, pointer, pointer __result,
 461              _Tp_alloc_type&, false_type) noexcept
 462       { return __result; }
 463
 464       static pointer
 465       _S_relocate(pointer __first, pointer __last, pointer __result,
 466           _Tp_alloc_type& __alloc) noexcept
 467       {
 468     using __do_it = __bool_constant<_S_use_relocate()>;
 469     return _S_do_relocate(__first, __last, __result, __alloc, __do_it{});
 470       }

459行永远也走不到,因为_S_use_relocate()位true才会调用到这,而其值为true则一定匹配452行的函数特化版本。
__relocate_a最终调用到__relocate_a_1,上面提到过它有两个版本:
只有_Tp是trivial 且 用std::allocator 才会调用memmove。

1004   template <typename _Tp, typename _Up>
1005     inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
1006     __relocate_a_1(_Tp* __first, _Tp* __last,
1007            _Tp* __result, allocator<_Up>&) noexcept
1008     {
1009       ptrdiff_t __count = __last - __first;
1010       if (__count > 0)
1011     __builtin_memmove(__result, __first, __count * sizeof(_Tp));
1012       return __result + __count;
1013     }
1014
1015   template <typename _InputIterator, typename _ForwardIterator,
1016         typename _Allocator>
1017     inline _ForwardIterator
1018     __relocate_a_1(_InputIterator __first, _InputIterator __last,
1019            _ForwardIterator __result, _Allocator& __alloc)
1020     noexcept(noexcept(std::__relocate_object_a(std::addressof(*__result),
1021                            std::addressof(*__first),
1022                            __alloc)))
1023     {
1024       typedef typename iterator_traits<_InputIterator>::value_type
1025     _ValueType;
1026       typedef typename iterator_traits<_ForwardIterator>::value_type
1027     _ValueType2;
1028       static_assert(std::is_same<_ValueType, _ValueType2>::value,
1029       "relocation is only possible for values of the same type");
1030       _ForwardIterator __cur = __result;
1031       for (; __first != __last; ++__first, (void)++__cur)

5. 看一个简单的class的例子

上面我用的是int,下面用一个简单的类看看,验证下上面的流程图。
我就不分析了,大家执行代码看结果来理解吧。

#include<iostream>
#include<memory_resource>
#include<vector>
#include "../PerfSum.hpp"
using namespace std;

struct MyClass{
        MyClass(int _i):i(_i) {}
        int i;
};

void TestPmrVec(){
    char buffer[1000000*4] = {0};
    std::pmr::monotonic_buffer_resource pool{
        std::data(buffer), std::size(buffer)
    };
    std::pmr::vector<MyClass> vec{&pool};
    PerfSum t;
     for(int i=0;i<1000000;i++){
         vec.push_back(MyClass{i});
     }
     std::cout<<"End"<<std::endl;
 }

 void TestStdVec(){
     std::vector<MyClass> vec ;
     PerfSum t;
     for(int i=0;i<1000000;i++){
         vec.push_back(MyClass{i});
     }
     std::cout<<"End"<<std::endl;
 }

int main() {
        std::cout<<"is_move_constructible<MyClass>: "<<std::is_move_constructible_v<MyClass><<std::endl;
        std::cout<<"is_nothrow_constructible<MyClass>: "<<std::is_nothrow_constructible_v<MyClass,MyClass&&><<std::endl;
        std::cout<<"is_nothrow_destructible<MyClass>: "<<std::is_nothrow_destructible_v<MyClass><<std::endl;
        std::cout<<"trivail<MyClass>: "<<std::is_trivial_v<MyClass><<std::endl;

    std::cout<<"std vector cost:"<<std::endl;
    TestStdVec();
    std::cout<<"pmr vector cost:"<<std::endl;
    TestPmrVec();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1440748.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

中小型网络系统总体规划与设计方法

目录 1.基于网络的信息系统基本结构 2.网络需求调研与系统设计原则 3.网络用户调查 4.网络节点地理位置分布情况 5.网络需求详细分析 6.应用概要分析 7.网络工程设计总体目标与设计原则 8.网络结构与拓扑构型设计方法 9.核心层网络结构设计 10.接入核心路由器 11.汇聚…

寒假作业2024.2.8

1.现有文件test.c\test1.c\main.c,请编写Makefile Makefile文件&#xff1a; CCgcc EXEfile OBJS$(patsubst %.c,%.o,$(wildcard *.c)) CFLAGS-c -o all:$(EXE)file:test.o test1.o main.o$(CC) $^ -o $%.o:%.c$(CC) $(CFLAGS) $ $^.PHONY:clean clean:rm $(OBJS)main.c文件:…

Codeforces Round 260 (Div. 1)A. Boredom(dp)

最开始写了一发贪心wa了&#xff0c;然后这种选和不选的组合优化问题&#xff0c;一般是考虑动态规划 d p [ i ] [ 0 ] &#xff1a; dp[i][0]&#xff1a; dp[i][0]&#xff1a;表示第i个数不选的最大值 d p [ i ] [ 1 ] &#xff1a; dp[i][1]&#xff1a; dp[i][1]&#xf…

【LeetCode】37. 解数独(困难)——代码随想录算法训练营Day30

题目链接&#xff1a;37. 解数独 题目描述 编写一个程序&#xff0c;通过填充空格来解决数独问题。 数独的解法需 遵循如下规则&#xff1a; 数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。&…

Python进阶--爬取美女图片壁纸(基于回车桌面网的爬虫程序)

目录 一、前言 二、爬取下载美女图片 1、抓包分析 a、分析页面 b、明确需求 c、抓包搜寻 d、总结特点 2、编写爬虫代码 a、获取图片页网页源代码 b、提取所有图片的链接和标题 c、下载并保存这组图片 d、 爬取目录页的各种类型美女图片的链接 e、实现翻页 三、各…

TCP和UDP相关问题(重点)——8.TCP的拥塞控制怎么实现的?

在某段时间内&#xff0c;若对网络中某一资源的需求超过了该资源所能提供的可用部分&#xff0c;网络性能就会变坏&#xff0c;比如在高速公路上行驶的车辆&#xff0c;如果一时期内涌入了太多的车辆&#xff0c;道路将变得拥堵&#xff0c;交通状况变差。网络中也是一样&#…

Android 识别车牌信息

打开我们心爱的Android Studio 导入需要的资源 gradle //开源车牌识别安卓SDK库implementation("com.github.HyperInspire:hyperlpr3-android-sdk:1.0.3")button.setOnClickListener(v -> {Log.d("Test", "");try (InputStream file getAs…

Java并发基础:Deque接口和Queue接口的区别?

核心概念 Deque&#xff08;double ended queue&#xff0c;双端队列&#xff09;和Queue&#xff08;队列&#xff09;都是Java集合框架中的接口&#xff0c;它们用于处理元素的排队和出队&#xff0c;但是它们之间存在一些重要的区别&#xff0c;如下&#xff1a; 1、Queue…

RSA算法加密、签名和验签、解密

一、背景介绍 RSA是一种非对称加密算法&#xff0c;该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。通常个人保存私钥&#xff0c;公钥是公开的&#xff08;可能同时多人持有&#xff09;。 二、RSA算法工具类 package com.hl.rsademo.util;import java.i…

MYSQL分区NOW()不支持

传说同事写个复杂的SQL代码,跑一次需要7-10秒, 复杂如上,我也懒得去分析 IF IF IF是怎么回事了! 发现此表是分区表,后面要求加上了分区时间,以便利用到分区裁剪技术. 因为需求是查近10天来到期还款的人和金额.就是今天应该还款的人, 一般还款周期是7天. 给个10天的范围挺可以的…

【DDD】学习笔记-领域实现模型

实现模型与编码质量 领域设计模型体现了类的静态结构与动态协作&#xff0c;领域实现模型则进一步把领域知识与技术实现连接起来&#xff0c;但同时它必须守住二者之间的边界&#xff0c;保证业务与技术彼此隔离。这条边界线应由设计模型明确给出&#xff0c;其中的关键是遵循…

基于SSM的网络在线考试系统(有报告)。Javaee项目。ssm项目。

演示视频&#xff1a; 基于SSM的网络在线考试系统&#xff08;有报告&#xff09;。Javaee项目。ssm项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Spring …

数字图像处理实验记录十(图像分割实验)

一、基础知识 1、什么是图像分割 图像分割就是指把图像分成各具特性的区域并提取出感兴趣目标的技术和过程&#xff0c;特性可以是灰度、颜色、纹理等&#xff0c;目标可以对应单个区域&#xff0c;也可以对应多个区域。 2、图像分割是怎么实现的 图像分割算法基于像素值的不连…

Java微服务学习Day1

文章目录 认识微服务服务拆分及远程调用服务拆分服务远程调用提供者与消费者 Eureka注册中心介绍构建EurekaServer注册user-serviceorder-service完成服务拉取 Ribbon负载均衡介绍原理策略饥饿加载 Nacos注册中心介绍配置分级存储负载均衡环境隔离nacos注册中心原理 认识微服务…

《剑指 Offer》专项突破版 - 面试题 30 和 31:详解如何设计哈希表以及利用哈希表设计更加高级、复杂的数据结构

目录 一、哈希表的基础知识 二、哈希表的设计 2.1 - 插入、删除和随机访问都是 O(1) 的容器 2.2 - 最近最少使用缓存 一、哈希表的基础知识 哈希表是一种常见的数据结构&#xff0c;在解决算法面试题的时候经常需要用到哈希表。哈希表最大的优点是高效&#xff0c;在哈希表…

java实现算法

一、二分法 二分法查找主要是为了快速查找给定数组内&#xff0c;期待值在数组中的位置&#xff08;下标&#xff09; 二分法查找通过对整个数组取中间值&#xff0c;判断期待值所在的范围并缩小范围&#xff0c;每次查找范围折半&#xff0c;直到范围的边界重合&#xff0c;…

终端命令提示符:如何查看我们电脑端口是否被占用和处理方式

文章目录 端口信息查看1、Windows:2、Linux/macOS: 使用 netstat使用 lsof 端口信息查看 在不同的操作系统中&#xff0c;查看端口是否被占用的指令有所不同。以下是一些常见的指令&#xff1a; 1、Windows: 使用命令行工具 netstat 来查看端口占用情况。 电脑键盘按住 win…

第九个知识点:内部对象

Date对象: <script>var date new Date();date.getFullYear();//年date.getMonth();//月date.getDate();//日date.getDay();//星期几date.getHours();//时date.getMinutes();//分date.getSeconds();//秒date.getTime();//获取时间戳&#xff0c;时间戳时全球统一&#x…

[计算机提升] 备份系统:系统映像

6.3 备份系统&#xff1a;系统映像 备份系统和还原系统是一套互补的操作。 操作系统的备份就是将操作系统当前的所有数据复制到硬盘的一个空闲区域&#xff0c;以防止系统崩溃或数据丢失。还原操作则是将先前备份的数据恢复到操作系统中&#xff0c;使系统回到之前的样子&…

Python进程之串行与并行

串行和并行 串行指的是任务的执行方式。串行在执行多个任务时&#xff0c;各个任务按顺序执行&#xff0c;完成一个之后才能进行下一个。&#xff08;早期单核CPU的情况下&#xff09; 并行指的是多个任务在同一时刻可以同时执行&#xff08;前提是多核CPU&#xff09;&#…