samedi 16 février 2019

boost beast async_write increases memory footprint dramatically

I am currently experimenting with the boost beast library and now very surprised by it's memory footprint. I've found out by using three different response types (string, file, dynamic) the program size grows up to 6Mb.

To get closer to the cause, I took the small server example from the library and reduced it to the following steps:

class http_connection : public std::enable_shared_from_this<http_connection>
{
public:
    http_connection(tcp::socket socket) : socket_(std::move(socket)) { }
    void start() {
        read_request();
    }

private:
    tcp::socket socket_;
    beast::flat_buffer buffer_{8192};
    http::request<http::dynamic_body> request_;

    void read_request() {
        auto self = shared_from_this();
        http::async_read(
            socket_, buffer_, request_,
            [self](beast::error_code ec,
                std::size_t bytes_transferred)
            {
                    self->write_response(std::make_shared<http::response<http::dynamic_body>>());
                    self->write_response(std::make_shared<http::response<http::file_body>>());
                    self->write_response(std::make_shared<http::response<http::string_body>>(), true);
            });
    }

    template <class T>
    void write_response(std::shared_ptr<T> response, bool dostop=false) {

        auto self = shared_from_this();

        http::async_write(
            socket_,
            *response,
            [self,response,dostop](beast::error_code ec, std::size_t)
            {
                if (dostop)
                    self->socket_.shutdown(tcp::socket::shutdown_send, ec);
            });
    }
};

when I comment out the three self->write_response lines and compile the program and execute the size command on the result, I get:

   text    data     bss     dec     hex filename
 343474    1680    7408  352562   56132 small

When I remove the comment of the first write, then I get:

 864740    1714    7408  873862   d5586 small
   text    data     bss     dec     hex filename

After removing all comments the final size become:

   text    data     bss     dec     hex filename
1333510    1730    7408 1342648  147cb8 small

4,8M Feb 16 22:13 small*

The question now is:

Am I doing something wrong?

Is there a way to reduce the size?

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire