Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow byte_stream->byte_slice conversion to shrink unused buffer space #7005

Merged

Conversation

vtnerd
Copy link
Contributor

@vtnerd vtnerd commented Nov 10, 2020

This allows for the optional shrinkage of the byte_stream buffer when converting to an immutable byte_slice. This uses realloc so in the bulk of calls this can be done without a copy. Larger buffers are likely to be "overcommited" by OSes anyway, so it frequently could be a NOP. Its still beneficial given that Monero supports 32-bit builds, where the address space reclamation can be beneficial.

: storage_(nullptr), portion_(stream.data(), stream.size())
{
if (stream.size())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From cecca7a0fe43044e8f163a748347868f38b1b5e6 Mon Sep 17 00:00:00 2001
From: anon <anon [at] nowhere>
Date: Sat, 20 Feb 2021 19:56:02 +0000
Subject: [PATCH] byte_slice: simplify

---
 contrib/epee/src/byte_slice.cpp | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/contrib/epee/src/byte_slice.cpp b/contrib/epee/src/byte_slice.cpp
index 453b63a4c..2e8a2f820 100644
--- a/contrib/epee/src/byte_slice.cpp
+++ b/contrib/epee/src/byte_slice.cpp
@@ -181,24 +181,20 @@ namespace epee
   byte_slice::byte_slice(byte_stream&& stream, const bool shrink)
     : storage_(nullptr), portion_(stream.data(), stream.size())
   {
-    if (portion_.size())
+    if (stream.size())
     {
-      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()};
+        auto buf = byte_buffer_resize(stream.take_buffer(), portion_.size());
+        if (!buf)
+          throw std::bad_alloc{};
+        portion_ = {buf.get(), portion_.size()};
+        storage_.reset(new (buf.release() - sizeof(raw_byte_slice)) raw_byte_slice{});
       }
-      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
+        storage_.reset(new (stream.take_buffer().release() - sizeof(raw_byte_slice)) raw_byte_slice{});
     }
-    else // empty stream
+    else
       portion_ = nullptr;
   }
 
-- 
2.30.1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this an improvement? It ends up duplicating the placement new call with no obvious benefit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above patch can be ignored if there is no obvious benefit.

Copy link
Collaborator

@perfect-daemon perfect-daemon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

@luigi1111 luigi1111 merged commit 7bf89dc into monero-project:master Mar 20, 2021
@vtnerd vtnerd deleted the improve/byte_slice_reallocation branch April 1, 2024 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants