Skip to content

Commit

Permalink
[clang-format] Use std::iota and reserve when sorting Java imports. 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 e75a342 commit af8f1db
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2749,13 +2749,16 @@ static void sortJavaImports(const FormatStyle &Style,
unsigned ImportsBlockSize = ImportsEndOffset - ImportsBeginOffset;
if (!affectsRange(Ranges, ImportsBeginOffset, ImportsEndOffset))
return;

SmallVector<unsigned, 16> Indices;
Indices.resize(Imports.size());
std::iota(Indices.begin(), Indices.end(), 0);

SmallVector<unsigned, 16> JavaImportGroups;
for (unsigned i = 0, e = Imports.size(); i != e; ++i) {
Indices.push_back(i);
JavaImportGroups.push_back(
findJavaImportGroup(Style, Imports[i].Identifier));
}
JavaImportGroups.reserve(Imports.size());
for (const JavaImportDirective &Import : Imports)
JavaImportGroups.push_back(findJavaImportGroup(Style, Import.Identifier));

bool StaticImportAfterNormalImport =
Style.SortJavaStaticImport == FormatStyle::SJSIO_After;
llvm::sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
Expand Down

0 comments on commit af8f1db

Please sign in to comment.