Sort iterators to avoid extra std::string creation in Block::sortColumns()

Since std::string is pretty heavy.

Suggested-by: @alexey-milovidov
This commit is contained in:
Azat Khuzhin 2020-05-12 03:51:58 +03:00
parent 0e117abd2b
commit 4de7d7a84f

View File

@ -404,13 +404,18 @@ Block Block::sortColumns() const
Block sorted_block;
/// std::unordered_map (index_by_name) cannot be used to guarantee the sort order
std::vector<std::pair<String, size_t>> sorted_index_by_name(index_by_name.begin(), index_by_name.end());
std::vector<decltype(index_by_name.begin())> sorted_index_by_name(index_by_name.size());
{
size_t i = 0;
for (auto it = index_by_name.begin(); it != index_by_name.end(); ++it)
sorted_index_by_name[i++] = it;
}
std::sort(sorted_index_by_name.begin(), sorted_index_by_name.end(), [](const auto & lhs, const auto & rhs) {
return lhs.first < rhs.first;
return lhs->first < rhs->first;
});
for (const auto & name : sorted_index_by_name)
sorted_block.insert(data[name.second]);
for (const auto & it : sorted_index_by_name)
sorted_block.insert(data[it->second]);
return sorted_block;
}