Skip to content

Commit

Permalink
Merge pull request #7005
Browse files Browse the repository at this point in the history
249eae5 Allow byte_stream->byte_slice conversion to shrink unused buffer space (Lee Clagett)
  • Loading branch information
luigi1111 committed Mar 20, 2021
2 parents 8d7c113 + 249eae5 commit 7bf89dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion contrib/epee/include/byte_slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace epee
explicit byte_slice(std::string&& buffer);

//! Convert `stream` into a slice with zero allocations.
explicit byte_slice(byte_stream&& stream) noexcept;
explicit byte_slice(byte_stream&& stream, bool shrink = true);

byte_slice(byte_slice&& source) noexcept;
~byte_slice() noexcept = default;
Expand Down
24 changes: 20 additions & 4 deletions contrib/epee/src/byte_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
#include "byte_slice.h"
#include "byte_stream.h"

namespace
{
const std::size_t page_size = 4096;
}

namespace epee
{
struct byte_slice_data
Expand Down Expand Up @@ -173,16 +178,27 @@ namespace epee
: byte_slice(adapt_buffer{}, std::move(buffer))
{}

byte_slice::byte_slice(byte_stream&& stream) noexcept
byte_slice::byte_slice(byte_stream&& stream, const bool shrink)
: storage_(nullptr), portion_(stream.data(), stream.size())
{
if (stream.size())
if (portion_.size())
{
std::uint8_t* const data = stream.take_buffer().release() - sizeof(raw_byte_slice);
byte_buffer buf;
if (shrink && page_size <= stream.available())
{
buf = byte_buffer_resize(stream.take_buffer(), portion_.size());
if (!buf)
throw std::bad_alloc{};
portion_ = {buf.get(), portion_.size()};
}
else // no need to shrink buffer
buf = stream.take_buffer();

std::uint8_t* const data = buf.release() - sizeof(raw_byte_slice);
new (data) raw_byte_slice{};
storage_.reset(reinterpret_cast<raw_byte_slice*>(data));
}
else
else // empty stream
portion_ = nullptr;
}

Expand Down
4 changes: 3 additions & 1 deletion tests/unit_tests/epee_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,13 @@ TEST(ByteStream, ToByteSlice)

epee::byte_stream stream;

stream.reserve(128*1024);
stream.write(source);
EXPECT_EQ(sizeof(source), stream.size());
EXPECT_EQ(128*1024, stream.capacity());
EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()}));

const epee::byte_slice slice{std::move(stream)};
const epee::byte_slice slice{std::move(stream), true};
EXPECT_EQ(0u, stream.size());
EXPECT_EQ(0u, stream.available());
EXPECT_EQ(0u, stream.capacity());
Expand Down

0 comments on commit 7bf89dc

Please sign in to comment.