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.
This commit is contained in:
Lioncash 2019-05-27 09:31:13 -04:00
parent 8dc8cf8019
commit b15f595130
1 changed files with 6 additions and 6 deletions

View File

@ -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);