Commit 6f97a09f authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook Github Bot

Don't read the first word on construction in EliasFanoReader and BitVectorReader

Summary: In most cases lists are not iterated from the beginning, so reading the first word wastes a memory access.

Reviewed By: philippv

Differential Revision: D18140137

fbshipit-source-id: d5cd07a8b9ed67febcc89cef0314a538e5ca8423
parent 0a395466
...@@ -264,9 +264,10 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>, ...@@ -264,9 +264,10 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>,
} }
void reset() { void reset() {
block_ = (bits_ != nullptr) ? folly::loadUnaligned<uint64_t>(bits_) : 0; // Pretend the bitvector is prefixed by a block of zeroes.
outer_ = 0; block_ = 0;
position_ = -1; position_ = static_cast<SizeType>(-1);
outer_ = static_cast<SizeType>(-sizeof(uint64_t));
value_ = kInvalidValue; value_ = kInvalidValue;
} }
...@@ -360,15 +361,15 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>, ...@@ -360,15 +361,15 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>,
} }
// Find the value. // Find the value.
size_t outer = v / 64 * 8; size_t outer = v / 64 * sizeof(uint64_t);
while (outer_ < outer) { while (outer_ != outer) {
position_ += Instructions::popcount(block_); position_ += Instructions::popcount(block_);
outer_ += sizeof(uint64_t); outer_ += sizeof(uint64_t);
block_ = folly::loadUnaligned<uint64_t>(bits_ + outer_); block_ = folly::loadUnaligned<uint64_t>(bits_ + outer_);
DCHECK_LE(outer_, outer);
} }
DCHECK_EQ(outer_, outer);
uint64_t mask = ~((uint64_t(1) << (v % 64)) - 1); uint64_t mask = ~((uint64_t(1) << (v % 64)) - 1);
position_ += Instructions::popcount(block_ & ~mask) + 1; position_ += Instructions::popcount(block_ & ~mask) + 1;
block_ &= mask; block_ &= mask;
...@@ -418,9 +419,8 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>, ...@@ -418,9 +419,8 @@ class BitVectorReader : detail::ForwardPointers<Encoder::forwardQuantum>,
} }
private: private:
constexpr static ValueType kInvalidValue = // Must hold kInvalidValue + 1 == 0.
std::numeric_limits<ValueType>::max(); // Must hold kInvalidValue + 1 == constexpr static ValueType kInvalidValue = -1;
// 0.
bool setValue(size_t inner) { bool setValue(size_t inner) {
value_ = static_cast<ValueType>(8 * outer_ + inner); value_ = static_cast<ValueType>(8 * outer_ + inner);
......
...@@ -360,9 +360,10 @@ class UpperBitsReader : ForwardPointers<Encoder::forwardQuantum>, ...@@ -360,9 +360,10 @@ class UpperBitsReader : ForwardPointers<Encoder::forwardQuantum>,
} }
void reset() { void reset() {
block_ = start_ != nullptr ? folly::loadUnaligned<block_t>(start_) : 0; // Pretend the bitvector is prefixed by a block of zeroes.
position_ = std::numeric_limits<SizeType>::max(); block_ = 0;
outer_ = 0; position_ = static_cast<SizeType>(-1);
outer_ = static_cast<OuterType>(-sizeof(block_t));
value_ = 0; value_ = 0;
} }
...@@ -754,8 +755,7 @@ class EliasFanoReader { ...@@ -754,8 +755,7 @@ class EliasFanoReader {
private: private:
// Must hold kInvalidValue + 1 == 0. // Must hold kInvalidValue + 1 == 0.
constexpr static ValueType kInvalidValue = constexpr static ValueType kInvalidValue = -1;
std::numeric_limits<ValueType>::max();
bool setDone() { bool setDone() {
value_ = kInvalidValue; value_ = kInvalidValue;
......
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