Commit 7139fa7a authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook GitHub Bot

Optimize skipTo() if it is guaranteed to make progress

Summary: Add an option to specify that `skipTo()` cannot be called on the current value, so we can save a comparison.

Reviewed By: luciang

Differential Revision: D32161927

fbshipit-source-id: 6ebfe7711a5ee3bcdf40cd8a1097da777f78c277
parent 928aa5de
......@@ -332,6 +332,7 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>,
return setValue(inner);
}
template <bool kCanBeAtValue = true>
bool skipTo(ValueType v) {
// Also works when value_ == kInvalidValue.
if (v != kInvalidValue) {
......@@ -340,7 +341,7 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>,
if (!kUnchecked && v > upperBound_) {
return setDone();
} else if (v == value_) {
} else if (kCanBeAtValue && v == value_) {
return true;
}
......
......@@ -725,12 +725,18 @@ class EliasFanoReader {
* the current position.
* Requires that value >= value() (or that the reader is positioned before the
* first element). Returns false if no such element exists.
* If kCanBeAtValue is false, the requirement above becomes value > value().
*/
template <bool kCanBeAtValue = true>
bool skipTo(ValueType value) {
if (valid()) {
DCHECK_GE(value, value_);
if (UNLIKELY(value == value_)) {
return true;
if constexpr (kCanBeAtValue) {
DCHECK_GE(value, value_);
if (UNLIKELY(value == value_)) {
return true;
}
} else {
DCHECK_GT(value, value_);
}
}
......@@ -753,11 +759,16 @@ class EliasFanoReader {
* Prepare to skip to `value` by prefetching appropriate memory in both the
* upper and lower bits.
*/
template <bool kCanBeAtValue = true>
void prepareSkipTo(ValueType value) const {
if (valid()) {
DCHECK_GE(value, value_);
if (UNLIKELY(value == value_)) {
return;
if constexpr (kCanBeAtValue) {
DCHECK_GE(value, value_);
if (UNLIKELY(value == value_)) {
return;
}
} else {
DCHECK_GT(value, value_);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment