Skip to content
Permalink
Browse files

Common/HttpRequest: Avoid unnecessary copies in loop in Fetch()

Previously, every entry pair within the map would be copied. The reason
for this is subtle.

A std::map's internal entry type is defined as:

std::pair<const Key, Value>

but the loop was declaring it as:

std::pair<Key, Value>

These two types aren't synonymous with one another and so the compiler
is required to always perform a copy.

Using structured bindings avoids this (as would plain auto or correcting
the explicit type), while also allowing the use of more appropriate
names compared to first and second.
  • Loading branch information...
lioncash committed May 27, 2019
1 parent 8dc8cf8 commit b15f595130c22c595464707a372132fc8e7ea72b
Showing with 6 additions and 6 deletions.
  1. +6 −6 Source/Core/Common/HttpRequest.cpp
@@ -197,14 +197,14 @@ HttpRequest::Response HttpRequest::Impl::Fetch(const std::string& url, Method me

curl_slist* list = nullptr;
Common::ScopeGuard list_guard{[&list] { curl_slist_free_all(list); }};
for (const std::pair<std::string, std::optional<std::string>>& header : headers)
for (const auto& [name, value] : headers)
{
if (!header.second)
list = curl_slist_append(list, (header.first + ":").c_str());
else if (header.second->empty())
list = curl_slist_append(list, (header.first + ";").c_str());
if (!value)
list = curl_slist_append(list, (name + ':').c_str());
else if (value->empty())
list = curl_slist_append(list, (name + ';').c_str());
else
list = curl_slist_append(list, (header.first + ": " + *header.second).c_str());
list = curl_slist_append(list, (name + ": " + *value).c_str());
}
curl_easy_setopt(m_curl.get(), CURLOPT_HTTPHEADER, list);

0 comments on commit b15f595

Please sign in to comment.
You can’t perform that action at this time.