[clang] af8f1db - [clang-format] Use std::iota and reserve when sorting Java imports. NFC.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 1 05:30:05 PST 2022


Author: Marek Kurdej
Date: 2022-02-01T14:29:31+01:00
New Revision: af8f1dbb43bc1a6267ea0b760c7b0fae635938ff

URL: https://github.com/llvm/llvm-project/commit/af8f1dbb43bc1a6267ea0b760c7b0fae635938ff
DIFF: https://github.com/llvm/llvm-project/commit/af8f1dbb43bc1a6267ea0b760c7b0fae635938ff.diff

LOG: [clang-format] Use std::iota and reserve when sorting Java imports. NFC.

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.

Added: 
    

Modified: 
    clang/lib/Format/Format.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 9611e4ae33f8..dd4755c2227e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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) {


        


More information about the cfe-commits mailing list