Skip to content

Commit 33f9073

Browse files
author
casaserv
committed
Timer in http_client linux implementation was allocated on the heap in a unique_ptr, even though it is used in all code paths. I changed this. Less code and saves a heap allocation per request now.
1 parent defb4f2 commit 33f9073

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ namespace web { namespace http
9191
request_context::report_error(errorcodeValue, message);
9292
}
9393

94-
std::unique_ptr<tcp::socket> m_socket;
94+
tcp::socket m_socket;
9595
std::unique_ptr<boost::asio::ssl::stream<tcp::socket>> m_ssl_stream;
9696
size_t m_known_size;
9797
size_t m_current_size;
9898
bool m_needChunked;
9999
bool m_timedout;
100100
boost::asio::streambuf m_body_buf;
101-
std::unique_ptr<boost::asio::deadline_timer> m_timer;
101+
boost::asio::deadline_timer m_timer;
102102

103103
template <typename socket_type>
104104
void shutdown_socket(socket_type &socket)
@@ -110,17 +110,9 @@ namespace web { namespace http
110110

111111
~linux_request_context()
112112
{
113-
if (m_timer)
114-
{
115-
m_timer->cancel();
116-
m_timer.reset();
117-
}
113+
m_timer.cancel();
118114

119-
if (m_socket)
120-
{
121-
shutdown_socket(*m_socket);
122-
m_socket.reset();
123-
}
115+
shutdown_socket(m_socket);
124116

125117
if (m_ssl_stream)
126118
{
@@ -144,11 +136,7 @@ namespace web { namespace http
144136
}
145137
else
146138
{
147-
auto sock = m_socket.get();
148-
if (sock != nullptr)
149-
{
150-
sock->cancel();
151-
}
139+
m_socket.cancel();
152140
}
153141
}
154142
}
@@ -160,6 +148,7 @@ namespace web { namespace http
160148
, m_needChunked(false)
161149
, m_timedout(false)
162150
, m_current_size(0)
151+
, m_timer(crossplat::threadpool::shared_instance().service())
163152
{
164153
}
165154

@@ -267,10 +256,9 @@ namespace web { namespace http
267256

268257
tcp::resolver::query query(host, utility::conversions::print_string(port));
269258

270-
ctx->m_timer.reset(new boost::asio::deadline_timer(m_io_service));
271259
const int secs = static_cast<int>(client_config().timeout().count());
272-
ctx->m_timer->expires_from_now(boost::posix_time::milliseconds(secs * 1000));
273-
ctx->m_timer->async_wait(boost::bind(&linux_request_context::cancel, ctx.get(), boost::asio::placeholders::error));
260+
ctx->m_timer.expires_from_now(boost::posix_time::milliseconds(secs * 1000));
261+
ctx->m_timer.async_wait(boost::bind(&linux_request_context::cancel, ctx.get(), boost::asio::placeholders::error));
274262

275263
m_resolver.async_resolve(query, boost::bind(&linux_client::handle_resolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator, ctx));
276264
}

0 commit comments

Comments
 (0)