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

Add reserve() methods to dynamic buffers #1345

Closed
mst7555 opened this issue Dec 3, 2018 · 5 comments
Closed

Add reserve() methods to dynamic buffers #1345

mst7555 opened this issue Dec 3, 2018 · 5 comments
Labels

Comments

@mst7555
Copy link

mst7555 commented Dec 3, 2018

It seems that async_read_some triggers the handler for every 0.5k chunk of data - is there a way to control size of it ?

@vinniefalco
Copy link
Member

Are you talking about http::async_read_some? That can be indirectly controlled by the choice of dynamic buffer. Which one are you using?

@mst7555
Copy link
Author

mst7555 commented Dec 4, 2018

Yes it is http::async_read_some and I currently use boost::beast::flat_buffer.

@vinniefalco
Copy link
Member

vinniefalco commented Dec 4, 2018

We have confirmed the problem you are experiencing. What you need is to call flat_buffer::reserve to ensure a certain minimum capacity before calling async_read_some (or async_read). Unfortunately, that function does not exist yet. But you can use this function as a work-around:

/** Guarantee a minimum capacity.

    Any buffers obtained using @ref data or @prepare will be invalidated.

    @param n The desired minimum capacity. The actual capacity upon return may
        be greater than this value.
*/
template<class Allocator>
void reserve(boost::beast::basic_flat_buffer<Allocator>& b, std::size_t n)
{
    if(n < b.capacity())
        return;
    if(b.size() > 0 && b.size() >= b.capacity())
        return;
    if(n >= b.max_size())
        return;
    b.prepare(n - b.size());
    BOOST_ASSERT(b.capacity() >= n);
}

I'll add this as a member function but it won't ship until Boost 1.70

@vinniefalco vinniefalco changed the title async_read_some chunk size Add reserve() methods to dynamic buffers Dec 4, 2018
@mst7555
Copy link
Author

mst7555 commented Dec 4, 2018

Great! Thanks for a very quick response! I've tried your code and modified it to make it work.
First problem was bad parameter type of the buffer - there should be:
void reserve(boost::beast::basic_flat_buffer<Allocator>& b, std::size_t n)
Another thing was that initially both b.size() and b.capacity() for flat_buffer returns 0 what fulfils 2nd if condition preventing reserve() from doing anything. I've changed it to:
if (b.size() > 0 && b.size() >= b.capacity())
And everything started to work as expected!
Cheers!

@vinniefalco
Copy link
Member

I've edited my response to incorporate your changes, thanks!

vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 7, 2018
fix boostorg#1345

* Add reserve() member
* Add max_size() member
* Revise documentation
* Specify exception safety
vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 8, 2018
fix boostorg#1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Respect Allocator max_size
* Specify exception safety
vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 8, 2018
fix boostorg#1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Respect Allocator max_size
* Specify exception safety
vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 8, 2018
fix boostorg#1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Respect Allocator max_size
* Specify exception safety
vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 8, 2018
fix boostorg#1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Respect Allocator max_size
* Specify exception safety
vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 8, 2018
fix boostorg#1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Add shrink_to_fit() member
* Respect Allocator max_size
* Specify exception safety
vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 9, 2018
fix boostorg#1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Add shrink_to_fit() member
* Respect Allocator max_size
* Specify exception safety
vinniefalco added a commit to vinniefalco/beast that referenced this issue Dec 13, 2018
fix boostorg#1345

* Revise documentation
* Add reserve() member
* Add max_size() member
* Add shrink_to_fit() member
* Respect Allocator max_size
* Specify exception safety
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants