You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2019/06/24 22:49:49 UTC
[incubator-datasketches-cpp] 05/05: use placement new
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a commit to branch placement_new
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-cpp.git
commit 08eca0d097921970eac530400c5f51e7f32dc7a8
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Jun 24 15:49:31 2019 -0700
use placement new
---
hll/include/AuxHashMap-internal.hpp | 24 +++++++-----------------
hll/include/CouponHashSet-internal.hpp | 14 ++++----------
hll/include/Hll4Array-internal.hpp | 7 ++-----
hll/include/Hll6Array-internal.hpp | 7 ++-----
hll/include/Hll8Array-internal.hpp | 9 +++------
hll/include/HllSketch-internal.hpp | 14 ++++++--------
hll/include/HllSketchImplFactory.hpp | 33 +++++++++------------------------
hll/include/HllUnion-internal.hpp | 7 +++----
8 files changed, 36 insertions(+), 79 deletions(-)
diff --git a/hll/include/AuxHashMap-internal.hpp b/hll/include/AuxHashMap-internal.hpp
index 2cea0c5..6f483fd 100644
--- a/hll/include/AuxHashMap-internal.hpp
+++ b/hll/include/AuxHashMap-internal.hpp
@@ -44,9 +44,7 @@ AuxHashMap<A>::AuxHashMap(int lgAuxArrInts, int lgConfigK)
template<typename A>
AuxHashMap<A>* AuxHashMap<A>::newAuxHashMap(int lgAuxArrInts, int lgConfigK) {
- AuxHashMap<A>* map = ahmAlloc().allocate(1);
- ahmAlloc().construct(map, lgAuxArrInts, lgConfigK);
- return map;
+ return new (ahmAlloc().allocate(1)) AuxHashMap<A>(lgAuxArrInts, lgConfigK);
}
template<typename A>
@@ -62,9 +60,7 @@ AuxHashMap<A>::AuxHashMap(const AuxHashMap& that)
template<typename A>
AuxHashMap<A>* AuxHashMap<A>::newAuxHashMap(const AuxHashMap& that) {
- AuxHashMap<A>* map = ahmAlloc().allocate(1);
- ahmAlloc().construct(map, that);
- return map;
+ return new (ahmAlloc().allocate(1)) AuxHashMap<A>(that);
}
template<typename A>
@@ -87,8 +83,7 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(const void* bytes, size_t len,
if (len < auxCount * sizeof(int)) {
throw std::invalid_argument("Input array too small to hold AuxHashMap image");
}
- auxHashMap = ahmAlloc().allocate(1);
- ahmAlloc().construct(auxHashMap, lgArrInts, lgConfigK);
+ auxHashMap = new (ahmAlloc().allocate(1)) AuxHashMap<A>(lgArrInts, lgConfigK);
for (int i = 0; i < auxCount; ++i) {
int pair = auxPtr[i];
int slotNo = HllUtil<A>::getLow26(pair) & configKmask;
@@ -100,8 +95,7 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(const void* bytes, size_t len,
if (len < itemsToRead * sizeof(int)) {
throw std::invalid_argument("Input array too small to hold AuxHashMap image");
}
- auxHashMap = ahmAlloc().allocate(1);
- ahmAlloc().construct(auxHashMap, lgArrInts, lgConfigK);
+ auxHashMap = new (ahmAlloc().allocate(1)) AuxHashMap<A>(lgArrInts, lgConfigK);
for (int i = 0; i < itemsToRead; ++i) {
int pair = auxPtr[i];
if (pair == HllUtil<A>::EMPTY) { continue; }
@@ -131,8 +125,7 @@ AuxHashMap<A>* AuxHashMap<A>::deserialize(std::istream& is, const int lgConfigK,
}
// TODO: truncated stream will throw exception without freeing memory
- AuxHashMap<A>* auxHashMap = ahmAlloc().allocate(1);
- ahmAlloc().construct(auxHashMap, lgArrInts, lgConfigK);
+ AuxHashMap<A>* auxHashMap = new (ahmAlloc().allocate(1)) AuxHashMap<A>(lgArrInts, lgConfigK);
int configKmask = (1 << lgConfigK) - 1;
if (srcCompact) {
@@ -180,9 +173,7 @@ std::function<void(AuxHashMap<A>*)> AuxHashMap<A>::make_deleter() {
template<typename A>
AuxHashMap<A>* AuxHashMap<A>::copy() const {
- AuxHashMap<A>* ptr = ahmAlloc().allocate(1);
- ahmAlloc().construct(ptr, *this);
- return ptr;
+ return new (ahmAlloc().allocate(1)) AuxHashMap<A>(*this);
}
template<typename A>
@@ -213,8 +204,7 @@ int AuxHashMap<A>::getUpdatableSizeBytes() const {
template<typename A>
std::unique_ptr<PairIterator<A>, std::function<void(PairIterator<A>*)>> AuxHashMap<A>::getIterator() const {
typedef typename std::allocator_traits<A>::template rebind_alloc<IntArrayPairIterator<A>> iapiAlloc;
- IntArrayPairIterator<A>* itr = iapiAlloc().allocate(1);
- iapiAlloc().construct(itr, auxIntArr, 1 << lgAuxArrInts, lgConfigK);
+ IntArrayPairIterator<A>* itr = new (iapiAlloc().allocate(1)) IntArrayPairIterator<A>(auxIntArr, 1 << lgAuxArrInts, lgConfigK);
return std::unique_ptr<PairIterator<A>, std::function<void(PairIterator<A>*)>>(
itr,
[](PairIterator<A>* ptr) {
diff --git a/hll/include/CouponHashSet-internal.hpp b/hll/include/CouponHashSet-internal.hpp
index 2e69d06..49dcf8a 100644
--- a/hll/include/CouponHashSet-internal.hpp
+++ b/hll/include/CouponHashSet-internal.hpp
@@ -109,8 +109,7 @@ CouponHashSet<A>* CouponHashSet<A>::newSet(const void* bytes, size_t len) {
+ ", found: " + std::to_string(len));
}
- CouponHashSet<A>* sketch = chsAlloc().allocate(1);
- chsAlloc().construct(sketch, lgK, tgtHllType);
+ CouponHashSet<A>* sketch = new (chsAlloc().allocate(1)) CouponHashSet<A>(lgK, tgtHllType);
sketch->putOutOfOrderFlag(true);
if (compactFlag) {
@@ -174,8 +173,7 @@ CouponHashSet<A>* CouponHashSet<A>::newSet(std::istream& is) {
lgArrInts = HllUtil<A>::computeLgArrInts(SET, couponCount, lgK);
}
- CouponHashSet<A>* sketch = chsAlloc().allocate(1);
- chsAlloc().construct(sketch, lgK, tgtHllType);
+ CouponHashSet<A>* sketch = new (chsAlloc().allocate(1)) CouponHashSet<A>(lgK, tgtHllType);
sketch->putOutOfOrderFlag(true);
// Don't set couponCount here;
@@ -203,16 +201,12 @@ CouponHashSet<A>* CouponHashSet<A>::newSet(std::istream& is) {
template<typename A>
CouponHashSet<A>* CouponHashSet<A>::copy() const {
- CouponHashSet<A>* sketch = chsAlloc().allocate(1);
- chsAlloc().construct(sketch, *this);
- return sketch;
+ return new (chsAlloc().allocate(1)) CouponHashSet<A>(*this);
}
template<typename A>
CouponHashSet<A>* CouponHashSet<A>::copyAs(const TgtHllType tgtHllType) const {
- CouponHashSet<A>* sketch = chsAlloc().allocate(1);
- chsAlloc().construct(sketch, *this, tgtHllType);
- return sketch;
+ return new (chsAlloc().allocate(1)) CouponHashSet<A>(*this, tgtHllType);
}
template<typename A>
diff --git a/hll/include/Hll4Array-internal.hpp b/hll/include/Hll4Array-internal.hpp
index 454131e..c9abffb 100644
--- a/hll/include/Hll4Array-internal.hpp
+++ b/hll/include/Hll4Array-internal.hpp
@@ -93,16 +93,13 @@ std::function<void(HllSketchImpl<A>*)> Hll4Array<A>::get_deleter() const {
template<typename A>
Hll4Array<A>* Hll4Array<A>::copy() const {
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll4Array<A>> hll4Alloc;
- Hll4Array<A>* hll = hll4Alloc().allocate(1);
- hll4Alloc().construct(hll, *this);
- return hll;
+ return new (hll4Alloc().allocate(1)) Hll4Array<A>(*this);
}
template<typename A>
PairIterator_with_deleter<A> Hll4Array<A>::getIterator() const {
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll4Iterator<A>> itrAlloc;
- Hll4Iterator<A>* itr = itrAlloc().allocate(1);
- itrAlloc().construct(itr, *this, 1 << this->lgConfigK);
+ Hll4Iterator<A>* itr = new (itrAlloc().allocate(1)) Hll4Iterator<A>(*this, 1 << this->lgConfigK);
return PairIterator_with_deleter<A>(
itr,
[](PairIterator<A>* ptr) {
diff --git a/hll/include/Hll6Array-internal.hpp b/hll/include/Hll6Array-internal.hpp
index 7e92844..f23074b 100644
--- a/hll/include/Hll6Array-internal.hpp
+++ b/hll/include/Hll6Array-internal.hpp
@@ -80,16 +80,13 @@ std::function<void(HllSketchImpl<A>*)> Hll6Array<A>::get_deleter() const {
template<typename A>
Hll6Array<A>* Hll6Array<A>::copy() const {
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll6Array<A>> hll6Alloc;
- Hll6Array<A>* hll = hll6Alloc().allocate(1);
- hll6Alloc().construct(hll, *this);
- return hll;
+ return new (hll6Alloc().allocate(1)) Hll6Array<A>(*this);
}
template<typename A>
PairIterator_with_deleter<A> Hll6Array<A>::getIterator() const {
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll6Iterator<A>> itrAlloc;
- Hll6Iterator<A>* itr = itrAlloc().allocate(1);
- itrAlloc().construct(itr, *this, 1 << this->lgConfigK);
+ Hll6Iterator<A>* itr = new (itrAlloc().allocate(1)) Hll6Iterator<A>(*this, 1 << this->lgConfigK);
return PairIterator_with_deleter<A>(
itr,
[](PairIterator<A>* ptr) {
diff --git a/hll/include/Hll8Array-internal.hpp b/hll/include/Hll8Array-internal.hpp
index 3da2bf6..20474db 100644
--- a/hll/include/Hll8Array-internal.hpp
+++ b/hll/include/Hll8Array-internal.hpp
@@ -74,16 +74,13 @@ std::function<void(HllSketchImpl<A>*)> Hll8Array<A>::get_deleter() const {
template<typename A>
Hll8Array<A>* Hll8Array<A>::copy() const {
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>> hll8Alloc;
- Hll8Array<A>* hll = hll8Alloc().allocate(1);
- hll8Alloc().construct(hll, *this);
- return hll;
+ return new (hll8Alloc().allocate(1)) Hll8Array<A>(*this);
}
template<typename A>
PairIterator_with_deleter<A> Hll8Array<A>::getIterator() const {
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll8Iterator<A>> itrAlloc;
- Hll8Iterator<A>* itr = itrAlloc().allocate(1);
- itrAlloc().construct(itr, *this, 1 << this->lgConfigK);
+ Hll8Iterator<A>* itr = new (itrAlloc().allocate(1)) Hll8Iterator<A>(*this, 1 << this->lgConfigK);
return PairIterator_with_deleter<A>(
itr,
[](PairIterator<A>* ptr) {
@@ -134,4 +131,4 @@ HllSketchImpl<A>* Hll8Array<A>::couponUpdate(const int coupon) { // used by HLL_
}
-#endif // _HLL8ARRAY_INTERNAL_HPP_
\ No newline at end of file
+#endif // _HLL8ARRAY_INTERNAL_HPP_
diff --git a/hll/include/HllSketch-internal.hpp b/hll/include/HllSketch-internal.hpp
index c6d4cae..da28f04 100644
--- a/hll/include/HllSketch-internal.hpp
+++ b/hll/include/HllSketch-internal.hpp
@@ -48,9 +48,7 @@ HllSketch<A>::HllSketch(int lgConfigK, TgtHllType tgtHllType, bool startFullSize
hllSketchImpl = HllSketchImplFactory<A>::newHll(lgConfigK, tgtHllType, startFullSize);
} else {
typedef typename std::allocator_traits<A>::template rebind_alloc<CouponList<A>> clAlloc;
- CouponList<A>* cl = clAlloc().allocate(1);
- clAlloc().construct(cl, lgConfigK, tgtHllType, CurMode::LIST);
- hllSketchImpl = cl;
+ hllSketchImpl = new (clAlloc().allocate(1)) CouponList<A>(lgConfigK, tgtHllType, CurMode::LIST);
}
}
@@ -76,7 +74,7 @@ HllSketch<A>::~HllSketch() {
}
template<typename A>
-std::ostream& operator<<(std::ostream& os, HllSketch<A>& sketch) {
+std::ostream& operator<<(std::ostream& os, const HllSketch<A>& sketch) {
return sketch.to_string(os, true, true, false, false);
}
@@ -234,7 +232,7 @@ void HllSketch<A>::couponUpdate(int coupon) {
if (coupon == HllUtil<A>::EMPTY) { return; }
HllSketchImpl<A>* result = this->hllSketchImpl->couponUpdate(coupon);
if (result != this->hllSketchImpl) {
- delete this->hllSketchImpl;
+ this->hllSketchImpl->get_deleter()(this->hllSketchImpl);
this->hllSketchImpl = result;
}
}
@@ -251,14 +249,14 @@ void HllSketch<A>::serializeUpdatable(std::ostream& os) const {
template<typename A>
std::pair<std::unique_ptr<uint8_t, std::function<void(uint8_t*)>>, const size_t>
-HllSketch<A>::serializeCompact() const {
- return hllSketchImpl->serialize(true);
+HllSketch<A>::serializeCompact(unsigned header_size_bytes) const {
+ return hllSketchImpl->serialize(true, header_size_bytes);
}
template<typename A>
std::pair<std::unique_ptr<uint8_t, std::function<void(uint8_t*)>>, const size_t>
HllSketch<A>::serializeUpdatable() const {
- return hllSketchImpl->serialize(false);
+ return hllSketchImpl->serialize(false, 0);
}
template<typename A>
diff --git a/hll/include/HllSketchImplFactory.hpp b/hll/include/HllSketchImplFactory.hpp
index 9cf0a7b..cb71290 100644
--- a/hll/include/HllSketchImplFactory.hpp
+++ b/hll/include/HllSketchImplFactory.hpp
@@ -57,8 +57,7 @@ CouponHashSet<A>* HllSketchImplFactory<A>::promoteListToSet(const CouponList<A>&
PairIterator_with_deleter<A> iter = list.getIterator();
typedef typename std::allocator_traits<A>::template rebind_alloc<CouponHashSet<A>> chsAlloc;
- CouponHashSet<A>* chSet = chsAlloc().allocate(1);
- chsAlloc().construct(chSet, list.getLgConfigK(), list.getTgtHllType());
+ CouponHashSet<A>* chSet = new (chsAlloc().allocate(1)) CouponHashSet<A>(list.getLgConfigK(), list.getTgtHllType());
while (iter->nextValid()) {
chSet->couponUpdate(iter->getPair());
}
@@ -113,26 +112,16 @@ HllSketchImpl<A>* HllSketchImplFactory<A>::deserialize(const void* bytes, size_t
template<typename A>
HllArray<A>* HllSketchImplFactory<A>::newHll(int lgConfigK, TgtHllType tgtHllType, bool startFullSize) {
- HllArray<A>* hll;
switch (tgtHllType) {
case HLL_8:
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>> hll8Alloc;
- hll = hll8Alloc().allocate(1);
- hll8Alloc().construct((Hll8Array<A>*) hll, lgConfigK, startFullSize);
- return hll;
- //return (HllArray<A>*) new Hll8Array<A>(lgConfigK, startFullSize);
+ return new (hll8Alloc().allocate(1)) Hll8Array<A>(lgConfigK, startFullSize);
case HLL_6:
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll6Array<A>> hll6Alloc;
- hll = hll6Alloc().allocate(1);
- hll6Alloc().construct((Hll6Array<A>*) hll, lgConfigK, startFullSize);
- return hll;
- //return (HllArray<A>*) new Hll6Array<A>(lgConfigK, startFullSize);
+ return new (hll6Alloc().allocate(1)) Hll6Array<A>(lgConfigK, startFullSize);
case HLL_4:
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll4Array<A>> hll4Alloc;
- hll = hll4Alloc().allocate(1);
- hll4Alloc().construct((Hll4Array<A>*) hll, lgConfigK, startFullSize);
- return hll;
- //return (HllArray<A>*) new Hll4Array<A>(lgConfigK, startFullSize);
+ return new (hll4Alloc().allocate(1)) Hll4Array<A>(lgConfigK, startFullSize);
}
throw std::logic_error("Invalid TgtHllType");
}
@@ -145,8 +134,7 @@ HllSketchImpl<A>* HllSketchImplFactory<A>::reset(HllSketchImpl<A>* impl, bool st
return hll;
} else {
typedef typename std::allocator_traits<A>::template rebind_alloc<CouponList<A>> clAlloc;
- CouponList<A>* cl = clAlloc().allocate(1);
- clAlloc().construct(cl, impl->getLgConfigK(), impl->getTgtHllType(), CurMode::LIST);
+ CouponList<A>* cl = new (clAlloc().allocate(1)) CouponList<A>(impl->getLgConfigK(), impl->getTgtHllType(), CurMode::LIST);
impl->get_deleter()(impl);
return cl;
}
@@ -156,8 +144,7 @@ template<typename A>
Hll4Array<A>* HllSketchImplFactory<A>::convertToHll4(const HllArray<A>& srcHllArr) {
const int lgConfigK = srcHllArr.getLgConfigK();
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll4Array<A>> hll4Alloc;
- Hll4Array<A>* hll4Array = hll4Alloc().allocate(1);
- hll4Alloc().construct(hll4Array, lgConfigK, srcHllArr.isStartFullSize());
+ Hll4Array<A>* hll4Array = new (hll4Alloc().allocate(1)) Hll4Array<A>(lgConfigK, srcHllArr.isStartFullSize());
hll4Array->putOutOfOrderFlag(srcHllArr.isOutOfOrderFlag());
// 1st pass: compute starting curMin and numAtCurMin
@@ -216,8 +203,7 @@ template<typename A>
Hll6Array<A>* HllSketchImplFactory<A>::convertToHll6(const HllArray<A>& srcHllArr) {
const int lgConfigK = srcHllArr.getLgConfigK();
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll6Array<A>> hll6Alloc;
- Hll6Array<A>* hll6Array = hll6Alloc().allocate(1);
- hll6Alloc().construct(hll6Array, lgConfigK, srcHllArr.isStartFullSize());
+ Hll6Array<A>* hll6Array = new (hll6Alloc().allocate(1)) Hll6Array<A>(lgConfigK, srcHllArr.isStartFullSize());
hll6Array->putOutOfOrderFlag(srcHllArr.isOutOfOrderFlag());
int numZeros = 1 << lgConfigK;
@@ -238,8 +224,7 @@ template<typename A>
Hll8Array<A>* HllSketchImplFactory<A>::convertToHll8(const HllArray<A>& srcHllArr) {
const int lgConfigK = srcHllArr.getLgConfigK();
typedef typename std::allocator_traits<A>::template rebind_alloc<Hll8Array<A>> hll8Alloc;
- Hll8Array<A>* hll8Array = hll8Alloc().allocate(1);
- hll8Alloc().construct(hll8Array, lgConfigK, srcHllArr.isStartFullSize());
+ Hll8Array<A>* hll8Array = new (hll8Alloc().allocate(1)) Hll8Array<A>(lgConfigK, srcHllArr.isStartFullSize());
hll8Array->putOutOfOrderFlag(srcHllArr.isOutOfOrderFlag());
int numZeros = 1 << lgConfigK;
@@ -266,4 +251,4 @@ Hll8Array<A>* HllSketchImplFactory<A>::convertToHll8(const HllArray<A>& srcHllAr
//#include "Hll6Array-internal.hpp"
//#include "Hll8Array-internal.hpp"
-#endif /* _HLLSKETCHIMPLFACTORY_HPP_ */
\ No newline at end of file
+#endif /* _HLLSKETCHIMPLFACTORY_HPP_ */
diff --git a/hll/include/HllUnion-internal.hpp b/hll/include/HllUnion-internal.hpp
index 8eb571f..cb5d67a 100644
--- a/hll/include/HllUnion-internal.hpp
+++ b/hll/include/HllUnion-internal.hpp
@@ -35,8 +35,7 @@ template<typename A>
HllUnion<A>::HllUnion(const int lgMaxK)
: lgMaxK(HllUtil<A>::checkLgK(lgMaxK)) {
typedef typename std::allocator_traits<A>::template rebind_alloc<HllSketch<A>> AllocHllSketch;
- gadget = AllocHllSketch().allocate(1);
- AllocHllSketch().construct(gadget, lgMaxK, TgtHllType::HLL_8);
+ gadget = new (AllocHllSketch().allocate(1)) HllSketch<A>(lgMaxK, TgtHllType::HLL_8);
}
template<typename A>
@@ -77,7 +76,7 @@ HllUnion<A>::~HllUnion() {
}
template<typename A>
-static std::ostream& operator<<(std::ostream& os, HllUnion<A>& hllUnion) {
+static std::ostream& operator<<(std::ostream& os, const HllUnion<A>& hllUnion) {
return hllUnion.to_string(os, true, true, false, false);
}
@@ -515,4 +514,4 @@ void HllUnion<A>::unionImpl(HllSketchImpl<A>* incomingImpl, const int lgMaxK) {
}
-#endif // _HLLUNION_INTERNAL_HPP_
\ No newline at end of file
+#endif // _HLLUNION_INTERNAL_HPP_
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org