proxygen
folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType > Class Template Reference

#include <EliasFanoCoding.h>

Public Types

typedef Encoder EncoderType
 
typedef Encoder::ValueType ValueType
 

Public Member Functions

 EliasFanoReader (const typename Encoder::CompressedList &list)
 
void reset ()
 
bool previous ()
 
bool next ()
 
bool skip (SizeType n)
 
bool skipTo (ValueType value)
 
void prepareSkipTo (ValueType value) const
 
bool jump (SizeType n)
 
bool jumpTo (ValueType value)
 
ValueType lastValue () const
 
ValueType previousValue () const
 
SizeType size () const
 
bool valid () const
 
SizeType position () const
 
ValueType value () const
 

Private Member Functions

bool setDone ()
 
ValueType readLowerPart (SizeType i) const
 
void iterateTo (ValueType value)
 

Private Attributes

detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
 
const uint8_tlower_
 
SizeType size_
 
ValueType value_ = kInvalidValue
 
ValueType lastValue_
 
uint8_t numLowerBits_
 

Static Private Attributes

static constexpr ValueType kInvalidValue
 
static constexpr size_t kLinearScanThreshold = 8
 

Detailed Description

template<class Encoder, class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
class folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >

Definition at line 576 of file EliasFanoCoding.h.

Member Typedef Documentation

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
typedef Encoder folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::EncoderType

Definition at line 578 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
typedef Encoder::ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::ValueType

Definition at line 579 of file EliasFanoCoding.h.

Constructor & Destructor Documentation

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::EliasFanoReader ( const typename Encoder::CompressedList &  list)
inlineexplicit

Definition at line 581 of file EliasFanoCoding.h.

References folly::findLastSet(), and UNLIKELY.

582  : upper_(list),
583  lower_(list.lower),
584  size_(list.size),
585  numLowerBits_(list.numLowerBits) {
586  DCHECK(Instructions::supported());
587  // To avoid extra branching during skipTo() while reading
588  // upper sequence we need to know the last element.
589  // If kUnchecked == true, we do not check that skipTo() is called
590  // within the bounds, so we can avoid initializing lastValue_.
591  if (kUnchecked || UNLIKELY(list.size == 0)) {
592  lastValue_ = 0;
593  return;
594  }
595  ValueType lastUpperValue = ValueType(8 * list.upperSize() - size_);
596  auto it = list.upper + list.upperSize() - 1;
597  DCHECK_NE(*it, 0);
598  lastUpperValue -= 8 - folly::findLastSet(*it);
599  lastValue_ = readLowerPart(size_ - 1) | (lastUpperValue << numLowerBits_);
600  }
ValueType readLowerPart(SizeType i) const
Encoder::MutableCompressedList list
constexpr unsigned int findLastSet(T const v)
Definition: Bits.h:105
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
#define UNLIKELY(x)
Definition: Likely.h:48

Member Function Documentation

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
void folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::iterateTo ( ValueType  value)
inlineprivate

Definition at line 766 of file EliasFanoCoding.h.

References LIKELY.

766  {
767  while (true) {
768  value_ =
769  readLowerPart(upper_.position()) | (upper_.value() << numLowerBits_);
770  if (LIKELY(value_ >= value)) {
771  break;
772  }
773  upper_.next();
774  }
775  }
#define LIKELY(x)
Definition: Likely.h:47
ValueType readLowerPart(SizeType i) const
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::jump ( SizeType  n)
inline

Definition at line 698 of file EliasFanoCoding.h.

References LIKELY.

698  {
699  if (LIKELY(n < size_)) { // Also checks that n != -1.
700  value_ = readLowerPart(n) | (upper_.jump(n + 1) << numLowerBits_);
701  return true;
702  }
703  return setDone();
704  }
#define LIKELY(x)
Definition: Likely.h:47
ValueType readLowerPart(SizeType i) const
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::jumpTo ( ValueType  value)
inline

Definition at line 706 of file EliasFanoCoding.h.

706  {
707  if (!kUnchecked && value > lastValue_) {
708  return setDone();
709  }
710 
711  upper_.jumpToNext(value >> numLowerBits_);
712  iterateTo(value);
713  return true;
714  }
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::lastValue ( ) const
inline

Definition at line 716 of file EliasFanoCoding.h.

716  {
717  CHECK(!kUnchecked);
718  return lastValue_;
719  }
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::next ( )
inline

Definition at line 618 of file EliasFanoCoding.h.

References UNLIKELY.

618  {
619  if (!kUnchecked && UNLIKELY(position() + 1 >= size_)) {
620  return setDone();
621  }
622  upper_.next();
623  value_ =
624  readLowerPart(upper_.position()) | (upper_.value() << numLowerBits_);
625  return true;
626  }
ValueType readLowerPart(SizeType i) const
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
#define UNLIKELY(x)
Definition: Likely.h:48
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
SizeType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::position ( ) const
inline

Definition at line 736 of file EliasFanoCoding.h.

736  {
737  return upper_.position();
738  }
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
void folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::prepareSkipTo ( ValueType  value) const
inline

Prepare to skip to value by prefetching appropriate memory in both the upper and lower bits.

Definition at line 679 of file EliasFanoCoding.h.

References addr.

679  {
680  // Also works when value_ == kInvalidValue.
681  if (value != kInvalidValue) {
682  DCHECK_GE(value + 1, value_ + 1);
683  }
684 
685  if ((!kUnchecked && value > lastValue_) || (value == value_)) {
686  return;
687  }
688 
689  // Do minimal computation required to prefetch address used in
690  // `readLowerPart()`.
691  ValueType upperValue = (value >> numLowerBits_);
692  const auto upperPosition = upper_.prepareSkipTo(upperValue);
693  const auto addr = lower_ + (upperPosition * numLowerBits_ / 8);
694  __builtin_prefetch(addr);
695  __builtin_prefetch(addr + kCacheLineSize);
696  }
constexpr size_t kCacheLineSize
static constexpr ValueType kInvalidValue
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
ThreadPoolListHook * addr
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::previous ( )
inline

Definition at line 607 of file EliasFanoCoding.h.

References UNLIKELY.

607  {
608  if (!kUnchecked && UNLIKELY(position() == 0)) {
609  reset();
610  return false;
611  }
612  upper_.previous();
613  value_ =
614  readLowerPart(upper_.position()) | (upper_.value() << numLowerBits_);
615  return true;
616  }
ValueType readLowerPart(SizeType i) const
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
#define UNLIKELY(x)
Definition: Likely.h:48
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::previousValue ( ) const
inline

Definition at line 721 of file EliasFanoCoding.h.

References folly::compression::EliasFanoCompressedListBase< Pointer >::size.

721  {
722  DCHECK_GT(position(), 0);
723  DCHECK_LT(position(), size());
724  return readLowerPart(upper_.position() - 1) |
725  (upper_.previousValue() << numLowerBits_);
726  }
ValueType readLowerPart(SizeType i) const
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::readLowerPart ( SizeType  i) const
inlineprivate

Definition at line 755 of file EliasFanoCoding.h.

References folly::assume(), ptr, and uint64_t.

755  {
756  DCHECK_LT(i, size_);
757  const size_t pos = i * numLowerBits_;
758  const unsigned char* ptr = lower_ + (pos / 8);
759  const uint64_t ptrv = folly::loadUnaligned<uint64_t>(ptr);
760  // This removes the branch in the fallback implementation of
761  // bzhi. The condition is verified at encoding time.
762  assume(numLowerBits_ < sizeof(ValueType) * 8);
763  return Instructions::bzhi(ptrv >> (pos % 8), numLowerBits_);
764  }
void * ptr
FOLLY_ALWAYS_INLINE void assume(bool cond)
Definition: Assume.h:41
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
void folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::reset ( )
inline

Definition at line 602 of file EliasFanoCoding.h.

602  {
603  upper_.reset();
605  }
static constexpr ValueType kInvalidValue
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::setDone ( )
inlineprivate

Definition at line 749 of file EliasFanoCoding.h.

749  {
751  upper_.setDone(size_);
752  return false;
753  }
static constexpr ValueType kInvalidValue
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
SizeType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::size ( ) const
inline

Definition at line 728 of file EliasFanoCoding.h.

728  {
729  return size_;
730  }
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::skip ( SizeType  n)
inline

Definition at line 628 of file EliasFanoCoding.h.

References i, and LIKELY.

628  {
629  CHECK_GT(n, 0);
630 
631  if (kUnchecked || LIKELY(position() + n < size_)) {
632  if (LIKELY(n < kLinearScanThreshold)) {
633  for (SizeType i = 0; i < n; ++i) {
634  upper_.next();
635  }
636  } else {
637  upper_.skip(n);
638  }
639  value_ =
640  readLowerPart(upper_.position()) | (upper_.value() << numLowerBits_);
641  return true;
642  }
643 
644  return setDone();
645  }
#define LIKELY(x)
Definition: Likely.h:47
ValueType readLowerPart(SizeType i) const
static constexpr size_t kLinearScanThreshold
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::skipTo ( ValueType  value)
inline

Definition at line 647 of file EliasFanoCoding.h.

References UNLIKELY.

647  {
648  // Also works when value_ == kInvalidValue.
649  if (value != kInvalidValue) {
650  DCHECK_GE(value + 1, value_ + 1);
651  }
652 
653  if (!kUnchecked && value > lastValue_) {
654  return setDone();
655  } else if (value == value_) {
656  return true;
657  }
658 
659  ValueType upperValue = (value >> numLowerBits_);
660  ValueType upperSkip = upperValue - upper_.value();
661  // The average density of ones in upper bits is 1/2.
662  // LIKELY here seems to make things worse, even for small skips.
663  if (upperSkip < 2 * kLinearScanThreshold) {
664  do {
665  upper_.next();
666  } while (UNLIKELY(upper_.value() < upperValue));
667  } else {
668  upper_.skipToNext(upperValue);
669  }
670 
671  iterateTo(value);
672  return true;
673  }
static constexpr size_t kLinearScanThreshold
static constexpr ValueType kInvalidValue
detail::UpperBitsReader< Encoder, Instructions, SizeType > upper_
#define UNLIKELY(x)
Definition: Likely.h:48
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
bool folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::valid ( ) const
inline

Definition at line 732 of file EliasFanoCoding.h.

References folly::compression::EliasFanoCompressedListBase< Pointer >::size.

732  {
733  return position() < size(); // Also checks that position() != -1.
734  }
template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::value ( ) const
inline

Definition at line 739 of file EliasFanoCoding.h.

739  {
740  DCHECK(valid());
741  return value_;
742  }

Member Data Documentation

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
constexpr ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::kInvalidValue
staticprivate
Initial value:

Definition at line 746 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
constexpr size_t folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::kLinearScanThreshold = 8
staticprivate

Definition at line 777 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::lastValue_
private

Definition at line 783 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
const uint8_t* folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::lower_
private

Definition at line 780 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
uint8_t folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::numLowerBits_
private

Definition at line 784 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
SizeType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::size_
private

Definition at line 781 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
detail::UpperBitsReader<Encoder, Instructions, SizeType> folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::upper_
private

Definition at line 779 of file EliasFanoCoding.h.

template<class Encoder , class Instructions = instructions::Default, bool kUnchecked = false, class SizeType = size_t>
ValueType folly::compression::EliasFanoReader< Encoder, Instructions, kUnchecked, SizeType >::value_ = kInvalidValue
private

Definition at line 782 of file EliasFanoCoding.h.


The documentation for this class was generated from the following file: