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

Various micro perf fixes and cleanup found while implementing CN overrides so far #818

Merged
merged 16 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Release/include/cpprest/asyncrt_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace conversions

#if defined(__ANDROID__)
template<class T>
inline std::string to_string(const T& t)
inline std::string to_string(const T t)
{
std::ostringstream os;
os.imbue(std::locale::classic());
Expand All @@ -238,16 +238,16 @@ namespace conversions
#endif

template<class T>
inline utility::string_t to_string_t(T&& t)
inline utility::string_t to_string_t(const T t)
{
#ifdef _UTF16_STRINGS
using std::to_wstring;
return to_wstring(std::forward<T>(t));
return to_wstring(t);
#else
#if !defined(__ANDROID__)
using std::to_string;
#endif
return to_string(std::forward<T>(t));
return to_string(t);
#endif
}

Expand Down
11 changes: 0 additions & 11 deletions Release/include/cpprest/details/web_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@

namespace web
{

namespace http { namespace client { namespace details {
class winhttp_client;
class winrt_client;
class asio_context;
}}}
namespace websockets { namespace client { namespace details {
class winrt_callback_client;
class wspp_callback_client;
}}}

namespace details
{

Expand Down
2 changes: 1 addition & 1 deletion Release/include/cpprest/streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ class type_parser<CharType, std::enable_if_t<sizeof(CharType) == 1, std::basic_s

static pplx::task<std::wstring> parse(streams::streambuf<CharType> buffer)
{
return base::_parse_input<std::basic_string<char>,std::basic_string<wchar_t>>(buffer, _accept_char, _extract_result);
return base::template _parse_input<std::basic_string<char>,std::basic_string<wchar_t>>(buffer, _accept_char, _extract_result);
}

private:
Expand Down
50 changes: 25 additions & 25 deletions Release/src/http/client/http_client_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

#include "stdafx.h"

#undef min
#undef max

#include "cpprest/asyncrt_utils.h"
#include "../common/internal_http_helpers.h"

Expand Down Expand Up @@ -82,7 +79,7 @@ using utility::conversions::details::to_string;
using std::to_string;
#endif

#define CRLF std::string("\r\n")
static const std::string CRLF("\r\n");

namespace web { namespace http
{
Expand Down Expand Up @@ -173,14 +170,15 @@ class asio_connection
template <typename Iterator, typename Handler>
void async_connect(const Iterator &begin, const Handler &handler)
{
std::unique_lock<std::mutex> lock(m_socket_lock);
if (!m_closed)
m_socket.async_connect(begin, handler);
else
{
lock.unlock();
handler(boost::asio::error::operation_aborted);
}
std::lock_guard<std::mutex> lock(m_socket_lock);
if (!m_closed) {
m_socket.async_connect(begin, handler);
return;
}
} // unlock

handler(boost::asio::error::operation_aborted);
}

template <typename HandshakeHandler, typename CertificateHandler>
Expand Down Expand Up @@ -299,7 +297,7 @@ class asio_connection
/// }
/// </code>
/// </remarks>
class asio_connection_pool : public std::enable_shared_from_this<asio_connection_pool>
class asio_connection_pool final : public std::enable_shared_from_this<asio_connection_pool>
{
public:
asio_connection_pool() : m_pool_epoch_timer(crossplat::threadpool::shared_instance().service())
Expand Down Expand Up @@ -356,24 +354,25 @@ class asio_connection_pool : public std::enable_shared_from_this<asio_connection
if (!pool)
return;
auto& self = *pool;
auto& connections = self.m_connections;

std::lock_guard<std::mutex> lock(self.m_lock);
if (self.m_prev_epoch == self.m_epoch)
{
self.m_connections.clear();
connections.clear();
self.is_timer_running = false;
return;
}
else
{
auto prev_epoch = self.m_prev_epoch;
auto erase_end = std::find_if(self.m_connections.begin(), self.m_connections.end(),
auto erase_end = std::find_if(connections.begin(), connections.end(),
[prev_epoch](std::pair<uint64_t, std::shared_ptr<asio_connection>>& p)
{
return p.first > prev_epoch;
});

self.m_connections.erase(self.m_connections.begin(), erase_end);
connections.erase(connections.begin(), erase_end);
start_epoch_interval(pool);
}
});
Expand All @@ -398,7 +397,7 @@ class asio_client final : public _http_client_communicator
, m_start_with_ssl(base_uri().scheme() == U("https") && !this->client_config().proxy().is_specified())
{}

void send_request(const std::shared_ptr<request_context> &request_ctx) override;
virtual void send_request(const std::shared_ptr<request_context> &request_ctx) override;

void release_connection(std::shared_ptr<asio_connection>& conn)
{
Expand Down Expand Up @@ -428,7 +427,7 @@ class asio_client final : public _http_client_communicator
const bool m_start_with_ssl;
};

class asio_context : public request_context, public std::enable_shared_from_this<asio_context>
class asio_context final : public request_context, public std::enable_shared_from_this<asio_context>
{
friend class asio_client;
public:
Expand Down Expand Up @@ -461,7 +460,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
return ctx;
}

class ssl_proxy_tunnel : public std::enable_shared_from_this<ssl_proxy_tunnel>
class ssl_proxy_tunnel final : public std::enable_shared_from_this<ssl_proxy_tunnel>
{
public:
ssl_proxy_tunnel(std::shared_ptr<asio_context> context, std::function<void(std::shared_ptr<asio_context>)> ssl_tunnel_established)
Expand All @@ -478,14 +477,15 @@ class asio_context : public request_context, public std::enable_shared_from_this

const auto &base_uri = m_context->m_http_client->base_uri();
const auto &host = utility::conversions::to_utf8string(base_uri.host());
const auto &port = base_uri.port();
const int portRaw = base_uri.port();
const int port = (portRaw != 0) ? portRaw : 443;

std::ostream request_stream(&m_request);
request_stream.imbue(std::locale::classic());

request_stream << "CONNECT " << host << ":" << ((port != 0) ? port : 443) << " HTTP/1.1" << CRLF;
request_stream << "Host: " << host << ":" << ((port != 0) ? port : 443) << CRLF;
request_stream << "Proxy-Connection: Keep-Alive" << CRLF;
request_stream << "CONNECT " << host << ":" << port << " HTTP/1.1\r\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

May I ask what is the reason for using both CRLF and hardcoded "\r\n" in this code snippet?

Copy link
Member Author

Choose a reason for hiding this comment

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

When CRLF was the entire constant, I left it alone; worst case it saves a strlen on the crlf. Where CRLF was causing an additional trip through <<, I removed it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand, thx for reply.

request_stream << "Host: " << host << ":" << port << CRLF;
request_stream << "Proxy-Connection: Keep-Alive\r\n";

if(m_context->m_http_client->client_config().proxy().credentials().is_set())
{
Expand Down Expand Up @@ -683,7 +683,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
request_stream.imbue(std::locale::classic());
const auto &host = utility::conversions::to_utf8string(base_uri.host());

request_stream << utility::conversions::to_utf8string(method) << " " << utility::conversions::to_utf8string(encoded_resource) << " " << "HTTP/1.1" << CRLF;
request_stream << utility::conversions::to_utf8string(method) << " " << utility::conversions::to_utf8string(encoded_resource) << " " << "HTTP/1.1\r\n";

int port = base_uri.port();

Expand Down Expand Up @@ -751,7 +751,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
request_stream << utility::conversions::to_utf8string(::web::http::details::flatten_http_headers(ctx->m_request.headers()));
request_stream << extra_headers;
// Enforce HTTP connection keep alive (even for the old HTTP/1.0 protocol).
request_stream << "Connection: Keep-Alive" << CRLF << CRLF;
request_stream << "Connection: Keep-Alive\r\n\r\n";

// Start connection timeout timer.
if (!ctx->m_timer.has_started())
Expand Down Expand Up @@ -1323,7 +1323,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
}
else
{
async_read_until_buffersize(octets + CRLF.size(), // + 2 for crlf
async_read_until_buffersize(octets + CRLF.size(),
boost::bind(&asio_context::handle_chunk, shared_from_this(), boost::asio::placeholders::error, octets));
}
}
Expand Down
23 changes: 8 additions & 15 deletions Release/src/http/client/http_client_winhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
#include <VersionHelpers.h>
#endif

#undef min
#undef max

namespace web
{
namespace http
Expand Down Expand Up @@ -185,7 +182,7 @@ enum msg_body_type
};

// Additional information necessary to track a WinHTTP request.
class winhttp_request_context : public request_context
class winhttp_request_context final : public request_context
{
public:

Expand Down Expand Up @@ -248,7 +245,7 @@ class winhttp_request_context : public request_context
std::shared_ptr<winhttp_request_context> m_self_reference;
memory_holder m_body_data;

virtual void cleanup()
void cleanup()
{
if(m_request_handle != nullptr)
{
Expand All @@ -260,7 +257,7 @@ class winhttp_request_context : public request_context

protected:

virtual void finish()
virtual void finish() override
{
request_context::finish();
assert(m_self_reference != nullptr);
Expand Down Expand Up @@ -346,7 +343,7 @@ struct ie_proxy_config : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
};

// WinHTTP client.
class winhttp_client : public _http_client_communicator
class winhttp_client final : public _http_client_communicator
{
public:
winhttp_client(http::uri address, http_client_config client_config)
Expand Down Expand Up @@ -502,17 +499,13 @@ class winhttp_client : public _http_client_communicator
}
else
{
proxy_str = uri.host();
if (uri.port() > 0)
{
utility::ostringstream_t ss;
ss.imbue(std::locale::classic());
ss << uri.host() << _XPLATSTR(":") << uri.port();
proxy_str = ss.str();
}
else
{
proxy_str = uri.host();
proxy_str.push_back(_XPLATSTR(':'));
proxy_str.append(::utility::conversions::details::to_string_t(uri.port()));
}

proxy_name = proxy_str.c_str();
}
}
Expand Down
15 changes: 6 additions & 9 deletions Release/src/http/client/http_client_winrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ using namespace std;
using namespace Platform;
using namespace Microsoft::WRL;

#undef min
#undef max

namespace web
{
namespace http
Expand All @@ -41,7 +38,7 @@ namespace details
{

// Additional information necessary to track a WinRT request.
class winrt_request_context : public request_context
class winrt_request_context final : public request_context
{
public:

Expand All @@ -63,7 +60,7 @@ class winrt_request_context : public request_context
};

// Implementation of IXMLHTTPRequest2Callback.
class HttpRequestCallback :
class HttpRequestCallback final :
public RuntimeClass<RuntimeClassFlags<ClassicCom>, IXMLHTTPRequest2Callback, FtmBase>
{
public:
Expand Down Expand Up @@ -190,7 +187,7 @@ class HttpRequestCallback :
/// read and write operations. The I/O will be done off the UI thread, so there is no risk
/// of causing the UI to become unresponsive.
/// </remarks>
class IRequestStream
class IRequestStream final
: public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<ClassicCom>, ISequentialStream>
{
public:
Expand Down Expand Up @@ -279,7 +276,7 @@ class IRequestStream
/// read and write operations. The I/O will be done off the UI thread, so there is no risk
/// of causing the UI to become unresponsive.
/// </remarks>
class IResponseStream
class IResponseStream final
: public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<ClassicCom>, ISequentialStream>
{
public:
Expand Down Expand Up @@ -353,7 +350,7 @@ class IResponseStream
};

// WinRT client.
class winrt_client : public _http_client_communicator
class winrt_client final : public _http_client_communicator
{
public:
winrt_client(http::uri&& address, http_client_config&& client_config)
Expand All @@ -379,7 +376,7 @@ class winrt_client : public _http_client_communicator
protected:

// Start sending request.
void send_request(_In_ const std::shared_ptr<request_context> &request)
virtual void send_request(_In_ const std::shared_ptr<request_context> &request) override
{
http_request &msg = request->m_request;
auto winrt_context = std::static_pointer_cast<winrt_request_context>(request);
Expand Down
3 changes: 0 additions & 3 deletions Release/src/http/common/http_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#include "stdafx.h"
#include "../common/internal_http_helpers.h"

#undef min
#undef max

using namespace web;
using namespace utility;
using namespace concurrency;
Expand Down
7 changes: 2 additions & 5 deletions Release/src/http/listener/http_server_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/
#include "stdafx.h"

#undef min
#undef max

#include <boost/algorithm/string/find.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/asio/read_until.hpp>
Expand Down Expand Up @@ -1033,7 +1030,7 @@ will_deref_and_erase_t asio_server_connection::cancel_sending_response_with_erro
{
auto * context = static_cast<linux_request_context*>(response._get_server_context());
context->m_response_completed.set_exception(eptr);

// always terminate the connection since error happens
return finish_request_response();
}
Expand All @@ -1044,7 +1041,7 @@ will_deref_and_erase_t asio_server_connection::handle_write_chunked_response(con
{
return handle_response_written(response, ec);
}

auto readbuf = response._get_impl()->instream().streambuf();
if (readbuf.is_eof())
{
Expand Down
Loading