Skip to content

Commit

Permalink
[clang-format] Use std::iota and reserve. NFC.
Browse files Browse the repository at this point in the history
This way we have at most 1 allocation even if the number of includes is greater than the on-stack size of the small vector.
  • Loading branch information
mkurdej committed Feb 1, 2022
1 parent 34b4f00 commit e75a342
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions clang/lib/Format/Format.cpp
Expand Up @@ -44,6 +44,7 @@
#include <algorithm>
#include <memory>
#include <mutex>
#include <numeric>
#include <string>
#include <unordered_map>

Expand Down Expand Up @@ -2521,8 +2522,8 @@ static void sortCppIncludes(const FormatStyle &Style,
if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset))
return;
SmallVector<unsigned, 16> Indices;
for (unsigned i = 0, e = Includes.size(); i != e; ++i)
Indices.push_back(i);
Indices.resize(Includes.size());
std::iota(Indices.begin(), Indices.end(), 0);

if (Style.SortIncludes == FormatStyle::SI_CaseInsensitive) {
llvm::stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
Expand Down

2 comments on commit e75a342

@d0k
Copy link
Member

@d0k d0k commented on e75a342 Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cleaner way of writing this with LLVM utilities would be something like llvm::to_vector<16>(llvm::seq<unsigned>(0, Includes.size()))

@mkurdej
Copy link
Member Author

@mkurdej mkurdej commented on e75a342 Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I didn't know about llvm::seq. Thanks!

Please sign in to comment.