Skip to content

Commit 89c1fc9

Browse files
committed
boost serialisation (read path zerocopy)
1 parent 1015d2e commit 89c1fc9

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

include/rfl/boost_serialization/read.hpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <boost/archive/binary_oarchive.hpp>
66
#include <boost/serialization/string.hpp>
77
#include <istream>
8-
#include <sstream>
8+
#include <streambuf>
99
#include <string>
1010

1111
#include "../Processors.hpp"
@@ -26,6 +26,15 @@ auto read_from_archive(IArchive& _ar) {
2626
return Parser<IArchive, OArchive, T, Processors<Ps...>>::read(r, var);
2727
}
2828

29+
/// A read-only streambuf that wraps an existing memory buffer without copying.
30+
class MemBuf : public std::streambuf {
31+
public:
32+
MemBuf(const char* _data, size_t _size) {
33+
auto* p = const_cast<char*>(_data);
34+
setg(p, p, p + _size);
35+
}
36+
};
37+
2938
} // namespace detail
3039

3140
/// Reads from an existing Boost input archive.
@@ -39,8 +48,8 @@ template <class T, class... Ps>
3948
Result<internal::wrap_in_rfl_array_t<T>> read(
4049
const concepts::ByteLike auto* _bytes, const size_t _size) {
4150
try {
42-
std::string str(reinterpret_cast<const char*>(_bytes), _size);
43-
std::istringstream stream(str);
51+
detail::MemBuf buf(reinterpret_cast<const char*>(_bytes), _size);
52+
std::istream stream(&buf);
4453
boost::archive::binary_iarchive ar(
4554
stream, boost::archive::no_header | boost::archive::no_tracking);
4655
return detail::read_from_archive<T, boost::archive::binary_iarchive,

0 commit comments

Comments
 (0)