[cfe-commits] r67452 - /cfe/trunk/lib/Sema/SemaInit.cpp

Douglas Gregor dgregor at apple.com
Sat Mar 21 11:13:52 PDT 2009


Author: dgregor
Date: Sat Mar 21 13:13:52 2009
New Revision: 67452

URL: http://llvm.org/viewvc/llvm-project?rev=67452&view=rev
Log:
Fix a thinko in the pre-allocation strategy for structured initializer
lists. The code wasn't accounting for the distinction between the
top-level call to getStructuredSubobjectInit and later calls that
occur deeper in the hierarchy. This problem manifested itself as
over-allocation in cases where we have large arrays of small
structures (<rdar://problem/6707362>).

Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=67452&r1=67451&r2=67452&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Sat Mar 21 13:13:52 2009
@@ -1452,13 +1452,21 @@
 
   // Pre-allocate storage for the structured initializer list.
   unsigned NumElements = 0;
+  unsigned NumInits = 0;
+  if (!StructuredList)
+    NumInits = IList->getNumInits();
+  else if (Index < IList->getNumInits()) {
+    if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
+      NumInits = SubList->getNumInits();
+  }
+
   if (const ArrayType *AType 
       = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
     if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
       NumElements = CAType->getSize().getZExtValue();
       // Simple heuristic so that we don't allocate a very large
       // initializer with many empty entries at the end.
-      if (IList && NumElements > IList->getNumInits())
+      if (NumInits && NumElements > NumInits)
         NumElements = 0;
     }
   } else if (const VectorType *VType = CurrentObjectType->getAsVectorType())
@@ -1471,7 +1479,7 @@
       NumElements = std::distance(RDecl->field_begin(), RDecl->field_end());
   }
 
-  if (IList && NumElements < IList->getNumInits())
+  if (NumElements < NumInits)
     NumElements = IList->getNumInits();
 
   Result->reserveInits(NumElements);





More information about the cfe-commits mailing list