forked from ad-freiburg/qlever
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better move semantics for
IdTable
s (ad-freiburg#1110)
When an `IdTable` is moved from, it now becomes a valid `IdTable` with the same number of columns as before the move and zero rows. So far, it became an invalid `IdTable` with no columns. This simplifies the code in various places. In particular, we can now simple call `clear()` on an `IdTable` in order to empty it. This comes with a small overhead, namely for allocating space for `k` new empty column vectors after the move, where `k` is the number of columns before the move. However, this should not matter in practice because we handle only few `IdTable`s, which are either small (in which case everything is very fast anyway) or they have a large number of rows (in which case the cost for creating `k` empty colums is negligible). Should be performance overhead really matter at some point, we could implement a `small_vector` type, that allocates storage for the columns on the stack when the number of columns is below a certain threshold.
- Loading branch information
Showing
10 changed files
with
239 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023. University of Freiburg, | ||
// Chair of Algorithms and Data Structures. | ||
// Author: Johannes Kalmbach <[email protected]> | ||
|
||
#ifndef QLEVER_VECTORWITHELEMENTWISEMOVE_H | ||
#define QLEVER_VECTORWITHELEMENTWISEMOVE_H | ||
|
||
#include <vector> | ||
|
||
#include "util/ExceptionHandling.h" | ||
|
||
namespace columnBasedIdTable::detail { | ||
// A class that inherits from a `vector<T>`. This class changes the move | ||
// operators of the underlying `vector` as follows: Instead of moving the vector | ||
// as a whole, only the individual elements are moved. This is used for the | ||
// column-based `IdTables` where we move the individual columns, but still want | ||
// a table that was moved from to have the same number of columns as before, but | ||
// with the columns now being empty. | ||
|
||
// Note: This class is an implementation detail of the column-based ID | ||
// tables. Its semantics are very particular, so we don't expect it to have a | ||
// use case outside the `IdTable` module. | ||
template <typename T> | ||
struct VectorWithElementwiseMove : public std::vector<T> { | ||
using Base = std::vector<T>; | ||
using Base::Base; | ||
// Defaulted copy operations, inherited from the base class. | ||
VectorWithElementwiseMove(const VectorWithElementwiseMove&) = default; | ||
VectorWithElementwiseMove& operator=(const VectorWithElementwiseMove&) = | ||
default; | ||
|
||
// Move operations with the specified semantics. | ||
VectorWithElementwiseMove(VectorWithElementwiseMove&& other) noexcept { | ||
moveImpl(std::move(other)); | ||
} | ||
VectorWithElementwiseMove& operator=( | ||
VectorWithElementwiseMove&& other) noexcept { | ||
this->clear(); | ||
moveImpl(std::move(other)); | ||
return *this; | ||
} | ||
// Nothing changes for the destructors, but we obey the rule of 5. | ||
~VectorWithElementwiseMove() = default; | ||
|
||
private: | ||
// The common implementation of the move semantics, move-insert the elements | ||
// of `other` into `this`. Terminate with a readable error message in the | ||
// unlikely case of `std::bad_alloc`. | ||
void moveImpl(VectorWithElementwiseMove&& other) noexcept { | ||
ad_utility::terminateIfThrows( | ||
[&other, self = this] { | ||
AD_CORRECTNESS_CHECK(self->empty()); | ||
self->insert(self->end(), std::make_move_iterator(other.begin()), | ||
std::make_move_iterator(other.end())); | ||
}, | ||
"Error happened during the move construction or move assignment of an " | ||
"IdTable"); | ||
} | ||
}; | ||
} // namespace columnBasedIdTable::detail | ||
|
||
#endif // QLEVER_VECTORWITHELEMENTWISEMOVE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.