Skip to content

Commit

Permalink
Templatize MultiCfIteratorImpl to avoid std::function's overhead (#13052
Browse files Browse the repository at this point in the history
)

Summary:
Pull Request resolved: #13052

Currently, `MultiCfIteratorImpl` uses `std::function`s for `reset_func_` and `populate_func_`, which uses type erasure and has a performance overhead. The patch turns `MultiCfIteratorImpl` into a template that takes the two function object types as template parameters, and changes `AttributeGroupIteratorImpl` and `CoalescingIterator` so they pass in function objects of named types (as opposed to lambdas).

Reviewed By: jaykorean

Differential Revision: D63802598

fbshipit-source-id: e202f6d80c9054335e5b2571051a67a9e012c2d0
  • Loading branch information
ltamasi authored and facebook-github-bot committed Oct 3, 2024
1 parent 12960f0 commit 917e98f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
35 changes: 29 additions & 6 deletions db/attribute_group_iterator_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ class AttributeGroupIteratorImpl : public AttributeGroupIterator {
const Comparator* comparator,
const std::vector<ColumnFamilyHandle*>& column_families,
const std::vector<Iterator*>& child_iterators)
: impl_(
comparator, column_families, child_iterators, [this]() { Reset(); },
[this](const autovector<MultiCfIteratorInfo>& items) {
AddToAttributeGroups(items);
}) {}
: impl_(comparator, column_families, child_iterators, ResetFunc(this),
PopulateFunc(this)) {}
~AttributeGroupIteratorImpl() override {}

// No copy allowed
Expand All @@ -46,7 +43,33 @@ class AttributeGroupIteratorImpl : public AttributeGroupIterator {
void Reset() { attribute_groups_.clear(); }

private:
MultiCfIteratorImpl impl_;
class ResetFunc {
public:
explicit ResetFunc(AttributeGroupIteratorImpl* iter) : iter_(iter) {}

void operator()() const {
assert(iter_);
iter_->Reset();
}

private:
AttributeGroupIteratorImpl* iter_;
};

class PopulateFunc {
public:
explicit PopulateFunc(AttributeGroupIteratorImpl* iter) : iter_(iter) {}

void operator()(const autovector<MultiCfIteratorInfo>& items) const {
assert(iter_);
iter_->AddToAttributeGroups(items);
}

private:
AttributeGroupIteratorImpl* iter_;
};

MultiCfIteratorImpl<ResetFunc, PopulateFunc> impl_;
IteratorAttributeGroups attribute_groups_;
void AddToAttributeGroups(const autovector<MultiCfIteratorInfo>& items);
};
Expand Down
35 changes: 29 additions & 6 deletions db/coalescing_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ class CoalescingIterator : public Iterator {
CoalescingIterator(const Comparator* comparator,
const std::vector<ColumnFamilyHandle*>& column_families,
const std::vector<Iterator*>& child_iterators)
: impl_(
comparator, column_families, child_iterators, [this]() { Reset(); },
[this](const autovector<MultiCfIteratorInfo>& items) {
Coalesce(items);
}) {}
: impl_(comparator, column_families, child_iterators, ResetFunc(this),
PopulateFunc(this)) {}
~CoalescingIterator() override {}

// No copy allowed
Expand Down Expand Up @@ -51,7 +48,33 @@ class CoalescingIterator : public Iterator {
}

private:
MultiCfIteratorImpl impl_;
class ResetFunc {
public:
explicit ResetFunc(CoalescingIterator* iter) : iter_(iter) {}

void operator()() const {
assert(iter_);
iter_->Reset();
}

private:
CoalescingIterator* iter_;
};

class PopulateFunc {
public:
explicit PopulateFunc(CoalescingIterator* iter) : iter_(iter) {}

void operator()(const autovector<MultiCfIteratorInfo>& items) const {
assert(iter_);
iter_->Coalesce(items);
}

private:
CoalescingIterator* iter_;
};

MultiCfIteratorImpl<ResetFunc, PopulateFunc> impl_;
Slice value_;
WideColumns wide_columns_;

Expand Down
19 changes: 9 additions & 10 deletions db/multi_cf_iterator_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ struct MultiCfIteratorInfo {
int order;
};

template <typename ResetFunc, typename PopulateFunc>
class MultiCfIteratorImpl {
public:
MultiCfIteratorImpl(
const Comparator* comparator,
const std::vector<ColumnFamilyHandle*>& column_families,
const std::vector<Iterator*>& child_iterators,
std::function<void()> reset_func,
std::function<void(const autovector<MultiCfIteratorInfo>&)> populate_func)
MultiCfIteratorImpl(const Comparator* comparator,
const std::vector<ColumnFamilyHandle*>& column_families,
const std::vector<Iterator*>& child_iterators,
ResetFunc reset_func, PopulateFunc populate_func)
: comparator_(comparator),
heap_(MultiCfMinHeap(
MultiCfHeapItemComparator<std::greater<int>>(comparator_))),
Expand Down Expand Up @@ -136,8 +135,8 @@ class MultiCfIteratorImpl {

MultiCfIterHeap heap_;

std::function<void()> reset_func_;
std::function<void(autovector<MultiCfIteratorInfo>)> populate_func_;
ResetFunc reset_func_;
PopulateFunc populate_func_;

Iterator* current() const {
if (std::holds_alternative<MultiCfMaxHeap>(heap_)) {
Expand All @@ -163,11 +162,11 @@ class MultiCfIteratorImpl {
}

void InitMinHeap() {
heap_.emplace<MultiCfMinHeap>(
heap_.template emplace<MultiCfMinHeap>(
MultiCfHeapItemComparator<std::greater<int>>(comparator_));
}
void InitMaxHeap() {
heap_.emplace<MultiCfMaxHeap>(
heap_.template emplace<MultiCfMaxHeap>(
MultiCfHeapItemComparator<std::less<int>>(comparator_));
}

Expand Down

0 comments on commit 917e98f

Please sign in to comment.