Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions hll/include/AuxHashMap-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ auxCount(0),
entries(1ULL << lgAuxArrInts, 0, allocator)
{}

template<typename A>
AuxHashMap<A>::AuxHashMap(const AuxHashMap& that, const A& allocator):
lgConfigK(that.lgConfigK),
lgAuxArrInts(that.lgAuxArrInts),
auxCount(that.auxCount),
entries(that.entries, allocator)
{}

template<typename A>
AuxHashMap<A>* AuxHashMap<A>::newAuxHashMap(uint8_t lgAuxArrInts, uint8_t lgConfigK, const A& allocator) {
return new (ahmAlloc(allocator).allocate(1)) AuxHashMap<A>(lgAuxArrInts, lgConfigK, allocator);
return HllUtil<A>::template newObject<AuxHashMap<A>>(allocator, lgAuxArrInts, lgConfigK, allocator);
}

template<typename A>
AuxHashMap<A>* AuxHashMap<A>::newAuxHashMap(const AuxHashMap& that) {
return new (ahmAlloc(that.entries.get_allocator()).allocate(1)) AuxHashMap<A>(that);
const A allocator(that.entries.get_allocator());
return HllUtil<A>::template newObject<AuxHashMap<A>>(allocator, that, allocator);
}

template<typename A>
Expand All @@ -60,12 +69,14 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(const void* bytes, size_t len,
const uint32_t configKmask = (1 << lgConfigK) - 1;

AuxHashMap<A>* auxHashMap;
std::unique_ptr<AuxHashMap<A>, std::function<void(AuxHashMap<A>*)>> aux_ptr(nullptr, make_deleter());
const uint32_t* auxPtr = static_cast<const uint32_t*>(bytes);
if (srcCompact) {
if (len < auxCount * sizeof(int)) {
throw std::out_of_range("Input array too small to hold AuxHashMap image");
}
auxHashMap = new (ahmAlloc(allocator).allocate(1)) AuxHashMap<A>(lgArrInts, lgConfigK, allocator);
auxHashMap = newAuxHashMap(lgArrInts, lgConfigK, allocator);
aux_ptr.reset(auxHashMap);
for (uint32_t i = 0; i < auxCount; ++i) {
const uint32_t pair = auxPtr[i];
const uint32_t slotNo = HllUtil<A>::getLow26(pair) & configKmask;
Expand All @@ -77,7 +88,8 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(const void* bytes, size_t len,
if (len < itemsToRead * sizeof(uint32_t)) {
throw std::out_of_range("Input array too small to hold AuxHashMap image");
}
auxHashMap = new (ahmAlloc(allocator).allocate(1)) AuxHashMap<A>(lgArrInts, lgConfigK, allocator);
auxHashMap = newAuxHashMap(lgArrInts, lgConfigK, allocator);
aux_ptr.reset(auxHashMap);
for (uint32_t i = 0; i < itemsToRead; ++i) {
const uint32_t pair = auxPtr[i];
if (pair == hll_constants::EMPTY) { continue; }
Expand All @@ -88,11 +100,10 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(const void* bytes, size_t len,
}

if (auxHashMap->getAuxCount() != auxCount) {
make_deleter()(auxHashMap);
throw std::invalid_argument("Deserialized AuxHashMap has wrong number of entries");
}

return auxHashMap;
return aux_ptr.release();
}

template<typename A>
Expand All @@ -106,7 +117,7 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(std::istream& is, uint8_t lgConfigK,
lgArrInts = lgAuxArrInts;
}

AuxHashMap<A>* auxHashMap = new (ahmAlloc(allocator).allocate(1)) AuxHashMap<A>(lgArrInts, lgConfigK, allocator);
AuxHashMap<A>* auxHashMap = newAuxHashMap(lgArrInts, lgConfigK, allocator);
typedef std::unique_ptr<AuxHashMap<A>, std::function<void(AuxHashMap<A>*)>> aux_hash_map_ptr;
aux_hash_map_ptr aux_ptr(auxHashMap, auxHashMap->make_deleter());

Expand All @@ -131,7 +142,6 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(std::istream& is, uint8_t lgConfigK,
}

if (auxHashMap->getAuxCount() != auxCount) {
make_deleter()(auxHashMap);
throw std::invalid_argument("Deserialized AuxHashMap has wrong number of entries");
}

Expand All @@ -149,7 +159,7 @@ std::function<void(AuxHashMap<A>*)> AuxHashMap<A>::make_deleter() {

template<typename A>
AuxHashMap<A>* AuxHashMap<A>::copy() const {
return new (ahmAlloc(entries.get_allocator()).allocate(1)) AuxHashMap<A>(*this);
return newAuxHashMap(*this);
}

template<typename A>
Expand Down Expand Up @@ -189,7 +199,13 @@ void AuxHashMap<A>::mustAdd(uint32_t slotNo, uint8_t value) {
// found empty entry
entries[~index] = entry_pair;
++auxCount;
checkGrow();
try {
checkGrow();
} catch (...) {
entries[~index] = hll_constants::EMPTY;
--auxCount;
throw;
}
}

template<typename A>
Expand Down Expand Up @@ -224,17 +240,19 @@ void AuxHashMap<A>::checkGrow() {
template<typename A>
void AuxHashMap<A>::growAuxSpace() {
const int configKmask = (1 << lgConfigK) - 1;
const int newArrLen = 1 << ++lgAuxArrInts;
const uint8_t newLgAuxArrInts = static_cast<uint8_t>(lgAuxArrInts + 1);
const int newArrLen = 1 << newLgAuxArrInts;
vector_int entries_new(newArrLen, 0, entries.get_allocator());
for (size_t i = 0; i < entries.size(); ++i) {
const uint32_t fetched = entries[i];
if (fetched != hll_constants::EMPTY) {
// find empty in new array
const int32_t idx = find(entries_new.data(), lgAuxArrInts, lgConfigK, fetched & configKmask);
const int32_t idx = find(entries_new.data(), newLgAuxArrInts, lgConfigK, fetched & configKmask);
entries_new[~idx] = fetched;
}
}
entries = std::move(entries_new);
lgAuxArrInts = newLgAuxArrInts;
}

//Searches the Aux arr hash table for an empty or a matching slotNo depending on the context.
Expand Down
1 change: 1 addition & 0 deletions hll/include/AuxHashMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ template<typename A>
class AuxHashMap final {
public:
AuxHashMap(uint8_t lgAuxArrInts, uint8_t lgConfigK, const A& allocator);
AuxHashMap(const AuxHashMap& that, const A& allocator);
static AuxHashMap* newAuxHashMap(uint8_t lgAuxArrInts, uint8_t lgConfigK, const A& allocator);
static AuxHashMap* newAuxHashMap(const AuxHashMap<A>& that);

Expand Down
120 changes: 76 additions & 44 deletions hll/include/CouponHashSet-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "CouponHashSet.hpp"

#include <algorithm>
#include <cstring>
#include <exception>
#include <stdexcept>
Expand Down Expand Up @@ -55,6 +56,39 @@ std::function<void(HllSketchImpl<A>*)> CouponHashSet<A>::get_deleter() const {
};
}

template<typename A>
uint32_t CouponHashSet<A>::getSerializedArraySize(uint8_t lgK, uint8_t lgArrInts, bool compact, uint32_t couponCount) {
const uint32_t maxCouponCount = hll_constants::RESIZE_NUMER * (uint32_t(1) << (lgK - 3))
/ hll_constants::RESIZE_DENOM;
if (couponCount > maxCouponCount) {
throw std::invalid_argument("CouponHashSet count exceeds the maximum for its precision: " + std::to_string(couponCount));
}
if (lgArrInts < hll_constants::LG_INIT_SET_SIZE) {
lgArrInts = HllUtil<A>::computeLgArrInts(SET, couponCount, lgK);
}
if (compact) return couponCount;
if (lgArrInts > lgK - 3) {
throw std::invalid_argument("CouponHashSet table size exceeds the maximum for its precision: "
+ std::to_string(lgArrInts));
}
const uint32_t capacity = uint32_t(1) << lgArrInts;
if (couponCount > hll_constants::RESIZE_NUMER * capacity / hll_constants::RESIZE_DENOM) {
throw std::invalid_argument("CouponHashSet count exceeds the table load limit: " + std::to_string(couponCount));
}
return capacity;
}

template<typename A>
void CouponHashSet<A>::checkOccupiedSlots(const vector_int& coupons, uint32_t couponCount) {
const auto occupied = std::count_if(coupons.begin(), coupons.end(), [](uint32_t coupon) {
return coupon != hll_constants::EMPTY;
});
if (occupied != couponCount) {
throw std::invalid_argument("CouponHashSet count does not match the number of occupied slots: "
+ std::to_string(couponCount) + " vs " + std::to_string(occupied));
}
}

template<typename A>
CouponHashSet<A>* CouponHashSet<A>::newSet(const void* bytes, size_t len, const A& allocator) {
if (len < hll_constants::HASH_SET_INT_ARR_START) { // hard-coded
Expand Down Expand Up @@ -83,26 +117,23 @@ CouponHashSet<A>* CouponHashSet<A>::newSet(const void* bytes, size_t len, const
if (lgK <= 7) {
throw std::invalid_argument("Attempt to deserialize invalid CouponHashSet with lgConfigK <= 7. Found: "
+ std::to_string(lgK));
}
uint8_t lgArrInts = data[hll_constants::LG_ARR_BYTE];
}
HllUtil<A>::checkLgK(lgK);
const uint8_t lgArrInts = data[hll_constants::LG_ARR_BYTE];
const bool compactFlag = ((data[hll_constants::FLAGS_BYTE] & hll_constants::COMPACT_FLAG_MASK) ? true : false);

uint32_t couponCount;
std::memcpy(&couponCount, data + hll_constants::HASH_SET_COUNT_INT, sizeof(couponCount));
if (lgArrInts < hll_constants::LG_INIT_SET_SIZE) {
lgArrInts = HllUtil<>::computeLgArrInts(SET, couponCount, lgK);
}
// Don't set couponCount in sketch here;
// we'll set later if updatable, and increment with updates if compact
const uint32_t couponsInArray = (compactFlag ? couponCount : (1 << lgArrInts));
const size_t expectedLength = hll_constants::HASH_SET_INT_ARR_START + (couponsInArray * sizeof(uint32_t));
const uint32_t couponsInArray = getSerializedArraySize(lgK, lgArrInts, compactFlag, couponCount);
const size_t arrayBytes = couponsInArray * sizeof(uint32_t);
const size_t expectedLength = hll_constants::HASH_SET_INT_ARR_START + arrayBytes;
if (len < expectedLength) {
throw std::out_of_range("Byte array too short for sketch. Expected " + std::to_string(expectedLength)
+ ", found: " + std::to_string(len));
}

ChsAlloc chsa(allocator);
CouponHashSet<A>* sketch = new (chsa.allocate(1)) CouponHashSet<A>(lgK, tgtHllType, allocator);
auto sketch = HllUtil<A>::template newObject<CouponHashSet<A>>(allocator, lgK, tgtHllType, allocator);
std::unique_ptr<CouponHashSet<A>, std::function<void(HllSketchImpl<A>*)>> ptr(sketch, sketch->get_deleter());

if (compactFlag) {
const uint8_t* curPos = data + hll_constants::HASH_SET_INT_ARR_START;
Expand All @@ -112,20 +143,20 @@ CouponHashSet<A>* CouponHashSet<A>::newSet(const void* bytes, size_t len, const
sketch->couponUpdate(coupon);
}
} else {
sketch->coupons_.resize(1ULL << lgArrInts);
sketch->coupons_.resize(couponsInArray);
std::memcpy(sketch->coupons_.data(), data + hll_constants::HASH_SET_INT_ARR_START, arrayBytes);
checkOccupiedSlots(sketch->coupons_, couponCount);
sketch->couponCount_ = couponCount;
std::memcpy(sketch->coupons_.data(),
data + hll_constants::HASH_SET_INT_ARR_START,
couponsInArray * sizeof(uint32_t));
}

return sketch;
return ptr.release();
}

template<typename A>
CouponHashSet<A>* CouponHashSet<A>::newSet(std::istream& is, const A& allocator) {
uint8_t listHeader[8];
read(is, listHeader, 8 * sizeof(uint8_t));
read(is, listHeader, sizeof(listHeader));
if (!is.good()) { throw std::runtime_error("error reading from std::istream"); }

if (listHeader[hll_constants::PREAMBLE_INTS_BYTE] != hll_constants::HASH_SET_PREINTS) {
throw std::invalid_argument("Incorrect number of preInts in input stream");
Expand All @@ -149,48 +180,45 @@ CouponHashSet<A>* CouponHashSet<A>::newSet(std::istream& is, const A& allocator)
throw std::invalid_argument("Attempt to deserialize invalid CouponHashSet with lgConfigK <= 7. Found: "
+ std::to_string(lgK));
}
uint8_t lgArrInts = listHeader[hll_constants::LG_ARR_BYTE];
HllUtil<A>::checkLgK(lgK);
const uint8_t lgArrInts = listHeader[hll_constants::LG_ARR_BYTE];
const bool compactFlag = ((listHeader[hll_constants::FLAGS_BYTE] & hll_constants::COMPACT_FLAG_MASK) ? true : false);

const auto couponCount = read<uint32_t>(is);
if (lgArrInts < hll_constants::LG_INIT_SET_SIZE) {
lgArrInts = HllUtil<>::computeLgArrInts(SET, couponCount, lgK);
}
uint32_t couponCount;
read(is, &couponCount, sizeof(couponCount));
if (!is.good()) { throw std::runtime_error("error reading from std::istream"); }
const uint32_t couponsInArray = getSerializedArraySize(lgK, lgArrInts, compactFlag, couponCount);

ChsAlloc chsa(allocator);
CouponHashSet<A>* sketch = new (chsa.allocate(1)) CouponHashSet<A>(lgK, tgtHllType, allocator);
auto sketch = HllUtil<A>::template newObject<CouponHashSet<A>>(allocator, lgK, tgtHllType, allocator);
typedef std::unique_ptr<CouponHashSet<A>, std::function<void(HllSketchImpl<A>*)>> coupon_hash_set_ptr;
coupon_hash_set_ptr ptr(sketch, sketch->get_deleter());

// Don't set couponCount here;
// we'll set later if updatable, and increment with updates if compact
if (compactFlag) {
for (uint32_t i = 0; i < couponCount; ++i) {
const auto coupon = read<uint32_t>(is);
uint32_t coupon;
read(is, &coupon, sizeof(coupon));
if (!is.good()) { throw std::runtime_error("error reading from std::istream"); }
sketch->couponUpdate(coupon);
}
} else {
sketch->coupons_.resize(1ULL << lgArrInts);
sketch->coupons_.resize(couponsInArray);
read(is, sketch->coupons_.data(), couponsInArray * sizeof(uint32_t));
if (!is.good()) { throw std::runtime_error("error reading from std::istream"); }
checkOccupiedSlots(sketch->coupons_, couponCount);
sketch->couponCount_ = couponCount;
// for stream processing, read entire list so read pointer ends up set correctly
read(is, sketch->coupons_.data(), sketch->coupons_.size() * sizeof(uint32_t));
}

if (!is.good()) { throw std::runtime_error("error reading from std::istream"); }
}

return ptr.release();
}

template<typename A>
CouponHashSet<A>* CouponHashSet<A>::copy() const {
ChsAlloc chsa(this->coupons_.get_allocator());
return new (chsa.allocate(1)) CouponHashSet<A>(*this);
return HllUtil<A>::template newObject<CouponHashSet<A>>(this->getAllocator(), *this);
}

template<typename A>
CouponHashSet<A>* CouponHashSet<A>::copyAs(target_hll_type tgtHllType) const {
ChsAlloc chsa(this->coupons_.get_allocator());
return new (chsa.allocate(1)) CouponHashSet<A>(*this, tgtHllType);
return HllUtil<A>::template newObject<CouponHashSet<A>>(this->getAllocator(), *this, tgtHllType);
}

template<typename A>
Expand All @@ -202,8 +230,8 @@ HllSketchImpl<A>* CouponHashSet<A>::couponUpdate(uint32_t coupon) {
}
this->coupons_[~index] = coupon; // found empty
++this->couponCount_;
if (checkGrowOrPromote()) {
return this->promoteHeapListOrSetToHll(*this);
if (static_cast<size_t>(hll_constants::RESIZE_DENOM * this->couponCount_) > (hll_constants::RESIZE_NUMER * this->coupons_.size())) {
return growOrPromoteWithRollback(static_cast<uint32_t>(~index));
}
return this;
}
Expand All @@ -219,15 +247,19 @@ uint8_t CouponHashSet<A>::getPreInts() const {
}

template<typename A>
bool CouponHashSet<A>::checkGrowOrPromote() {
if (static_cast<size_t>(hll_constants::RESIZE_DENOM * this->couponCount_) > (hll_constants::RESIZE_NUMER * this->coupons_.size())) {
HllSketchImpl<A>* CouponHashSet<A>::growOrPromoteWithRollback(uint32_t index) {
try {
const uint8_t lgCouponArrInts = count_trailing_zeros_in_u32(static_cast<uint32_t>(this->coupons_.size()));
if (lgCouponArrInts == (this->lgConfigK_ - 3)) { // at max size
return true; // promote to HLL
return this->promoteHeapListOrSetToHll(*this);
}
growHashSet(lgCouponArrInts + 1);
growHashSet(static_cast<uint8_t>(lgCouponArrInts + 1));
} catch (...) {
this->coupons_[index] = hll_constants::EMPTY;
--this->couponCount_;
throw;
}
return false;
return this;
}

template<typename A>
Expand Down
9 changes: 8 additions & 1 deletion hll/include/CouponHashSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ class CouponHashSet : public CouponList<A> {

private:
using ChsAlloc = typename std::allocator_traits<A>::template rebind_alloc<CouponHashSet<A>>;
bool checkGrowOrPromote();
static uint32_t getSerializedArraySize(uint8_t lgK, uint8_t lgArrInts, bool compact, uint32_t couponCount);
static void checkOccupiedSlots(const vector_int& coupons, uint32_t couponCount);
#if defined(_MSC_VER)
__declspec(noinline)
#elif defined(__GNUC__) || defined(__clang__)
__attribute__((noinline))
#endif
HllSketchImpl<A>* growOrPromoteWithRollback(uint32_t index);
void growHashSet(uint8_t tgtLgCoupArrSize);
};

Expand Down
Loading
Loading