[cfe-commits] r130420 - in /cfe/trunk: lib/Sema/SemaInit.cpp test/Index/initializer-memory.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Thu Apr 28 11:53:55 PDT 2011


Author: akirtzidis
Date: Thu Apr 28 13:53:55 2011
New Revision: 130420

URL: http://llvm.org/viewvc/llvm-project?rev=130420&view=rev
Log:
Don't waste memory if the initializer expression is empty.

Modified:
    cfe/trunk/lib/Sema/SemaInit.cpp
    cfe/trunk/test/Index/initializer-memory.cpp

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=130420&r1=130419&r2=130420&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Thu Apr 28 13:53:55 2011
@@ -1795,11 +1795,15 @@
   // Pre-allocate storage for the structured initializer list.
   unsigned NumElements = 0;
   unsigned NumInits = 0;
-  if (!StructuredList)
+  bool GotNumInits = false;
+  if (!StructuredList) {
     NumInits = IList->getNumInits();
-  else if (Index < IList->getNumInits()) {
-    if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index)))
+    GotNumInits = true;
+  } else if (Index < IList->getNumInits()) {
+    if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) {
       NumInits = SubList->getNumInits();
+      GotNumInits = true;
+    }
   }
 
   if (const ArrayType *AType
@@ -1808,7 +1812,7 @@
       NumElements = CAType->getSize().getZExtValue();
       // Simple heuristic so that we don't allocate a very large
       // initializer with many empty entries at the end.
-      if (NumInits && NumElements > NumInits)
+      if (GotNumInits && NumElements > NumInits)
         NumElements = 0;
     }
   } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())

Modified: cfe/trunk/test/Index/initializer-memory.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/initializer-memory.cpp?rev=130420&r1=130419&r2=130420&view=diff
==============================================================================
--- cfe/trunk/test/Index/initializer-memory.cpp (original)
+++ cfe/trunk/test/Index/initializer-memory.cpp Thu Apr 28 13:53:55 2011
@@ -3,6 +3,7 @@
 // rdar://9275920 - We would create millions of Exprs to fill out the initializer.
 
 double data[1000000] = {0};
+double data_empty_init[1000000] = {};
 
 struct S {
  S(int);
@@ -10,5 +11,6 @@
 };
 
 S data2[1000000] = {0};
+S data_empty_init2[1000000] = {};
 
 // CHECK: TOTAL = {{.*}} (0.{{.*}} MBytes)





More information about the cfe-commits mailing list