[all-commits] [llvm/llvm-project] 005916: [libc++][ranges]Refactor `copy{, _backward}` and `m...

Konstantin Varlamov via All-commits all-commits at lists.llvm.org
Sat Oct 1 17:35:54 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 005916de58f73aa5c4264c084ba7b0e21040d88f
      https://github.com/llvm/llvm-project/commit/005916de58f73aa5c4264c084ba7b0e21040d88f
  Author: Konstantin Varlamov <varconst at apple.com>
  Date:   2022-10-01 (Sat, 01 Oct 2022)

  Changed paths:
    M libcxx/include/CMakeLists.txt
    M libcxx/include/__algorithm/copy.h
    M libcxx/include/__algorithm/copy_backward.h
    A libcxx/include/__algorithm/copy_move_common.h
    M libcxx/include/__algorithm/move.h
    M libcxx/include/__algorithm/move_backward.h
    M libcxx/include/__algorithm/ranges_copy.h
    M libcxx/include/__algorithm/ranges_copy_backward.h
    M libcxx/include/__algorithm/ranges_copy_n.h
    M libcxx/include/__algorithm/ranges_move.h
    M libcxx/include/__algorithm/ranges_move_backward.h
    M libcxx/include/__algorithm/ranges_set_difference.h
    M libcxx/include/__algorithm/ranges_set_symmetric_difference.h
    M libcxx/include/__algorithm/ranges_set_union.h
    M libcxx/include/__algorithm/rotate.h
    M libcxx/include/__algorithm/set_difference.h
    M libcxx/include/__algorithm/set_symmetric_difference.h
    M libcxx/include/__algorithm/set_union.h
    M libcxx/include/__iterator/reverse_iterator.h
    M libcxx/include/algorithm
    M libcxx/include/module.modulemap.in
    M libcxx/include/valarray
    R libcxx/test/libcxx/algorithms/alg.modifying.operations/copy.pass.cpp
    A libcxx/test/libcxx/algorithms/alg.modifying.operations/copy_move_nontrivial.pass.cpp
    A libcxx/test/libcxx/algorithms/alg.modifying.operations/copy_move_trivial.pass.cpp
    M libcxx/test/libcxx/private_headers.verify.cpp
    M libcxx/test/libcxx/transitive_includes/cxx2b.csv
    M libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy.pass.cpp
    M libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/ranges.copy_backward.pass.cpp
    M libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move.pass.cpp
    M libcxx/test/std/algorithms/alg.modifying.operations/alg.move/ranges.move_backward.pass.cpp

  Log Message:
  -----------
  [libc++][ranges]Refactor `copy{,_backward}` and `move{,_backward}`

Instead of using `reverse_iterator`, share the optimization between the 4 algorithms. The key observation here that `memmove` applies to both `copy` and `move` identically, and to their `_backward` versions very similarly. All algorithms now follow the same pattern along the lines of:
```
if constexpr (can_memmove<InIter, OutIter>) {
  memmove(first, last, out);
} else {
  naive_implementation(first, last, out);
}
```
A follow-up will delete `unconstrained_reverse_iterator`.

This patch removes duplication and divergence between `std::copy`, `std::move` and `std::move_backward`. It also improves testing:
- the test for whether the optimization is used only applied to `std::copy` and, more importantly, was essentially a no-op because it would still pass if the optimization was not used;
- there were no tests to make sure the optimization is not used when the effect would be visible.

Differential Revision: https://reviews.llvm.org/D130695




More information about the All-commits mailing list