Skip to content

Commit

Permalink
add reserve() call when inserting lists of values with random access …
Browse files Browse the repository at this point in the history
…iterators.
  • Loading branch information
greg7mdp committed Jul 28, 2020
1 parent c818e86 commit 0c6e8c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/matt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void test(const char *name, Perturb perturb1, Perturb perturb2)

Timer t(name); // start timer
Set c;
c.reserve(order.size()); // whether this "reserve()" is present or not makes a huge difference
//c.reserve(order.size()); // whether this "reserve()" is present or not makes a huge difference
c.insert(order.begin(), order.end()); // time for inserting the same keys into the set
// should not depend on them being sorted or not.
}
Expand Down
41 changes: 33 additions & 8 deletions parallel_hashmap/phmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1209,9 +1209,8 @@ class raw_hash_set
// TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
// RequiresInsertable<T> with RequiresInsertable<const T&>.
// We are hitting this bug: https://godbolt.org/g/1Vht4f.
template <
class T, RequiresInsertable<T> = 0,
typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
template <class T, RequiresInsertable<T> = 0,
typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
std::pair<iterator, bool> insert(const T& value) {
return emplace(value);
}
Expand All @@ -1235,9 +1234,8 @@ class raw_hash_set
// TODO(romanp): Once we stop supporting gcc 5.1 and below, replace
// RequiresInsertable<T> with RequiresInsertable<const T&>.
// We are hitting this bug: https://godbolt.org/g/1Vht4f.
template <
class T, RequiresInsertable<T> = 0,
typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
template <class T, RequiresInsertable<T> = 0,
typename std::enable_if<IsDecomposable<const T&>::value, int>::type = 0>
iterator insert(const_iterator, const T& value) {
return insert(value).first;
}
Expand All @@ -1246,9 +1244,36 @@ class raw_hash_set
return insert(std::move(value)).first;
}

template <class InputIt>
template <typename It>
using IsRandomAccess = std::is_same<typename std::iterator_traits<It>::iterator_category,
std::random_access_iterator_tag>;


template<typename T>
struct has_difference_operator
{
private:
using yes = std::true_type;
using no = std::false_type;

template<typename U> static auto test(int) -> decltype(std::declval<U>() - std::declval<U>() == 1, yes());
template<typename> static no test(...);

public:
static constexpr bool value = std::is_same<decltype(test<T>(0)), yes>::value;
};

template <class InputIt, typename phmap::enable_if_t<has_difference_operator<InputIt>::value, int> = 0>
void insert(InputIt first, InputIt last) {
for (; first != last; ++first) insert(*first);
this->reserve(this->size() + (last - first));
for (; first != last; ++first)
insert(*first);
}

template <class InputIt, typename phmap::enable_if_t<!has_difference_operator<InputIt>::value, int> = 0>
void insert(InputIt first, InputIt last) {
for (; first != last; ++first)
insert(*first);
}

template <class T, RequiresNotInit<T> = 0, RequiresInsertable<const T&> = 0>
Expand Down

0 comments on commit 0c6e8c3

Please sign in to comment.