为什么gcc为复制std :: vector <>会生成memmove而不是memcpy?

骨质疏松症

在gcc 5.3中,以下示例中的两个函数都会生成对的调用memmove生成amemcpy是否不合适

#include <vector>

int blackhole(const std::vector<int>&);

int copy_vec1(const std::vector<int>& v1) {
    const std::vector<int> v2{v1.begin(), v1.end()};
    return blackhole(v2);
}

int copy_vec2(const std::vector<int>& v1) {
    const auto v2 = v1;
    return blackhole(v2);
}

哥德的例子

基思·汤普森(Keith Thompson)

我尝试使用g ++ 6.1.0编译此代码。我不确定所有细节,但我认为该memmove调用不是由编译器直接生成的;它不是由编译器直接生成的。而是在实现的代码中<vector>

当我使用预处理代码时

/o/apps/gcc-6.1.0/bin/g++ -E -std=c++14 c.cpp

我看到有两个电话打来__builtin_memmove,都来自.../include/c++/6.1.0/bits/stl_algobase.h查看该头文件,我看到以下注释:

// All of these auxiliary structs serve two purposes.  (1) Replace
// calls to copy with memmove whenever possible.  (Memmove, not memcpy,
// because the input and output ranges are permitted to overlap.)
// (2) If we're using random access iterators, then write the loop as
// a for loop with an explicit count.

我认为正在发生的事情是,用于复制向量的代码通常更适用于可以重叠的副本(例如对std::move(?)的调用)。

(我尚未确认memmove出现在程序集列表中的__builtin_memmove调用与中的调用相对应stl_algobase.h。我邀请其他人对此进行跟进。)

根据实现的不同,相对于memmove()可能会有一些开销memcpy(),但是差异很小。为不能重叠的副本创建特殊情况的代码可能不值得。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么gcc为复制std :: vector <>会生成memmove而不是memcpy?

来自分类Dev

为什么std :: array :: size constexpr具有简单类型(int,double,...)而不是std :: vector(GCC)?

来自分类Dev

为什么返回std :: vector仍在复制?

来自分类Dev

为什么std :: string不是std :: vector的特化?

来自分类Dev

为什么std :: vector :: insert需要复制分配?

来自分类Dev

如何grep表示'vector'而不是'std :: vector'

来自分类Dev

为什么我特别应该将std :: span而不是std :: vector&传递给函数?

来自分类Dev

为什么std :: vector是连续的?

来自分类常见问题

为什么vector <bool>不是STL容器?

来自分类Dev

为什么gcc和clang为std :: find生成了这么多代码?

来自分类Dev

为什么std :: vector :: insert复杂度是线性的(而不是恒定的)?

来自分类Dev

为什么std :: vector要求is_trivial进行按位移动,而不是is_trivially_copyable?

来自分类Dev

为什么要使用新运算符而不是std :: vector?

来自分类Dev

来自对象std :: vector的memcpy基础数据

来自分类Dev

Memcpy数据直接进入std :: vector

来自分类Dev

为什么gcc会生成“回音-8240(%ebp),%eax; cmpl $ 1,%eax; ja XXX”而不是jmp指令?

来自分类Dev

为什么在对std :: vector :: emplace_back()的调用中调用了复制构造函数?

来自分类Dev

为什么std :: vector构造函数通过复制接受初始化列表?

来自分类Dev

为什么GCC不能假设std :: vector :: size在此循环中不会改变?

来自分类Dev

std::string 不是 std::vector<char> 吗?

来自分类Dev

为什么会生成副本?

来自分类常见问题

为什么libc ++的vector <bool> :: const_reference不是bool?

来自分类Dev

为什么ByteString不是Vector Word8?

来自分类Dev

为什么std :: endl会生成此神秘错误消息?

来自分类Dev

为什么会生成更新的文件而不是正确移动重复文件?

来自分类Dev

为什么会生成较新的文件,而不是正确移动重复文件?

来自分类Dev

为什么不能在C ++中为std :: vector的字符串文字初始值设定项列表创建std :: vector <std :: string>?

来自分类Dev

为什么比std :: deque更喜欢std :: vector?

来自分类Dev

为什么用“ std :: vector <std :: string>&”代替“ void”?

Related 相关文章

  1. 1

    为什么gcc为复制std :: vector <>会生成memmove而不是memcpy?

  2. 2

    为什么std :: array :: size constexpr具有简单类型(int,double,...)而不是std :: vector(GCC)?

  3. 3

    为什么返回std :: vector仍在复制?

  4. 4

    为什么std :: string不是std :: vector的特化?

  5. 5

    为什么std :: vector :: insert需要复制分配?

  6. 6

    如何grep表示'vector'而不是'std :: vector'

  7. 7

    为什么我特别应该将std :: span而不是std :: vector&传递给函数?

  8. 8

    为什么std :: vector是连续的?

  9. 9

    为什么vector <bool>不是STL容器?

  10. 10

    为什么gcc和clang为std :: find生成了这么多代码?

  11. 11

    为什么std :: vector :: insert复杂度是线性的(而不是恒定的)?

  12. 12

    为什么std :: vector要求is_trivial进行按位移动,而不是is_trivially_copyable?

  13. 13

    为什么要使用新运算符而不是std :: vector?

  14. 14

    来自对象std :: vector的memcpy基础数据

  15. 15

    Memcpy数据直接进入std :: vector

  16. 16

    为什么gcc会生成“回音-8240(%ebp),%eax; cmpl $ 1,%eax; ja XXX”而不是jmp指令?

  17. 17

    为什么在对std :: vector :: emplace_back()的调用中调用了复制构造函数?

  18. 18

    为什么std :: vector构造函数通过复制接受初始化列表?

  19. 19

    为什么GCC不能假设std :: vector :: size在此循环中不会改变?

  20. 20

    std::string 不是 std::vector<char> 吗?

  21. 21

    为什么会生成副本?

  22. 22

    为什么libc ++的vector <bool> :: const_reference不是bool?

  23. 23

    为什么ByteString不是Vector Word8?

  24. 24

    为什么std :: endl会生成此神秘错误消息?

  25. 25

    为什么会生成更新的文件而不是正确移动重复文件?

  26. 26

    为什么会生成较新的文件,而不是正确移动重复文件?

  27. 27

    为什么不能在C ++中为std :: vector的字符串文字初始值设定项列表创建std :: vector <std :: string>?

  28. 28

    为什么比std :: deque更喜欢std :: vector?

  29. 29

    为什么用“ std :: vector <std :: string>&”代替“ void”?

热门标签

归档