Skip to content
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

autovector: performance improves #10230

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
46 changes: 30 additions & 16 deletions util/autovector.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class autovector : public std::vector<T> {
}
};
#else

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
#endif

// A vector that leverages pre-allocated stack-based array to achieve better
// performance for array with small amount of items.
//
Expand Down Expand Up @@ -181,10 +187,9 @@ class autovector {
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

autovector() : values_(reinterpret_cast<pointer>(buf_)) {}
autovector() {}

autovector(std::initializer_list<T> init_list)
: values_(reinterpret_cast<pointer>(buf_)) {
autovector(std::initializer_list<T> init_list) {
for (const T& item : init_list) {
push_back(item);
}
Expand All @@ -207,13 +212,15 @@ class autovector {
if (n > kSize) {
vect_.resize(n - kSize);
while (num_stack_items_ < kSize) {
new ((void*)(&values_[num_stack_items_++])) value_type();
new ((void*)(&values_[num_stack_items_])) value_type();
num_stack_items_++; // exception-safe: inc after cons finish
}
num_stack_items_ = kSize;
} else {
vect_.clear();
while (num_stack_items_ < n) {
new ((void*)(&values_[num_stack_items_++])) value_type();
new ((void*)(&values_[num_stack_items_])) value_type();
num_stack_items_++; // exception-safe: inc after cons finish
}
while (num_stack_items_ > n) {
values_[--num_stack_items_].~value_type();
Expand Down Expand Up @@ -356,42 +363,49 @@ class autovector {
}

private:
size_type num_stack_items_ = 0; // current number of items
alignas(alignof(
value_type)) char buf_[kSize *
sizeof(value_type)]; // the first `kSize` items
pointer values_;
static void destory(value_type* p, size_t n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be renamed to destroy (typo in previous method name).

Suggested change
static void destory(value_type* p, size_t n) {
static void destroy(value_type* p, size_t n) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks for your correction!

if (!std::is_trivially_destructible<value_type>::value) {
while (n) p[--n].~value_type();
}
}

// used only if there are more than `kSize` items.
std::vector<T> vect_;
size_type num_stack_items_ = 0; // current number of items
union {
value_type values_[kSize];
};
};

template <class T, size_t kSize>
autovector<T, kSize>& autovector<T, kSize>::assign(
const autovector<T, kSize>& other) {
values_ = reinterpret_cast<pointer>(buf_);
// copy the internal vector
vect_.assign(other.vect_.begin(), other.vect_.end());

destory(values_, num_stack_items_);
// copy array
num_stack_items_ = other.num_stack_items_;
std::copy(other.values_, other.values_ + num_stack_items_, values_);
std::uninitialized_copy_n(other.values_, num_stack_items_, values_);

return *this;
}

template <class T, size_t kSize>
autovector<T, kSize>& autovector<T, kSize>::operator=(
autovector<T, kSize>&& other) {
values_ = reinterpret_cast<pointer>(buf_);
vect_ = std::move(other.vect_);
destory(values_, num_stack_items_);
size_t n = other.num_stack_items_;
num_stack_items_ = n;
other.num_stack_items_ = 0;
for (size_t i = 0; i < n; ++i) {
values_[i] = std::move(other.values_[i]);
}
std::uninitialized_move_n(other.values_, n, values_);
return *this;
}

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

#endif // ROCKSDB_LITE
} // namespace ROCKSDB_NAMESPACE