perf: omit extra std::map in tr_torrentPrimaryMimeType() (#2064)

This commit is contained in:
Charles Kerr 2021-10-30 11:49:47 -05:00 committed by GitHub
parent 85c4a323bb
commit 7c07f4acbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 11 deletions

View File

@ -3186,20 +3186,20 @@ std::string_view tr_torrentPrimaryMimeType(tr_torrent const* tor)
size_per_mime_type[mime_type] += it->length;
}
// now that we have the totals,
// sort by number so that we can get the biggest
auto mime_type_per_size = std::map<size_t, std::string_view>{};
for (auto it : size_per_mime_type)
if (std::empty(size_per_mime_type))
{
mime_type_per_size.emplace(it.second, it.first);
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
// application/octet-stream is the default value for all other cases.
// An unknown file type should use this type.
auto constexpr Fallback = "application/octet-stream"sv;
return Fallback;
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
// application/octet-stream is the default value for all other cases.
// An unknown file type should use this type.
auto constexpr Fallback = "application/octet-stream"sv;
return std::empty(mime_type_per_size) ? Fallback : mime_type_per_size.rbegin()->second;
auto const it = std::max_element(
std::begin(size_per_mime_type),
std::end(size_per_mime_type),
[](auto const& a, auto const& b) { return a.second < b.second; });
return it->first;
}
/***