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>, ...@@ -332,6 +332,7 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>,
return setValue(inner); return setValue(inner);
} }
template <bool kCanBeAtValue = true>
bool skipTo(ValueType v) { bool skipTo(ValueType v) {
// Also works when value_ == kInvalidValue. // Also works when value_ == kInvalidValue.
if (v != kInvalidValue) { if (v != kInvalidValue) {
...@@ -340,7 +341,7 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>, ...@@ -340,7 +341,7 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>,
if (!kUnchecked && v > upperBound_) { if (!kUnchecked && v > upperBound_) {
return setDone(); return setDone();
} else if (v == value_) { } else if (kCanBeAtValue && v == value_) {
return true; return true;
} }
......
...@@ -725,12 +725,18 @@ class EliasFanoReader { ...@@ -725,12 +725,18 @@ class EliasFanoReader {
* the current position. * the current position.
* Requires that value >= value() (or that the reader is positioned before the * Requires that value >= value() (or that the reader is positioned before the
* first element). Returns false if no such element exists. * 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) { bool skipTo(ValueType value) {
if (valid()) { if (valid()) {
DCHECK_GE(value, value_); if constexpr (kCanBeAtValue) {
if (UNLIKELY(value == value_)) { DCHECK_GE(value, value_);
return true; if (UNLIKELY(value == value_)) {
return true;
}
} else {
DCHECK_GT(value, value_);
} }
} }
...@@ -753,11 +759,16 @@ class EliasFanoReader { ...@@ -753,11 +759,16 @@ class EliasFanoReader {
* Prepare to skip to `value` by prefetching appropriate memory in both the * Prepare to skip to `value` by prefetching appropriate memory in both the
* upper and lower bits. * upper and lower bits.
*/ */
template <bool kCanBeAtValue = true>
void prepareSkipTo(ValueType value) const { void prepareSkipTo(ValueType value) const {
if (valid()) { if (valid()) {
DCHECK_GE(value, value_); if constexpr (kCanBeAtValue) {
if (UNLIKELY(value == value_)) { DCHECK_GE(value, value_);
return; 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