[clang] [Clang] Avoid quadratic pack-indexing instantiation (store only the selected element) (PR #213790)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 5 02:31:06 PDT 2026


================
@@ -7121,6 +7127,56 @@ TreeTransform<Derived>::TransformPackIndexingType(TypeLocBuilder &TLB,
   if (Types.empty() && !PIT->expandsToEmptyPack())
     Types = llvm::ArrayRef<QualType>(&Pattern, 1);
 
+  // Fast path: substitute only the selected element instead of all N. A
+  // pack-indexing type inside a pack expansion (`T...[Is]...`) is transformed
+  // once per outer element, so substituting the whole pack each time is
+  // O(N^2) in time and memory.
+  if (Types.size() == 1 && Types[0]->containsUnexpandedParameterPack() &&
+      IndexExpr.isUsable() && !IndexExpr.get()->isInstantiationDependent()) {
----------------
zyn0217 wrote:

Please also note that, if any AI agent is involved, you should clarify that in your PR description: https://llvm.org/docs/AIToolPolicy.html

https://github.com/llvm/llvm-project/pull/213790


More information about the cfe-commits mailing list