Skip to content
Permalink
Browse files
Merge pull request #246 from apache/fix_allocation_in_to_string
use default allocator for temporary ostringstream
  • Loading branch information
AlexanderSaydakov committed Oct 19, 2021
2 parents e221e49 + f933c66 commit b51ec556bd0102d3c34ef03f81e3ad92fe968e95
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 44 deletions.
@@ -381,7 +381,9 @@ void cpc_sketch_alloc<A>::refresh_kxp(const uint64_t* bit_matrix) {

template<typename A>
string<A> cpc_sketch_alloc<A>::to_string() const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### CPC sketch summary:" << std::endl;
os << " lg_k : " << std::to_string(lg_k) << std::endl;
os << " seed hash : " << std::hex << compute_seed_hash(seed) << std::dec << std::endl;
@@ -392,14 +394,14 @@ string<A> cpc_sketch_alloc<A>::to_string() const {
os << " HIP estimate : " << hip_est_accum << std::endl;
os << " kxp : " << kxp << std::endl;
}
os << " intresting col : " << std::to_string(first_interesting_column) << std::endl;
os << " interesting col: " << std::to_string(first_interesting_column) << std::endl;
os << " table entries : " << surprising_value_table.get_num_items() << std::endl;
os << " window : " << (sliding_window.size() == 0 ? "not " : "") << "allocated" << std::endl;
if (sliding_window.size() > 0) {
os << " window offset : " << std::to_string(window_offset) << std::endl;
}
os << "### End sketch summary" << std::endl;
return os.str();
return string<A>(os.str().c_str(), sliding_window.get_allocator());
}

template<typename A>
@@ -421,7 +421,9 @@ void frequent_items_sketch<T, W, H, E, S, A>::check_size(uint8_t lg_cur_size, ui

template<typename T, typename W, typename H, typename E, typename S, typename A>
string<A> frequent_items_sketch<T, W, H, E, S, A>::to_string(bool print_items) const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### Frequent items sketch summary:" << std::endl;
os << " lg cur map size : " << (int) map.get_lg_cur_size() << std::endl;
os << " lg max map size : " << (int) map.get_lg_max_size() << std::endl;
@@ -444,7 +446,7 @@ string<A> frequent_items_sketch<T, W, H, E, S, A>::to_string(bool print_items) c
}
os << "### End items" << std::endl;
}
return os.str();
return string<A>(os.str().c_str(), map.get_allocator());
}

// version for integral signed type
@@ -246,7 +246,9 @@ string<A> hll_sketch_alloc<A>::to_string(const bool summary,
const bool detail,
const bool aux_detail,
const bool all) const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::stringstream os;
if (summary) {
os << "### HLL sketch summary:" << std::endl
<< " Log Config K : " << std::to_string(get_lg_config_k()) << std::endl
@@ -338,7 +340,7 @@ string<A> hll_sketch_alloc<A>::to_string(const bool summary,
}
}

return os.str();
return string<A>(os.str().c_str(), sketch_impl->getAllocator());
}

template<typename A>
@@ -1023,7 +1023,9 @@ void kll_sketch<T, C, S, A>::check_family_id(uint8_t family_id) {

template <typename T, typename C, typename S, typename A>
string<A> kll_sketch<T, C, S, A>::to_string(bool print_levels, bool print_items) const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### KLL sketch summary:" << std::endl;
os << " K : " << k_ << std::endl;
os << " min K : " << min_k_ << std::endl;
@@ -1069,7 +1071,7 @@ string<A> kll_sketch<T, C, S, A>::to_string(bool print_levels, bool print_items)
}
os << "### End sketch data" << std::endl;
}
return os.str();
return string<A>(os.str().c_str(), allocator_);
}

template <typename T, typename C, typename S, typename A>
@@ -653,7 +653,9 @@ void req_sketch<T, C, S, A>::compress() {

template<typename T, typename C, typename S, typename A>
string<A> req_sketch<T, C, S, A>::to_string(bool print_levels, bool print_items) const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### REQ sketch summary:" << std::endl;
os << " K : " << k_ << std::endl;
os << " High Rank Acc : " << (hra_ ? "true" : "false") << std::endl;
@@ -693,7 +695,7 @@ string<A> req_sketch<T, C, S, A>::to_string(bool print_levels, bool print_items)
}
os << "### End sketch data" << std::endl;
}
return os.str();
return string<A>(os.str().c_str(), allocator_);
}

template<typename T, typename C, typename S, typename A>
@@ -731,33 +731,39 @@ void var_opt_sketch<T,S,A>::update(T&& item, double weight) {

template<typename T, typename S, typename A>
string<A> var_opt_sketch<T,S,A>::to_string() const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
os << "### VarOpt SUMMARY: " << std::endl;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### VarOpt SUMMARY:" << std::endl;
os << " k : " << k_ << std::endl;
os << " h : " << h_ << std::endl;
os << " r : " << r_ << std::endl;
os << " weight_r : " << total_wt_r_ << std::endl;
os << " Current size : " << curr_items_alloc_ << std::endl;
os << " Resize factor: " << (1 << rf_) << std::endl;
os << "### END SKETCH SUMMARY" << std::endl;
return os.str();
return string<A>(os.str().c_str(), allocator_);
}

template<typename T, typename S, typename A>
string<A> var_opt_sketch<T,S,A>::items_to_string() const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### Sketch Items" << std::endl;
int idx = 0;
for (auto record : *this) {
os << idx << ": " << record.first << "\twt = " << record.second << std::endl;
++idx;
}
return os.str();
return string<A>(os.str().c_str(), allocator_);
}

template<typename T, typename S, typename A>
string<A> var_opt_sketch<T,S,A>::items_to_string(bool print_gap) const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### Sketch Items" << std::endl;
const uint32_t array_length = (n_ < k_ ? n_ : k_ + 1);
for (uint32_t i = 0, display_idx = 0; i < array_length; ++i) {
@@ -774,7 +780,7 @@ string<A> var_opt_sketch<T,S,A>::items_to_string(bool print_gap) const {
++display_idx;
}
}
return os.str();
return string<A>(os.str().c_str(), allocator_);
}

template<typename T, typename S, typename A>
@@ -295,14 +295,16 @@ void var_opt_union<T,S,A>::reset() {

template<typename T, typename S, typename A>
string<A> var_opt_union<T,S,A>::to_string() const {
std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
os << "### VarOpt Union SUMMARY: " << std::endl;
os << " . n : " << n_ << std::endl;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### VarOpt Union SUMMARY:" << std::endl;
os << " n : " << n_ << std::endl;
os << " Max k : " << max_k_ << std::endl;
os << " Gadget Summary: " << std::endl;
os << " Gadget Summary:" << std::endl;
os << gadget_.to_string();
os << "### END VarOpt Union SUMMARY: " << std::endl;
return os.str();
os << "### END VarOpt Union SUMMARY" << std::endl;
return string<A>(os.str().c_str(), gadget_.allocator_);
}

template<typename T, typename S, typename A>
@@ -131,8 +131,7 @@ class theta_sketch_alloc {
virtual const_iterator end() const = 0;

protected:
using ostrstream = std::basic_ostringstream<char, std::char_traits<char>, AllocChar<Allocator>>;
virtual void print_specifics(ostrstream& os) const = 0;
virtual void print_specifics(std::ostringstream& os) const = 0;
};

// forward declaration
@@ -288,8 +287,7 @@ class update_theta_sketch_alloc: public theta_sketch_alloc<Allocator> {
update_theta_sketch_alloc(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, uint64_t theta,
uint64_t seed, const Allocator& allocator);

using ostrstream = typename Base::ostrstream;
virtual void print_specifics(ostrstream& os) const;
virtual void print_specifics(std::ostringstream& os) const;
};

// compact sketch
@@ -377,8 +375,7 @@ class compact_theta_sketch_alloc: public theta_sketch_alloc<Allocator> {
uint64_t theta_;
std::vector<uint64_t, Allocator> entries_;

using ostrstream = typename Base::ostrstream;
virtual void print_specifics(ostrstream& os) const;
virtual void print_specifics(std::ostringstream& os) const;
};

template<typename Allocator>
@@ -59,7 +59,9 @@ double theta_sketch_alloc<A>::get_upper_bound(uint8_t num_std_devs) const {

template<typename A>
string<A> theta_sketch_alloc<A>::to_string(bool detail) const {
ostrstream os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### Theta sketch summary:" << std::endl;
os << " num retained entries : " << get_num_retained() << std::endl;
os << " seed hash : " << get_seed_hash() << std::endl;
@@ -80,7 +82,7 @@ string<A> theta_sketch_alloc<A>::to_string(bool detail) const {
}
os << "### End retained entries" << std::endl;
}
return os.str();
return string<A>(os.str().c_str(), get_allocator());
}

// update sketch
@@ -228,7 +230,7 @@ compact_theta_sketch_alloc<A> update_theta_sketch_alloc<A>::compact(bool ordered
}

template<typename A>
void update_theta_sketch_alloc<A>::print_specifics(ostrstream& os) const {
void update_theta_sketch_alloc<A>::print_specifics(std::ostringstream& os) const {
os << " lg nominal size : " << static_cast<int>(table_.lg_nom_size_) << std::endl;
os << " lg current size : " << static_cast<int>(table_.lg_cur_size_) << std::endl;
os << " resize factor : " << (1 << table_.rf_) << std::endl;
@@ -321,7 +323,7 @@ auto compact_theta_sketch_alloc<A>::end() const -> const_iterator {
}

template<typename A>
void compact_theta_sketch_alloc<A>::print_specifics(ostrstream&) const {}
void compact_theta_sketch_alloc<A>::print_specifics(std::ostringstream&) const {}

template<typename A>
void compact_theta_sketch_alloc<A>::serialize(std::ostream& os) const {
@@ -153,8 +153,7 @@ class tuple_sketch {
virtual const_iterator end() const = 0;

protected:
using ostrstream = std::basic_ostringstream<char, std::char_traits<char>, AllocChar<Allocator>>;
virtual void print_specifics(ostrstream& os) const = 0;
virtual void print_specifics(std::ostringstream& os) const = 0;

static uint16_t get_seed_hash(uint64_t seed);

@@ -344,8 +343,7 @@ class update_tuple_sketch: public tuple_sketch<Summary, Allocator> {
// for builder
update_tuple_sketch(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, uint64_t theta, uint64_t seed, const Policy& policy, const Allocator& allocator);

using ostrstream = typename Base::ostrstream;
virtual void print_specifics(ostrstream& os) const;
virtual void print_specifics(std::ostringstream& os) const;
};

// compact sketch
@@ -473,8 +471,7 @@ class compact_tuple_sketch: public tuple_sketch<Summary, Allocator> {
bool destroy_;
};

using ostrstream = typename Base::ostrstream;
virtual void print_specifics(ostrstream& os) const;
virtual void print_specifics(std::ostringstream& os) const;

};

@@ -53,7 +53,9 @@ double tuple_sketch<S, A>::get_upper_bound(uint8_t num_std_devs) const {

template<typename S, typename A>
string<A> tuple_sketch<S, A>::to_string(bool detail) const {
ostrstream os;
// Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
// The stream does not support passing an allocator instance, and alternatives are complicated.
std::ostringstream os;
os << "### Tuple sketch summary:" << std::endl;
os << " num retained entries : " << get_num_retained() << std::endl;
os << " seed hash : " << get_seed_hash() << std::endl;
@@ -74,7 +76,7 @@ string<A> tuple_sketch<S, A>::to_string(bool detail) const {
}
os << "### End retained entries" << std::endl;
}
return os.str();
return string<A>(os.str().c_str(), get_allocator());
}

// update sketch
@@ -238,7 +240,7 @@ compact_tuple_sketch<S, A> update_tuple_sketch<S, U, P, A>::compact(bool ordered
}

template<typename S, typename U, typename P, typename A>
void update_tuple_sketch<S, U, P, A>::print_specifics(ostrstream& os) const {
void update_tuple_sketch<S, U, P, A>::print_specifics(std::ostringstream& os) const {
os << " lg nominal size : " << (int) map_.lg_nom_size_ << std::endl;
os << " lg current size : " << (int) map_.lg_cur_size_ << std::endl;
os << " resize factor : " << (1 << map_.rf_) << std::endl;
@@ -554,7 +556,7 @@ auto compact_tuple_sketch<S, A>::end() const -> const_iterator {
}

template<typename S, typename A>
void compact_tuple_sketch<S, A>::print_specifics(ostrstream&) const {}
void compact_tuple_sketch<S, A>::print_specifics(std::ostringstream&) const {}

// builder

0 comments on commit b51ec55

Please sign in to comment.