Skip to content

Commit 14adc2e

Browse files
BillyONealras0219-msft
authored andcommitted
Various micro perf fixes and cleanup found while implementing CN overrides so far (microsoft#818)
* Microperf: Use lock_guard instead of unique_lock. * Bill hates lock_guard more. * Ref them connections. * Demacroize. * Commonize port and merge some CRLFs into adjacent string literals. * Add missing template keyword. * Avoid stringstream in constructing the proxy_str. * Remove unused forward declarations of some HTTP things. * Use NOMINMAX instead of undef min and max everywhere. * Bunch of inheritance hygiene. * What do you mean you want examples to compile? * More CR comments from Robert. * Add static. * Use existing to_string_t.
1 parent ecabd2b commit 14adc2e

File tree

12 files changed

+60
-96
lines changed

12 files changed

+60
-96
lines changed

Release/include/cpprest/asyncrt_utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ namespace conversions
228228

229229
#if defined(__ANDROID__)
230230
template<class T>
231-
inline std::string to_string(const T& t)
231+
inline std::string to_string(const T t)
232232
{
233233
std::ostringstream os;
234234
os.imbue(std::locale::classic());
@@ -238,16 +238,16 @@ namespace conversions
238238
#endif
239239

240240
template<class T>
241-
inline utility::string_t to_string_t(T&& t)
241+
inline utility::string_t to_string_t(const T t)
242242
{
243243
#ifdef _UTF16_STRINGS
244244
using std::to_wstring;
245-
return to_wstring(std::forward<T>(t));
245+
return to_wstring(t);
246246
#else
247247
#if !defined(__ANDROID__)
248248
using std::to_string;
249249
#endif
250-
return to_string(std::forward<T>(t));
250+
return to_string(t);
251251
#endif
252252
}
253253

Release/include/cpprest/details/web_utilities.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@
1414

1515
namespace web
1616
{
17-
18-
namespace http { namespace client { namespace details {
19-
class winhttp_client;
20-
class winrt_client;
21-
class asio_context;
22-
}}}
23-
namespace websockets { namespace client { namespace details {
24-
class winrt_callback_client;
25-
class wspp_callback_client;
26-
}}}
27-
2817
namespace details
2918
{
3019

Release/include/cpprest/streams.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ class type_parser<CharType, std::enable_if_t<sizeof(CharType) == 1, std::basic_s
17481748

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

17541754
private:

Release/src/http/client/http_client_asio.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
#include "stdafx.h"
1717

18-
#undef min
19-
#undef max
20-
2118
#include "cpprest/asyncrt_utils.h"
2219
#include "../common/internal_http_helpers.h"
2320

@@ -83,7 +80,7 @@ using utility::conversions::details::to_string;
8380
using std::to_string;
8481
#endif
8582

86-
#define CRLF std::string("\r\n")
83+
static const std::string CRLF("\r\n");
8784

8885
namespace web { namespace http
8986
{
@@ -213,14 +210,15 @@ class asio_connection
213210
template <typename Iterator, typename Handler>
214211
void async_connect(const Iterator &begin, const Handler &handler)
215212
{
216-
std::unique_lock<std::mutex> lock(m_socket_lock);
217-
if (!m_closed)
218-
m_socket.async_connect(begin, handler);
219-
else
220213
{
221-
lock.unlock();
222-
handler(boost::asio::error::operation_aborted);
223-
}
214+
std::lock_guard<std::mutex> lock(m_socket_lock);
215+
if (!m_closed) {
216+
m_socket.async_connect(begin, handler);
217+
return;
218+
}
219+
} // unlock
220+
221+
handler(boost::asio::error::operation_aborted);
224222
}
225223

226224
template <typename HandshakeHandler, typename CertificateHandler>
@@ -339,7 +337,7 @@ class asio_connection
339337
/// }
340338
/// </code>
341339
/// </remarks>
342-
class asio_connection_pool : public std::enable_shared_from_this<asio_connection_pool>
340+
class asio_connection_pool final : public std::enable_shared_from_this<asio_connection_pool>
343341
{
344342
public:
345343
asio_connection_pool() : m_pool_epoch_timer(crossplat::threadpool::shared_instance().service())
@@ -396,24 +394,25 @@ class asio_connection_pool : public std::enable_shared_from_this<asio_connection
396394
if (!pool)
397395
return;
398396
auto& self = *pool;
397+
auto& connections = self.m_connections;
399398

400399
std::lock_guard<std::mutex> lock(self.m_lock);
401400
if (self.m_prev_epoch == self.m_epoch)
402401
{
403-
self.m_connections.clear();
402+
connections.clear();
404403
self.is_timer_running = false;
405404
return;
406405
}
407406
else
408407
{
409408
auto prev_epoch = self.m_prev_epoch;
410-
auto erase_end = std::find_if(self.m_connections.begin(), self.m_connections.end(),
409+
auto erase_end = std::find_if(connections.begin(), connections.end(),
411410
[prev_epoch](std::pair<uint64_t, std::shared_ptr<asio_connection>>& p)
412411
{
413412
return p.first > prev_epoch;
414413
});
415414

416-
self.m_connections.erase(self.m_connections.begin(), erase_end);
415+
connections.erase(connections.begin(), erase_end);
417416
start_epoch_interval(pool);
418417
}
419418
});
@@ -438,7 +437,7 @@ class asio_client final : public _http_client_communicator
438437
, m_start_with_ssl(base_uri().scheme() == U("https") && !this->client_config().proxy().is_specified())
439438
{}
440439

441-
void send_request(const std::shared_ptr<request_context> &request_ctx) override;
440+
virtual void send_request(const std::shared_ptr<request_context> &request_ctx) override;
442441

443442
void release_connection(std::shared_ptr<asio_connection>& conn)
444443
{
@@ -468,7 +467,7 @@ class asio_client final : public _http_client_communicator
468467
const bool m_start_with_ssl;
469468
};
470469

471-
class asio_context : public request_context, public std::enable_shared_from_this<asio_context>
470+
class asio_context final : public request_context, public std::enable_shared_from_this<asio_context>
472471
{
473472
friend class asio_client;
474473
public:
@@ -501,7 +500,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
501500
return ctx;
502501
}
503502

504-
class ssl_proxy_tunnel : public std::enable_shared_from_this<ssl_proxy_tunnel>
503+
class ssl_proxy_tunnel final : public std::enable_shared_from_this<ssl_proxy_tunnel>
505504
{
506505
public:
507506
ssl_proxy_tunnel(std::shared_ptr<asio_context> context, std::function<void(std::shared_ptr<asio_context>)> ssl_tunnel_established)
@@ -518,14 +517,15 @@ class asio_context : public request_context, public std::enable_shared_from_this
518517

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

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

526-
request_stream << "CONNECT " << host << ":" << ((port != 0) ? port : 443) << " HTTP/1.1" << CRLF;
527-
request_stream << "Host: " << host << ":" << ((port != 0) ? port : 443) << CRLF;
528-
request_stream << "Proxy-Connection: Keep-Alive" << CRLF;
526+
request_stream << "CONNECT " << host << ":" << port << " HTTP/1.1\r\n";
527+
request_stream << "Host: " << host << ":" << port << CRLF;
528+
request_stream << "Proxy-Connection: Keep-Alive\r\n";
529529

530530
if(m_context->m_http_client->client_config().proxy().credentials().is_set())
531531
{
@@ -698,7 +698,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
698698
request_stream.imbue(std::locale::classic());
699699
const auto &host = utility::conversions::to_utf8string(base_uri.host());
700700

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

703703
int port = base_uri.port();
704704

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

771771
// Start connection timeout timer.
772772
if (!ctx->m_timer.has_started())
@@ -1345,7 +1345,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
13451345
}
13461346
else
13471347
{
1348-
async_read_until_buffersize(octets + CRLF.size(), // + 2 for crlf
1348+
async_read_until_buffersize(octets + CRLF.size(),
13491349
boost::bind(&asio_context::handle_chunk, shared_from_this(), boost::asio::placeholders::error, octets));
13501350
}
13511351
}

Release/src/http/client/http_client_winhttp.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
#include <VersionHelpers.h>
2424
#endif
2525

26-
#undef min
27-
#undef max
28-
2926
namespace web
3027
{
3128
namespace http
@@ -185,7 +182,7 @@ enum msg_body_type
185182
};
186183

187184
// Additional information necessary to track a WinHTTP request.
188-
class winhttp_request_context : public request_context
185+
class winhttp_request_context final : public request_context
189186
{
190187
public:
191188

@@ -248,7 +245,7 @@ class winhttp_request_context : public request_context
248245
std::shared_ptr<winhttp_request_context> m_self_reference;
249246
memory_holder m_body_data;
250247

251-
virtual void cleanup()
248+
void cleanup()
252249
{
253250
if(m_request_handle != nullptr)
254251
{
@@ -260,7 +257,7 @@ class winhttp_request_context : public request_context
260257

261258
protected:
262259

263-
virtual void finish()
260+
virtual void finish() override
264261
{
265262
request_context::finish();
266263
assert(m_self_reference != nullptr);
@@ -346,7 +343,7 @@ struct ie_proxy_config : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG
346343
};
347344

348345
// WinHTTP client.
349-
class winhttp_client : public _http_client_communicator
346+
class winhttp_client final : public _http_client_communicator
350347
{
351348
public:
352349
winhttp_client(http::uri address, http_client_config client_config)
@@ -502,17 +499,13 @@ class winhttp_client : public _http_client_communicator
502499
}
503500
else
504501
{
502+
proxy_str = uri.host();
505503
if (uri.port() > 0)
506504
{
507-
utility::ostringstream_t ss;
508-
ss.imbue(std::locale::classic());
509-
ss << uri.host() << _XPLATSTR(":") << uri.port();
510-
proxy_str = ss.str();
511-
}
512-
else
513-
{
514-
proxy_str = uri.host();
505+
proxy_str.push_back(_XPLATSTR(':'));
506+
proxy_str.append(::utility::conversions::details::to_string_t(uri.port()));
515507
}
508+
516509
proxy_name = proxy_str.c_str();
517510
}
518511
}

Release/src/http/client/http_client_winrt.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ using namespace std;
2828
using namespace Platform;
2929
using namespace Microsoft::WRL;
3030

31-
#undef min
32-
#undef max
33-
3431
namespace web
3532
{
3633
namespace http
@@ -41,7 +38,7 @@ namespace details
4138
{
4239

4340
// Additional information necessary to track a WinRT request.
44-
class winrt_request_context : public request_context
41+
class winrt_request_context final : public request_context
4542
{
4643
public:
4744

@@ -63,7 +60,7 @@ class winrt_request_context : public request_context
6360
};
6461

6562
// Implementation of IXMLHTTPRequest2Callback.
66-
class HttpRequestCallback :
63+
class HttpRequestCallback final :
6764
public RuntimeClass<RuntimeClassFlags<ClassicCom>, IXMLHTTPRequest2Callback, FtmBase>
6865
{
6966
public:
@@ -190,7 +187,7 @@ class HttpRequestCallback :
190187
/// read and write operations. The I/O will be done off the UI thread, so there is no risk
191188
/// of causing the UI to become unresponsive.
192189
/// </remarks>
193-
class IRequestStream
190+
class IRequestStream final
194191
: public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<ClassicCom>, ISequentialStream>
195192
{
196193
public:
@@ -279,7 +276,7 @@ class IRequestStream
279276
/// read and write operations. The I/O will be done off the UI thread, so there is no risk
280277
/// of causing the UI to become unresponsive.
281278
/// </remarks>
282-
class IResponseStream
279+
class IResponseStream final
283280
: public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<ClassicCom>, ISequentialStream>
284281
{
285282
public:
@@ -353,7 +350,7 @@ class IResponseStream
353350
};
354351

355352
// WinRT client.
356-
class winrt_client : public _http_client_communicator
353+
class winrt_client final : public _http_client_communicator
357354
{
358355
public:
359356
winrt_client(http::uri&& address, http_client_config&& client_config)
@@ -379,7 +376,7 @@ class winrt_client : public _http_client_communicator
379376
protected:
380377

381378
// Start sending request.
382-
void send_request(_In_ const std::shared_ptr<request_context> &request)
379+
virtual void send_request(_In_ const std::shared_ptr<request_context> &request) override
383380
{
384381
http_request &msg = request->m_request;
385382
auto winrt_context = std::static_pointer_cast<winrt_request_context>(request);

Release/src/http/common/http_msg.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
#include "stdafx.h"
1414
#include "../common/internal_http_helpers.h"
1515

16-
#undef min
17-
#undef max
18-
1916
using namespace web;
2017
using namespace utility;
2118
using namespace concurrency;

Release/src/http/listener/http_server_asio.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
*/
1515
#include "stdafx.h"
1616

17-
#undef min
18-
#undef max
19-
2017
#include <boost/algorithm/string/find.hpp>
2118
#include <boost/algorithm/string/predicate.hpp>
2219
#include <boost/asio/read_until.hpp>
@@ -1085,7 +1082,7 @@ will_deref_and_erase_t asio_server_connection::cancel_sending_response_with_erro
10851082
{
10861083
auto * context = static_cast<linux_request_context*>(response._get_server_context());
10871084
context->m_response_completed.set_exception(eptr);
1088-
1085+
10891086
// always terminate the connection since error happens
10901087
return finish_request_response();
10911088
}
@@ -1096,7 +1093,7 @@ will_deref_and_erase_t asio_server_connection::handle_write_chunked_response(con
10961093
{
10971094
return handle_response_written(response, ec);
10981095
}
1099-
1096+
11001097
auto readbuf = response._get_impl()->instream().streambuf();
11011098
if (readbuf.is_eof())
11021099
{

0 commit comments

Comments
 (0)