folly copyright 2015 -> copyright 2016
[folly.git] / folly / experimental / EliasFanoCoding.h
index ee4920ab15439bb11451115eb30bb7b44608b930..c586a6d5b1cc895a857e4aac98b28aa6318bd953 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #ifndef FOLLY_EXPERIMENTAL_ELIAS_FANO_CODING_H
 #define FOLLY_EXPERIMENTAL_ELIAS_FANO_CODING_H
 
-#ifndef __GNUC__
-#error EliasFanoCoding.h requires GCC
-#endif
-
-#if !FOLLY_X64
-#error EliasFanoCoding.h requires x86_64
-#endif
-
 #include <cstdlib>
 #include <limits>
 #include <type_traits>
-#include <boost/noncopyable.hpp>
-#include <glog/logging.h>
 
 #include <folly/Bits.h>
-#include <folly/CpuId.h>
 #include <folly/Likely.h>
+#include <folly/Portability.h>
 #include <folly/Range.h>
+#include <folly/experimental/Instructions.h>
 #include <folly/experimental/Select64.h>
+#include <glog/logging.h>
+
+#ifndef __GNUC__
+#error EliasFanoCoding.h requires GCC
+#endif
+
+#if !FOLLY_X64
+#error EliasFanoCoding.h requires x86_64
+#endif
 
 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
 #error EliasFanoCoding.h requires little endianness
 
 namespace folly { namespace compression {
 
-struct EliasFanoCompressedList {
-  EliasFanoCompressedList() { }
+template <class Pointer>
+struct EliasFanoCompressedListBase {
+  EliasFanoCompressedListBase() = default;
 
-  void free() {
-    ::free(const_cast<unsigned char*>(lower.data()));
-    ::free(const_cast<unsigned char*>(upper.data()));
-    ::free(const_cast<unsigned char*>(skipPointers.data()));
-    ::free(const_cast<unsigned char*>(forwardPointers.data()));
+  template <class OtherPointer>
+  EliasFanoCompressedListBase(
+      const EliasFanoCompressedListBase<OtherPointer>& other)
+      : size(other.size),
+        numLowerBits(other.numLowerBits),
+        data(other.data),
+        skipPointers(reinterpret_cast<Pointer>(other.skipPointers)),
+        forwardPointers(reinterpret_cast<Pointer>(other.forwardPointers)),
+        lower(reinterpret_cast<Pointer>(other.lower)),
+        upper(reinterpret_cast<Pointer>(other.upper)) { }
+
+  template <class T = Pointer>
+  auto free() -> decltype(::free(T(nullptr))) {
+    return ::free(data.data());
   }
 
+  size_t upperSize() const { return data.end() - upper; }
+
   size_t size = 0;
   uint8_t numLowerBits = 0;
 
-  // WARNING: EliasFanoCompressedList has no ownership of
-  // lower, upper, skipPointers and forwardPointers.
-  // The 7 bytes following the last byte of lower and upper
-  // sequences should be readable.
-  folly::ByteRange lower;
-  folly::ByteRange upper;
+  // WARNING: EliasFanoCompressedList has no ownership of data. The 7
+  // bytes following the last byte should be readable.
+  folly::Range<Pointer> data;
 
-  folly::ByteRange skipPointers;
-  folly::ByteRange forwardPointers;
+  Pointer skipPointers = nullptr;
+  Pointer forwardPointers = nullptr;
+  Pointer lower = nullptr;
+  Pointer upper = nullptr;
 };
 
+typedef EliasFanoCompressedListBase<const uint8_t*> EliasFanoCompressedList;
+typedef EliasFanoCompressedListBase<uint8_t*> MutableEliasFanoCompressedList;
+
 template <class Value,
           class SkipValue = size_t,
           size_t kSkipQuantum = 0,     // 0 = disabled
           size_t kForwardQuantum = 0>  // 0 = disabled
 struct EliasFanoEncoderV2 {
   static_assert(std::is_integral<Value>::value &&
-                std::is_unsigned<Value>::value,
+                    std::is_unsigned<Value>::value,
                 "Value should be unsigned integral");
 
   typedef EliasFanoCompressedList CompressedList;
+  typedef MutableEliasFanoCompressedList MutableCompressedList;
 
   typedef Value ValueType;
   typedef SkipValue SkipValueType;
+  struct Layout;
 
   static constexpr size_t skipQuantum = kSkipQuantum;
   static constexpr size_t forwardQuantum = kForwardQuantum;
@@ -101,14 +117,14 @@ struct EliasFanoEncoderV2 {
 
   // Requires: input range (begin, end) is sorted (encoding
   // crashes if it's not).
-  // WARNING: encode() mallocates lower, upper, skipPointers
-  // and forwardPointers. As EliasFanoCompressedList has
-  // no ownership of them, you need to call free() explicitly.
+  // WARNING: encode() mallocates EliasFanoCompressedList::data. As
+  // EliasFanoCompressedList has no ownership of it, you need to call
+  // free() explicitly.
   template <class RandomAccessIterator>
-  static EliasFanoCompressedList encode(RandomAccessIterator begin,
-                                        RandomAccessIterator end) {
+  static MutableCompressedList encode(RandomAccessIterator begin,
+                                      RandomAccessIterator end) {
     if (begin == end) {
-      return EliasFanoCompressedList();
+      return MutableCompressedList();
     }
     EliasFanoEncoderV2 encoder(end - begin, *(end - 1));
     for (; begin != end; ++begin) {
@@ -117,86 +133,23 @@ struct EliasFanoEncoderV2 {
     return encoder.finish();
   }
 
-  EliasFanoEncoderV2(size_t size, ValueType upperBound) {
-    if (size == 0) {
-      return;
-    }
-
-    uint8_t numLowerBits = defaultNumLowerBits(upperBound, size);
-
-    // This is detail::writeBits56 limitation.
-    numLowerBits = std::min<uint8_t>(numLowerBits, 56);
-    CHECK_LT(numLowerBits, 8 * sizeof(Value));  // As we shift by numLowerBits.
-
-    // WARNING: Current read/write logic assumes that the 7 bytes
-    // following the last byte of lower and upper sequences are
-    // readable (stored value doesn't matter and won't be changed),
-    // so we allocate additional 7B, but do not include them in size
-    // of returned value.
-
-    // *** Lower bits.
-    const size_t lowerSize = (numLowerBits * size + 7) / 8;
-    if (lowerSize > 0) {  // numLowerBits != 0
-      lower_ = static_cast<unsigned char*>(calloc(lowerSize + 7, 1));
-    }
-
-    // *** Upper bits.
-    // Upper bits are stored using unary delta encoding.
-    // For example, (3 5 5 9) will be encoded as 1000011001000_2.
-    const size_t upperSizeBits =
-      (upperBound >> numLowerBits) +  // Number of 0-bits to be stored.
-      size;                           // 1-bits.
-    const size_t upperSize = (upperSizeBits + 7) / 8;
-    upper_ = static_cast<unsigned char*>(calloc(upperSize + 7, 1));
-
-    // *** Skip pointers.
-    // Store (1-indexed) position of every skipQuantum-th
-    // 0-bit in upper bits sequence.
-    size_t numSkipPointers = 0;
-    /* static */ if (skipQuantum != 0) {
-      CHECK_LT(size, std::numeric_limits<SkipValueType>::max());
-
-      // 8 * upperSize is used here instead of upperSizeBits, as that is
-      // more serialization-friendly way (upperSizeBits isn't known outside of
-      // this function, unlike upperSize; thus numSkipPointers could easily be
-      // deduced from upperSize).
-      numSkipPointers = (8 * upperSize - size) / (skipQuantum ?: 1);
-      skipPointers_ = static_cast<SkipValueType*>(
-          numSkipPointers == 0
-            ? nullptr
-            : calloc(numSkipPointers, sizeof(SkipValueType)));
-    }
-
-    // *** Forward pointers.
-    // Store (1-indexed) position of every forwardQuantum-th
-    // 1-bit in upper bits sequence.
-    size_t numForwardPointers = 0;
-    /* static */ if (forwardQuantum != 0) {
-      CHECK_LT(upperBound >> numLowerBits,
-               std::numeric_limits<SkipValueType>::max());
-
-      // '?: 1' is a workaround for false 'division by zero' compile-time error.
-      numForwardPointers = size / (forwardQuantum ?: 1);
-      forwardPointers_ = static_cast<SkipValueType*>(
-        numForwardPointers == 0
-          ? nullptr
-          : malloc(numForwardPointers * sizeof(SkipValueType)));
-    }
-
-    // *** Result.
-    result_.size = size;
-    result_.numLowerBits = numLowerBits;
-    result_.lower.reset(lower_, lowerSize);
-    result_.upper.reset(upper_, upperSize);
-    result_.skipPointers.reset(
-        reinterpret_cast<unsigned char*>(skipPointers_),
-        numSkipPointers * sizeof(SkipValueType));
-    result_.forwardPointers.reset(
-        reinterpret_cast<unsigned char*>(forwardPointers_),
-        numForwardPointers * sizeof(SkipValueType));
+  explicit EliasFanoEncoderV2(const MutableCompressedList& result)
+      : lower_(result.lower),
+        upper_(result.upper),
+        skipPointers_(reinterpret_cast<SkipValueType*>(
+              result.skipPointers)),
+        forwardPointers_(reinterpret_cast<SkipValueType*>(
+              result.forwardPointers)),
+        result_(result) {
+    memset(result.data.data(), 0, result.data.size());
   }
 
+  EliasFanoEncoderV2(size_t size, ValueType upperBound)
+      : EliasFanoEncoderV2(
+            Layout::fromUpperBoundAndSize(upperBound, size).allocList()) { }
+
   void add(ValueType value) {
+    CHECK_LT(value, std::numeric_limits<ValueType>::max());
     CHECK_GE(value, lastValue_);
 
     const auto numLowerBits = result_.numLowerBits;
@@ -230,7 +183,7 @@ struct EliasFanoEncoderV2 {
     ++size_;
   }
 
-  const EliasFanoCompressedList& finish() const {
+  const MutableCompressedList& finish() const {
     CHECK_EQ(size_, result_.size);
     return result_;
   }
@@ -256,60 +209,124 @@ struct EliasFanoEncoderV2 {
   size_t size_ = 0;
   size_t skipPointersSize_ = 0;
 
-  EliasFanoCompressedList result_;
+  MutableCompressedList result_;
 };
 
-// NOTE: It's recommended to compile EF coding with -msse4.2, starting
-// with Nehalem, Intel CPUs support POPCNT instruction and gcc will emit
-// it for __builtin_popcountll intrinsic.
-// But we provide an alternative way for the client code: it can switch to
-// the appropriate version of EliasFanoReader<> in realtime (client should
-// implement this switching logic itself) by specifying instruction set to
-// use explicitly.
-namespace instructions {
-
-struct Default {
-  static bool supported(const folly::CpuId& cpuId = {}) {
-    return true;
-  }
-  static inline uint64_t popcount(uint64_t value) {
-    return __builtin_popcountll(value);
-  }
-  static inline int ctz(uint64_t value) {
-    DCHECK_GT(value, 0);
-    return __builtin_ctzll(value);
+template <class Value,
+          class SkipValue,
+          size_t kSkipQuantum,
+          size_t kForwardQuantum>
+struct EliasFanoEncoderV2<Value,
+                          SkipValue,
+                          kSkipQuantum,
+                          kForwardQuantum>::Layout {
+  static Layout fromUpperBoundAndSize(size_t upperBound, size_t size) {
+    // numLowerBits can be at most 56 because of detail::writeBits56.
+    const uint8_t numLowerBits = std::min(defaultNumLowerBits(upperBound,
+                                                              size),
+                                          uint8_t(56));
+    // *** Upper bits.
+    // Upper bits are stored using unary delta encoding.
+    // For example, (3 5 5 9) will be encoded as 1000011001000_2.
+    const size_t upperSizeBits =
+      (upperBound >> numLowerBits) +  // Number of 0-bits to be stored.
+      size;                           // 1-bits.
+    const size_t upper = (upperSizeBits + 7) / 8;
+
+    // *** Validity checks.
+    // Shift by numLowerBits must be valid.
+    CHECK_LT(numLowerBits, 8 * sizeof(Value));
+    CHECK_LT(size, std::numeric_limits<SkipValueType>::max());
+    CHECK_LT(upperBound >> numLowerBits,
+             std::numeric_limits<SkipValueType>::max());
+
+    return fromInternalSizes(numLowerBits, upper, size);
   }
-  static inline uint64_t blsr(uint64_t value) {
-    return value & (value - 1);
+
+  static Layout fromInternalSizes(uint8_t numLowerBits,
+                                  size_t upper,
+                                  size_t size) {
+    Layout layout;
+    layout.size = size;
+    layout.numLowerBits = numLowerBits;
+
+    layout.lower = (numLowerBits * size + 7) / 8;
+    layout.upper = upper;
+
+    // *** Skip pointers.
+    // Store (1-indexed) position of every skipQuantum-th
+    // 0-bit in upper bits sequence.
+    /* static */ if (skipQuantum != 0) {
+      // 8 * upper is used here instead of upperSizeBits, as that is
+      // more serialization-friendly way (upperSizeBits doesn't need
+      // to be known by this function, unlike upper).
+
+      // '?: 1' is a workaround for false 'division by zero'
+      // compile-time error.
+      size_t numSkipPointers = (8 * upper - size) / (skipQuantum ?: 1);
+      layout.skipPointers = numSkipPointers * sizeof(SkipValueType);
+    }
+
+    // *** Forward pointers.
+    // Store (1-indexed) position of every forwardQuantum-th
+    // 1-bit in upper bits sequence.
+    /* static */ if (forwardQuantum != 0) {
+      size_t numForwardPointers = size / (forwardQuantum ?: 1);
+      layout.forwardPointers = numForwardPointers * sizeof(SkipValueType);
+    }
+
+    return layout;
   }
-};
 
-struct Nehalem : public Default {
-  static bool supported(const folly::CpuId& cpuId = {}) {
-    return cpuId.popcnt();
+  size_t bytes() const {
+    return lower + upper + skipPointers + forwardPointers;
   }
-  static inline uint64_t popcount(uint64_t value) {
-    // POPCNT is supported starting with Intel Nehalem, AMD K10.
-    uint64_t result;
-    asm ("popcntq %1, %0" : "=r" (result) : "r" (value));
+
+  template <class Range>
+  EliasFanoCompressedListBase<typename Range::iterator>
+  openList(Range& buf) const {
+    EliasFanoCompressedListBase<typename Range::iterator> result;
+    result.size = size;
+    result.numLowerBits = numLowerBits;
+    result.data = buf.subpiece(0, bytes());
+
+    auto advance = [&] (size_t n) {
+      auto begin = buf.data();
+      buf.advance(n);
+      return begin;
+    };
+
+    result.skipPointers = advance(skipPointers);
+    result.forwardPointers = advance(forwardPointers);
+    result.lower = advance(lower);
+    result.upper = advance(upper);
+
     return result;
   }
-};
 
-struct Haswell : public Nehalem {
-  static bool supported(const folly::CpuId& cpuId = {}) {
-    return Nehalem::supported(cpuId) && cpuId.bmi1();
-  }
-  static inline uint64_t blsr(uint64_t value) {
-    // BMI1 is supported starting with Intel Haswell, AMD Piledriver.
-    // BLSR combines two instuctions into one and reduces register pressure.
-    uint64_t result;
-    asm ("blsrq %1, %0" : "=r" (result) : "r" (value));
-    return result;
+  MutableCompressedList allocList() const {
+    uint8_t* buf = nullptr;
+    // WARNING: Current read/write logic assumes that the 7 bytes
+    // following the last byte of lower and upper sequences are
+    // readable (stored value doesn't matter and won't be changed), so
+    // we allocate additional 7 bytes, but do not include them in size
+    // of returned value.
+    if (size > 0) {
+      buf = static_cast<uint8_t*>(malloc(bytes() + 7));
+    }
+    folly::MutableByteRange bufRange(buf, bytes());
+    return openList(bufRange);
   }
-};
 
-}  // namespace instructions
+  size_t size = 0;
+  uint8_t numLowerBits = 0;
+
+  // Sizes in bytes.
+  size_t lower = 0;
+  size_t upper = 0;
+  size_t skipPointers = 0;
+  size_t forwardPointers = 0;
+};
 
 namespace detail {
 
@@ -319,10 +336,10 @@ class UpperBitsReader {
  public:
   typedef typename Encoder::ValueType ValueType;
 
-  explicit UpperBitsReader(const EliasFanoCompressedList& list)
-    : forwardPointers_(list.forwardPointers.data()),
-      skipPointers_(list.skipPointers.data()),
-      start_(list.upper.data()) {
+  explicit UpperBitsReader(const typename Encoder::CompressedList& list)
+      : forwardPointers_(list.forwardPointers),
+        skipPointers_(list.skipPointers),
+        start_(list.upper) {
     reset();
   }
 
@@ -453,6 +470,28 @@ class UpperBitsReader {
     return skipToNext(v);
   }
 
+  ValueType previousValue() const {
+    DCHECK_NE(position(), -1);
+    DCHECK_GT(position(), 0);
+
+    size_t outer = outer_;
+    block_t block = folly::loadUnaligned<block_t>(start_ + outer);
+    block &= (block_t(1) << inner_) - 1;
+
+    while (UNLIKELY(block == 0)) {
+      DCHECK_GE(outer, sizeof(block_t));
+      outer -= sizeof(block_t);
+      block = folly::loadUnaligned<block_t>(start_ + outer);
+    }
+
+    auto inner = 8 * sizeof(block_t) - 1 - Instructions::clz(block);
+    return static_cast<ValueType>(8 * outer + inner - (position_ - 1));
+  }
+
+  void setDone(size_t endPos) {
+    position_ = endPos;
+  }
+
  private:
   ValueType setValue() {
     value_ = static_cast<ValueType>(8 * outer_ + inner_ - position_);
@@ -465,7 +504,7 @@ class UpperBitsReader {
     block_ &= ~((block_t(1) << (dest % 8)) - 1);
   }
 
-  typedef unsigned long long block_t;
+  typedef uint64_t block_t;
   const unsigned char* const forwardPointers_;
   const unsigned char* const skipPointers_;
   const unsigned char* const start_;
@@ -478,60 +517,65 @@ class UpperBitsReader {
 
 }  // namespace detail
 
+// If kUnchecked = true the caller must guarantee that all the
+// operations return valid elements, i.e., they would never return
+// false if checked.
 template <class Encoder,
-          class Instructions = instructions::Default>
-class EliasFanoReader : private boost::noncopyable {
+          class Instructions = instructions::Default,
+          bool kUnchecked = false>
+class EliasFanoReader {
  public:
   typedef Encoder EncoderType;
   typedef typename Encoder::ValueType ValueType;
 
-  explicit EliasFanoReader(const EliasFanoCompressedList& list)
-    : list_(list),
-      lowerMask_((ValueType(1) << list_.numLowerBits) - 1),
-      upper_(list_) {
+  explicit EliasFanoReader(const typename Encoder::CompressedList& list)
+      : size_(list.size),
+        lower_(list.lower),
+        upper_(list),
+        lowerMask_((ValueType(1) << list.numLowerBits) - 1),
+        numLowerBits_(list.numLowerBits) {
     DCHECK(Instructions::supported());
     // To avoid extra branching during skipTo() while reading
     // upper sequence we need to know the last element.
-    if (UNLIKELY(list_.size == 0)) {
+    // If kUnchecked == true, we do not check that skipTo() is called
+    // within the bounds, so we can avoid initializing lastValue_.
+    if (kUnchecked || UNLIKELY(list.size == 0)) {
       lastValue_ = 0;
       return;
     }
-    ValueType lastUpperValue = 8 * list_.upper.size() - list_.size;
-    auto it = list_.upper.end() - 1;
+    ValueType lastUpperValue = 8 * list.upperSize() - size_;
+    auto it = list.upper + list.upperSize() - 1;
     DCHECK_NE(*it, 0);
     lastUpperValue -= 8 - folly::findLastSet(*it);
-    lastValue_ = readLowerPart(list_.size - 1) |
-                 (lastUpperValue << list_.numLowerBits);
+    lastValue_ = readLowerPart(size_ - 1) | (lastUpperValue << numLowerBits_);
   }
 
   void reset() {
     upper_.reset();
-    progress_ = 0;
-    value_ = 0;
+    value_ = kInvalidValue;
   }
 
   bool next() {
-    if (UNLIKELY(progress_ >= list_.size)) {
+    if (!kUnchecked && UNLIKELY(position() + 1 >= size_)) {
       return setDone();
     }
-    value_ = readLowerPart(progress_) |
-             (upper_.next() << list_.numLowerBits);
-    ++progress_;
+    upper_.next();
+    value_ = readLowerPart(upper_.position()) |
+             (upper_.value() << numLowerBits_);
     return true;
   }
 
   bool skip(size_t n) {
     CHECK_GT(n, 0);
 
-    progress_ += n;
-    if (LIKELY(progress_ <= list_.size)) {
+    if (kUnchecked || LIKELY(position() + n < size_)) {
       if (LIKELY(n < kLinearScanThreshold)) {
         for (size_t i = 0; i < n; ++i) upper_.next();
       } else {
         upper_.skip(n);
       }
-      value_ = readLowerPart(progress_ - 1) |
-        (upper_.value() << list_.numLowerBits);
+      value_ = readLowerPart(upper_.position()) |
+        (upper_.value() << numLowerBits_);
       return true;
     }
 
@@ -539,14 +583,16 @@ class EliasFanoReader : private boost::noncopyable {
   }
 
   bool skipTo(ValueType value) {
-    DCHECK_GE(value, value_);
-    if (value <= value_) {
-      return true;
-    } else if (value > lastValue_) {
+    // Also works when value_ == kInvalidValue.
+    if (value != kInvalidValue) { DCHECK_GE(value + 1, value_ + 1); }
+
+    if (!kUnchecked && value > lastValue_) {
       return setDone();
+    } else if (value == value_) {
+      return true;
     }
 
-    size_t upperValue = (value >> list_.numLowerBits);
+    size_t upperValue = (value >> numLowerBits_);
     size_t upperSkip = upperValue - upper_.value();
     // The average density of ones in upper bits is 1/2.
     // LIKELY here seems to make things worse, even for small skips.
@@ -563,46 +609,56 @@ class EliasFanoReader : private boost::noncopyable {
   }
 
   bool jump(size_t n) {
-    if (LIKELY(n - 1 < list_.size)) {  // n > 0 && n <= list_.size
-      progress_ = n;
-      value_ = readLowerPart(n - 1) | (upper_.jump(n) << list_.numLowerBits);
-      return true;
-    } else if (n == 0) {
-      reset();
+    if (LIKELY(n < size_)) {  // Also checks that n != -1.
+      value_ = readLowerPart(n) | (upper_.jump(n + 1) << numLowerBits_);
       return true;
     }
     return setDone();
   }
 
   bool jumpTo(ValueType value) {
-    if (value <= 0) {
-      reset();
-      return true;
-    } else if (value > lastValue_) {
+    if (!kUnchecked && value > lastValue_) {
       return setDone();
     }
 
-    upper_.jumpToNext(value >> list_.numLowerBits);
+    upper_.jumpToNext(value >> numLowerBits_);
     iterateTo(value);
     return true;
   }
 
-  size_t size() const { return list_.size; }
+  ValueType previousValue() const {
+    DCHECK_GT(position(), 0);
+    DCHECK_LT(position(), size());
+    return readLowerPart(upper_.position() - 1) |
+      (upper_.previousValue() << numLowerBits_);
+  }
 
-  size_t position() const { return progress_ - 1; }
-  ValueType value() const { return value_; }
+  size_t size() const { return size_; }
+
+  bool valid() const {
+    return position() < size(); // Also checks that position() != -1.
+  }
+
+  size_t position() const { return upper_.position(); }
+  ValueType value() const {
+    DCHECK(valid());
+    return value_;
+  }
 
  private:
+  constexpr static ValueType kInvalidValue =
+    std::numeric_limits<ValueType>::max();  // Must hold kInvalidValue + 1 == 0.
+
   bool setDone() {
-    value_ = std::numeric_limits<ValueType>::max();
-    progress_ = list_.size + 1;
+    value_ = kInvalidValue;
+    upper_.setDone(size_);
     return false;
   }
 
   ValueType readLowerPart(size_t i) const {
-    DCHECK_LT(i, list_.size);
-    const size_t pos = i * list_.numLowerBits;
-    const unsigned char* ptr = list_.lower.data() + (pos / 8);
+    DCHECK_LT(i, size_);
+    const size_t pos = i * numLowerBits_;
+    const unsigned char* ptr = lower_ + (pos / 8);
     const uint64_t ptrv = folly::loadUnaligned<uint64_t>(ptr);
     return lowerMask_ & (ptrv >> (pos % 8));
   }
@@ -610,21 +666,21 @@ class EliasFanoReader : private boost::noncopyable {
   void iterateTo(ValueType value) {
     while (true) {
       value_ = readLowerPart(upper_.position()) |
-        (upper_.value() << list_.numLowerBits);
+        (upper_.value() << numLowerBits_);
       if (LIKELY(value_ >= value)) break;
       upper_.next();
     }
-    progress_ = upper_.position() + 1;
   }
 
   constexpr static size_t kLinearScanThreshold = 8;
 
-  const EliasFanoCompressedList list_;
-  const ValueType lowerMask_;
+  size_t size_;
+  const uint8_t* lower_;
   detail::UpperBitsReader<Encoder, Instructions> upper_;
-  size_t progress_ = 0;
-  ValueType value_ = 0;
+  const ValueType lowerMask_;
+  ValueType value_ = kInvalidValue;
   ValueType lastValue_;
+  uint8_t numLowerBits_;
 };
 
 }}  // namespaces