-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc++] Introduce __product_iterator_traits
and optimise flat_map::insert
#139454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||||||||
//===----------------------------------------------------------------------===// | ||||||||||
// | ||||||||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||
// See https://llvm.org/LICENSE.txt for license information. | ||||||||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||
// | ||||||||||
//===----------------------------------------------------------------------===// | ||||||||||
|
||||||||||
#ifndef _LIBCPP___PRODUCT_ITERATOR_H | ||||||||||
#define _LIBCPP___PRODUCT_ITERATOR_H | ||||||||||
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
// Product iterators are iterators that contain two or more underlying iterators. | ||||||||||
// | ||||||||||
// For example, std::flat_map stores its data into two separate containers, and its iterator | ||||||||||
// is a proxy over two separate underlying iterators. The concept of product iterators | ||||||||||
// allows algorithms to operate over these underlying iterators separately, opening the | ||||||||||
// door to various optimizations. | ||||||||||
// | ||||||||||
// If __product_iterator_traits can be instantiated, the following functions and associated types must be provided: | ||||||||||
// - static constexpr size_t Traits::__size | ||||||||||
// The number of underlying iterators inside the product iterator. | ||||||||||
// | ||||||||||
// - template <size_t _N> | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed just now, would it be useful to have a function like |
||||||||||
// static auto Traits::__get_iterator_element(It __it) | ||||||||||
// Returns the _Nth iterator element of the given product iterator. | ||||||||||
|
||||||||||
#include <__config> | ||||||||||
#include <__cstddef/size_t.h> | ||||||||||
#include <__type_traits/enable_if.h> | ||||||||||
#include <__type_traits/integral_constant.h> | ||||||||||
#include <__utility/declval.h> | ||||||||||
|
||||||||||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||||||||||
# pragma GCC system_header | ||||||||||
#endif | ||||||||||
|
||||||||||
_LIBCPP_BEGIN_NAMESPACE_STD | ||||||||||
|
||||||||||
template <class _Iterator> | ||||||||||
struct __product_iterator_traits; | ||||||||||
/* exposition-only: | ||||||||||
{ | ||||||||||
static constexpr size_t __size = ...; | ||||||||||
template <size_t _N> | ||||||||||
static auto __get_iterator_element(_Iterator); | ||||||||||
}; | ||||||||||
*/ | ||||||||||
|
||||||||||
template <class _Tp, size_t = 0> | ||||||||||
struct __is_product_iterator : false_type {}; | ||||||||||
|
||||||||||
template <class _Tp> | ||||||||||
struct __is_product_iterator<_Tp, sizeof(__product_iterator_traits<_Tp>) * 0> : true_type {}; | ||||||||||
|
||||||||||
template <class _Tp, size_t _Size, class = void> | ||||||||||
struct __is_product_iterator_of_size : false_type {}; | ||||||||||
|
||||||||||
template <class _Tp, size_t _Size> | ||||||||||
struct __is_product_iterator_of_size<_Tp, _Size, __enable_if_t<__product_iterator_traits<_Tp>::__size == _Size> > | ||||||||||
: true_type {}; | ||||||||||
|
||||||||||
template <class _Iterator, size_t _Nth> | ||||||||||
using __product_iterator_element_t _LIBCPP_NODEBUG = | ||||||||||
decltype(__product_iterator_traits<_Iterator>::__get_iterator_element<_Nth>(std::declval<_Iterator>())); | ||||||||||
|
||||||||||
_LIBCPP_END_NAMESPACE_STD | ||||||||||
|
||||||||||
#endif // _LIBCPP___PRODUCT_ITERATOR_H | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,6 +23,7 @@ | |||||
#include <__iterator/iter_move.h> | ||||||
#include <__iterator/iter_swap.h> | ||||||
#include <__iterator/iterator_traits.h> | ||||||
#include <__iterator/product_iterator.h> | ||||||
#include <__ranges/access.h> | ||||||
#include <__ranges/all.h> | ||||||
#include <__ranges/concepts.h> | ||||||
|
@@ -251,6 +252,10 @@ class zip_view<_Views...>::__iterator : public __zip_view_iterator_category_base | |||||
|
||||||
friend class zip_view<_Views...>; | ||||||
|
||||||
using __is_zip_view_iterator _LIBCPP_NODEBUG = true_type; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Slight simplification. This is what we do for |
||||||
|
||||||
friend struct __product_iterator_traits<__iterator>; | ||||||
|
||||||
public: | ||||||
using iterator_concept = decltype(ranges::__get_zip_view_iterator_tag<_Const, _Views...>()); | ||||||
using value_type = tuple<range_value_t<__maybe_const<_Const, _Views>>...>; | ||||||
|
@@ -468,6 +473,18 @@ inline constexpr auto zip = __zip::__fn{}; | |||||
} // namespace views | ||||||
} // namespace ranges | ||||||
|
||||||
template <class _Iter> | ||||||
requires _Iter::__is_zip_view_iterator::value | ||||||
struct __product_iterator_traits<_Iter> { | ||||||
static constexpr size_t __size = tuple_size<decltype(std::declval<_Iter>().__current_)>::value; | ||||||
|
||||||
template <size_t _Nth> | ||||||
requires(_Nth < __size) | ||||||
_LIBCPP_HIDE_FROM_ABI static constexpr auto __get_iterator_element(_Iter __it) { | ||||||
return std::get<_Nth>(__it.__current_); | ||||||
} | ||||||
Comment on lines
+483
to
+485
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you intend to be making copies here? You're making a copy of the This should get a test case. |
||||||
}; | ||||||
|
||||||
#endif // _LIBCPP_STD_VER >= 23 | ||||||
|
||||||
_LIBCPP_END_NAMESPACE_STD | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
#include <flat_map> | ||
#include <utility> | ||
#include <ranges> | ||
|
||
#include "associative_container_benchmarks.h" | ||
#include "../../GenerateInput.h" | ||
|
@@ -26,9 +27,54 @@ struct support::adapt_operations<std::flat_map<K, V>> { | |
static auto get_iterator(InsertionResult const& result) { return result.first; } | ||
}; | ||
|
||
void product_iterator_benchmark_flat_map(benchmark::State& state) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest looking at whether this can be moved to |
||
const std::size_t size = state.range(0); | ||
|
||
using M = std::flat_map<int, int>; | ||
|
||
const M source = | ||
std::views::iota(0, static_cast<int>(size)) | std::views::transform([](int i) { return std::pair(i, i); }) | | ||
std::ranges::to<std::flat_map<int, int>>(); | ||
|
||
for (auto _ : state) { | ||
M m; | ||
m.insert(std::sorted_unique, source.begin(), source.end()); | ||
benchmark::DoNotOptimize(m); | ||
benchmark::ClobberMemory(); | ||
} | ||
} | ||
|
||
void product_iterator_benchmark_zip_view(benchmark::State& state) { | ||
const std::size_t size = state.range(0); | ||
|
||
using M = std::flat_map<int, int>; | ||
|
||
const std::vector<int> keys = std::views::iota(0, static_cast<int>(size)) | std::ranges::to<std::vector<int>>(); | ||
const std::vector<int> values = keys; | ||
|
||
auto source = std::views::zip(keys, values); | ||
for (auto _ : state) { | ||
M m; | ||
m.insert(std::sorted_unique, source.begin(), source.end()); | ||
benchmark::DoNotOptimize(m); | ||
benchmark::ClobberMemory(); | ||
} | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
support::associative_container_benchmarks<std::flat_map<int, int>>("std::flat_map<int, int>"); | ||
|
||
benchmark::RegisterBenchmark("flat_map::insert_product_iterator_flat_map", product_iterator_benchmark_flat_map) | ||
->Arg(32) | ||
->Arg(1024) | ||
->Arg(8192) | ||
->Arg(65536); | ||
benchmark::RegisterBenchmark("flat_map::insert_product_iterator_zip", product_iterator_benchmark_zip_view) | ||
->Arg(32) | ||
->Arg(1024) | ||
->Arg(8192) | ||
->Arg(65536); | ||
|
||
benchmark::Initialize(&argc, argv); | ||
benchmark::RunSpecifiedBenchmarks(); | ||
benchmark::Shutdown(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TODO
above can now go away.