From e440a7c52f23e968d5767f8d04c08ec830efb4ed Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 6 Feb 2018 21:45:58 -0800 Subject: [PATCH] Convert some copy constructors to move constructors in query.cc --- src/query.cc | 14 +++++++------- src/serializer.h | 6 +++--- src/utils.h | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/query.cc b/src/query.cc index 6b4d4b63..efb1ba05 100644 --- a/src/query.cc +++ b/src/query.cc @@ -100,7 +100,7 @@ optional ToQuery(const IdMap& id_map, const IndexVar::Def& var) { template void AddMergeableRange( std::vector>* dest, - const std::vector>& source) { + std::vector>&& source) { // TODO: Consider caching the lookup table. It can probably save even more // time at the cost of some additional memory. @@ -111,13 +111,13 @@ void AddMergeableRange( id_to_index[(*dest)[i].id] = i; // Add entries. Try to add them to an existing entry. - for (const auto& entry : source) { + for (auto& entry : source) { auto it = id_to_index.find(entry.id); if (it != id_to_index.end()) { - AddRange(&(*dest)[it->second].to_add, entry.to_add); - AddRange(&(*dest)[it->second].to_remove, entry.to_remove); + AddRange(&(*dest)[it->second].to_add, std::move(entry.to_add)); + AddRange(&(*dest)[it->second].to_remove, std::move(entry.to_remove)); } else { - dest->push_back(entry); + dest->push_back(std::move(entry)); } } } @@ -664,8 +664,8 @@ IndexUpdate::IndexUpdate(const IdMap& previous_id_map, // This function runs on an indexer thread. void IndexUpdate::Merge(IndexUpdate&& update) { -#define INDEX_UPDATE_APPEND(name) AddRange(&name, update.name); -#define INDEX_UPDATE_MERGE(name) AddMergeableRange(&name, update.name); +#define INDEX_UPDATE_APPEND(name) AddRange(&name, std::move(update.name)); +#define INDEX_UPDATE_MERGE(name) AddMergeableRange(&name, std::move(update.name)); INDEX_UPDATE_APPEND(files_removed); INDEX_UPDATE_APPEND(files_def_update); diff --git a/src/serializer.h b/src/serializer.h index 714b6c10..3a0a3dcd 100644 --- a/src/serializer.h +++ b/src/serializer.h @@ -185,7 +185,7 @@ void Reflect(Reader& visitor, optional& value) { } T real_value; Reflect(visitor, real_value); - value = real_value; + value = std::move(real_value); } template void Reflect(Writer& visitor, optional& value) { @@ -204,7 +204,7 @@ void Reflect(Reader& visitor, Maybe& value) { } T real_value; Reflect(visitor, real_value); - value = real_value; + value = std::move(real_value); } template void Reflect(Writer& visitor, Maybe& value) { @@ -254,7 +254,7 @@ struct ReflectVariant { ReflectTag(Reader& visitor, std::variant& value) { T a; Reflect(visitor, a); - value = a; + value = std::move(a); } // This SFINAE overload is used to prevent compile error. value = a; is not // allowed if T does not appear in Ts... diff --git a/src/utils.h b/src/utils.h index 5b985ba6..aa27e721 100644 --- a/src/utils.h +++ b/src/utils.h @@ -137,8 +137,8 @@ void AddRange(std::vector* dest, const std::vector& to_add) { template void AddRange(std::vector* dest, std::vector&& to_add) { - dest->push_back(dest->end(), std::make_move_iterator(to_add.begin()), - std::make_move_iterator(to_add.end())); + dest->insert(dest->end(), std::make_move_iterator(to_add.begin()), + std::make_move_iterator(to_add.end())); } template