@@ -353,8 +353,7 @@ size_t compact_tuple_sketch<S, A>::get_serialized_size_summaries_bytes(const SD&
353353template <typename S, typename A>
354354template <typename SerDe>
355355void compact_tuple_sketch<S, A>::serialize(std::ostream& os, const SerDe& sd) const {
356- const bool is_single_item = entries_.size () == 1 && !this ->is_estimation_mode ();
357- const uint8_t preamble_longs = this ->is_empty () || is_single_item ? 1 : this ->is_estimation_mode () ? 3 : 2 ;
356+ const uint8_t preamble_longs = this ->is_estimation_mode () ? 3 : this ->is_empty () || entries_.size () == 1 ? 1 : 2 ;
358357 write (os, preamble_longs);
359358 const uint8_t serial_version = SERIAL_VERSION ;
360359 write (os, serial_version);
@@ -373,28 +372,25 @@ void compact_tuple_sketch<S, A>::serialize(std::ostream& os, const SerDe& sd) co
373372 write (os, flags_byte);
374373 const uint16_t seed_hash = get_seed_hash ();
375374 write (os, seed_hash);
376- if (!this ->is_empty ()) {
377- if (!is_single_item) {
378- const uint32_t num_entries = static_cast <uint32_t >(entries_.size ());
379- write (os, num_entries);
380- const uint32_t unused32 = 0 ;
381- write (os, unused32);
382- if (this ->is_estimation_mode ()) {
383- write (os, this ->theta_ );
384- }
385- }
386- for (const auto & it: entries_) {
387- write (os, it.first );
388- sd.serialize (os, &it.second , 1 );
389- }
375+ if (preamble_longs > 1 ) {
376+ const uint32_t num_entries = static_cast <uint32_t >(entries_.size ());
377+ write (os, num_entries);
378+ const uint32_t unused32 = 0 ;
379+ write (os, unused32);
380+ }
381+ if (this ->is_estimation_mode ()) {
382+ write (os, this ->theta_ );
383+ }
384+ for (const auto & it: entries_) {
385+ write (os, it.first );
386+ sd.serialize (os, &it.second , 1 );
390387 }
391388}
392389
393390template <typename S, typename A>
394391template <typename SerDe>
395392auto compact_tuple_sketch<S, A>::serialize(unsigned header_size_bytes, const SerDe& sd) const -> vector_bytes {
396- const bool is_single_item = entries_.size () == 1 && !this ->is_estimation_mode ();
397- const uint8_t preamble_longs = this ->is_empty () || is_single_item ? 1 : this ->is_estimation_mode () ? 3 : 2 ;
393+ const uint8_t preamble_longs = this ->is_estimation_mode () ? 3 : this ->is_empty () || entries_.size () == 1 ? 1 : 2 ;
398394 const size_t size = header_size_bytes + sizeof (uint64_t ) * preamble_longs
399395 + sizeof (uint64_t ) * entries_.size () + get_serialized_size_summaries_bytes (sd);
400396 vector_bytes bytes (size, 0 , entries_.get_allocator ());
@@ -418,19 +414,17 @@ auto compact_tuple_sketch<S, A>::serialize(unsigned header_size_bytes, const Ser
418414 ptr += copy_to_mem (flags_byte, ptr);
419415 const uint16_t seed_hash = get_seed_hash ();
420416 ptr += copy_to_mem (seed_hash, ptr);
421- if (!this ->is_empty ()) {
422- if (!is_single_item) {
423- const uint32_t num_entries = static_cast <uint32_t >(entries_.size ());
424- ptr += copy_to_mem (num_entries, ptr);
425- ptr += sizeof (uint32_t ); // unused
426- if (this ->is_estimation_mode ()) {
427- ptr += copy_to_mem (theta_, ptr);
428- }
429- }
430- for (const auto & it: entries_) {
431- ptr += copy_to_mem (it.first , ptr);
432- ptr += sd.serialize (ptr, end_ptr - ptr, &it.second , 1 );
433- }
417+ if (preamble_longs > 1 ) {
418+ const uint32_t num_entries = static_cast <uint32_t >(entries_.size ());
419+ ptr += copy_to_mem (num_entries, ptr);
420+ ptr += sizeof (uint32_t ); // unused
421+ }
422+ if (this ->is_estimation_mode ()) {
423+ ptr += copy_to_mem (theta_, ptr);
424+ }
425+ for (const auto & it: entries_) {
426+ ptr += copy_to_mem (it.first , ptr);
427+ ptr += sd.serialize (ptr, end_ptr - ptr, &it.second , 1 );
434428 }
435429 return bytes;
436430}
0 commit comments