[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:08 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:

Why should we check IndexExpr.isUsable() here? Do you have examples where we're transforming an invalid IndexExpr, that your fast path doesn't work whereas the slow path works?

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


More information about the cfe-commits mailing list