From 53e3f490031e33b035081bb2a92ccbeb00fad027 Mon Sep 17 00:00:00 2001 From: Marc Fehling Date: Sun, 13 Dec 2020 15:41:03 -0700 Subject: [PATCH] Remove temporary string objects in Utilities::(un)pack. --- include/deal.II/base/utilities.h | 101 ++++++++----------------- source/base/utilities.cc | 3 +- tests/base/utilities_pack_unpack_06.cc | 4 +- 3 files changed, 34 insertions(+), 74 deletions(-) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 8bb17607d1..bd88309c75 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -45,15 +45,15 @@ DEAL_II_DISABLE_EXTRA_DIAGNOSTICS #include #include #include +#include +#include +#include #include #include #include #ifdef DEAL_II_WITH_ZLIB -# include # include -# include -# include #endif DEAL_II_ENABLE_EXTRA_DIAGNOSTICS @@ -1183,9 +1183,6 @@ namespace Utilities std::vector &dest_buffer, const bool allow_compression) { - // the data is never compressed when we can't use zlib. - (void)allow_compression; - std::size_t size = 0; // see if the object is small and copyable via memcpy. if so, use @@ -1217,31 +1214,21 @@ namespace Utilities // use buffer as the target of a compressing // stream into which we serialize the current object const std::size_t previous_size = dest_buffer.size(); + { + boost::iostreams::filtering_ostreambuf fosb; #ifdef DEAL_II_WITH_ZLIB - if (allow_compression) - { - boost::iostreams::filtering_ostream out; - out.push( - boost::iostreams::gzip_compressor(boost::iostreams::gzip_params( - boost::iostreams::gzip::default_compression))); - out.push(boost::iostreams::back_inserter(dest_buffer)); - - boost::archive::binary_oarchive archive(out); - archive << object; - out.flush(); - } - else + if (allow_compression) + fosb.push(boost::iostreams::gzip_compressor()); +#else + (void)allow_compression; #endif - { - std::ostringstream out; - boost::archive::binary_oarchive archive(out); - archive << object; - - const std::string s = out.str(); - dest_buffer.reserve(dest_buffer.size() + s.size()); - std::move(s.begin(), s.end(), std::back_inserter(dest_buffer)); - } + fosb.push(boost::iostreams::back_inserter(dest_buffer)); + boost::archive::binary_oarchive boa(fosb); + boa << object; + // the stream object has to be destroyed before the return statement + // to ensure that all data has been written in the buffer + } size = dest_buffer.size() - previous_size; } @@ -1267,9 +1254,6 @@ namespace Utilities { T object; - // the data is never compressed when we can't use zlib. - (void)allow_compression; - // see if the object is small and copyable via memcpy. if so, use // this fast path. otherwise, we have to go through the BOOST // serialization machinery @@ -1292,29 +1276,18 @@ namespace Utilities } else { - std::string decompressed_buffer; - - // first decompress the buffer + // decompress the buffer section into the object + boost::iostreams::filtering_istreambuf fisb; #ifdef DEAL_II_WITH_ZLIB if (allow_compression) - { - boost::iostreams::filtering_ostream decompressing_stream; - decompressing_stream.push(boost::iostreams::gzip_decompressor()); - decompressing_stream.push( - boost::iostreams::back_inserter(decompressed_buffer)); - decompressing_stream.write(&*cbegin, std::distance(cbegin, cend)); - } - else + fisb.push(boost::iostreams::gzip_decompressor()); +#else + (void)allow_compression; #endif - { - decompressed_buffer.assign(cbegin, cend); - } - - // then restore the object from the buffer - std::istringstream in(decompressed_buffer); - boost::archive::binary_iarchive archive(in); + fisb.push(boost::iostreams::array_source(&*cbegin, &*cend)); - archive >> object; + boost::archive::binary_iarchive bia(fisb); + bia >> object; } return object; @@ -1357,30 +1330,18 @@ namespace Utilities } else { - std::string decompressed_buffer; - - // first decompress the buffer - (void)allow_compression; + // decompress the buffer section into the object + boost::iostreams::filtering_istreambuf fisb; #ifdef DEAL_II_WITH_ZLIB if (allow_compression) - { - boost::iostreams::filtering_ostream decompressing_stream; - decompressing_stream.push(boost::iostreams::gzip_decompressor()); - decompressing_stream.push( - boost::iostreams::back_inserter(decompressed_buffer)); - decompressing_stream.write(&*cbegin, std::distance(cbegin, cend)); - } - else + fisb.push(boost::iostreams::gzip_decompressor()); +#else + (void)allow_compression; #endif - { - decompressed_buffer.assign(cbegin, cend); - } - - // then restore the object from the buffer - std::istringstream in(decompressed_buffer); - boost::archive::binary_iarchive archive(in); + fisb.push(boost::iostreams::array_source(&*cbegin, &*cend)); - archive >> unpacked_object; + boost::archive::binary_iarchive bia(fisb); + bia >> unpacked_object; } } diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 1de6dc892f..f687aa26a9 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -398,8 +398,7 @@ namespace Utilities std::stringstream origin(input); bio::filtering_streambuf out; - out.push(bio::gzip_compressor( - bio::gzip_params(boost::iostreams::gzip::default_compression))); + out.push(bio::gzip_compressor()); out.push(origin); bio::copy(out, compressed); diff --git a/tests/base/utilities_pack_unpack_06.cc b/tests/base/utilities_pack_unpack_06.cc index 9811e2bc73..9071725c9c 100644 --- a/tests/base/utilities_pack_unpack_06.cc +++ b/tests/base/utilities_pack_unpack_06.cc @@ -149,7 +149,7 @@ check(const double (&array)[N], const Point(&point)) point_uncompressed.cend(), true); } - catch (const boost::archive::archive_exception &) + catch (const boost::iostreams::gzip_error &) { deallog << "unpacking uncompressed point with decompression failed!" << std::endl; @@ -162,7 +162,7 @@ check(const double (&array)[N], const Point(&point)) forbidden, true); } - catch (const boost::archive::archive_exception &) + catch (const boost::iostreams::gzip_error &) { deallog << "unpacking uncompressed array with decompression failed!" << std::endl; -- 2.39.5