[all-commits] [llvm/llvm-project] 056153: Optimize vector::assign for InputIterator-only pai...

Peng Liu via All-commits all-commits at lists.llvm.org
Thu Nov 28 11:53:20 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 056153f36eca184f81969f5cd5c8cd967c935f96
      https://github.com/llvm/llvm-project/commit/056153f36eca184f81969f5cd5c8cd967c935f96
  Author: Peng Liu <winner245 at hotmail.com>
  Date:   2024-11-28 (Thu, 28 Nov 2024)

  Changed paths:
    M libcxx/docs/ReleaseNotes/20.rst
    M libcxx/include/__vector/vector.h
    M libcxx/test/benchmarks/ContainerBenchmarks.h
    M libcxx/test/benchmarks/GenerateInput.h
    M libcxx/test/benchmarks/vector_operations.bench.cpp

  Log Message:
  -----------
  Optimize vector::assign for InputIterator-only pair inputs (#113852)

This PR optimizes the input iterator overload of `assign(_InputIterator,
_InputIterator)` in `std::vector<_Tp, _Allocator>` by directly assigning
to already initialized memory, rather than first destroying existing
elements and then constructing new ones. By eliminating unnecessary
destruction and construction, the proposed algorithm enhances the
performance by up to 2x for trivial element types (e.g.,
`std::vector<int>`), up to 2.6x for non-trivial element types like
`std::vector<std::string>`, and up to 3.4x for more complex non-trivial
types (e.g., `std::vector<std::vector<int>>`).

###  Google Benchmarks

Benchmark tests (`libcxx/test/benchmarks/vector_operations.bench.cpp`)
were conducted for the `assign()` implementations before and after this
patch. The tests focused on trivial element types like
`std::vector<int>`, and non-trivial element types such as
`std::vector<std::string>` and `std::vector<std::vector<int>>`.



#### Before
```
-------------------------------------------------------------------------------------------------
Benchmark                                                       Time             CPU   Iterations
-------------------------------------------------------------------------------------------------
BM_AssignInputIterIter/vector_int/1024/1024                  1157 ns         1169 ns       608188
BM_AssignInputIterIter<32>/vector_string/1024/1024          14559 ns        14710 ns        47277
BM_AssignInputIterIter<32>/vector_vector_int/1024/1024      26846 ns        27129 ns        25925
```


#### After
```
-------------------------------------------------------------------------------------------------
Benchmark                                                       Time             CPU   Iterations
-------------------------------------------------------------------------------------------------
BM_AssignInputIterIter/vector_int/1024/1024                   561 ns          566 ns      1242251
BM_AssignInputIterIter<32>/vector_string/1024/1024           5604 ns         5664 ns       128365
BM_AssignInputIterIter<32>/vector_vector_int/1024/1024       7927 ns         8012 ns        88579
```



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list