From 78d62705cc1d27e8654635b103d28d9af15b1f0c Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Fri, 4 May 2012 12:07:01 -0700 Subject: [PATCH] Cache element types in StructType. Previously, GetElementType() would end up causing dynamic allocation to happen to compute the final element type (turning types with unbound variability into the same type with the struct's variability) each it was called, which was wasteful and slow. Now we cache the result. Another 20% perf on compiling that problematic program. --- type.cpp | 27 +++++++++++++++++---------- type.h | 2 ++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/type.cpp b/type.cpp index c29ac599..a978a16b 100644 --- a/type.cpp +++ b/type.cpp @@ -1757,7 +1757,8 @@ StructType::StructType(const std::string &n, const std::vector &el : CollectionType(STRUCT_TYPE), name(n), elementTypes(elts), elementNames(en), elementPositions(ep), variability(v), isConst(ic), pos(p) { oppositeConstStructType = NULL; - + finalElementTypes.resize(elts.size(), NULL); + if (variability != Variability::Unbound) { // For structs with non-unbound variability, we'll create the // correspoing LLVM struct type now, if one hasn't been made @@ -2074,17 +2075,23 @@ const Type * StructType::GetElementType(int i) const { Assert(variability != Variability::Unbound); Assert(i < (int)elementTypes.size()); - const Type *ret = elementTypes[i]; - if (ret == NULL) { - Assert(m->errorCount > 0); - return NULL; + + if (finalElementTypes[i] == NULL) { + const Type *type = elementTypes[i]; + if (type == NULL) { + Assert(m->errorCount > 0); + return NULL; + } + + // If the element has unbound variability, resolve its variability to + // the struct type's variability + type = type ->ResolveUnboundVariability(variability); + if (isConst) + type = type->GetAsConstType(); + finalElementTypes[i] = type; } - // If the element has unbound variability, resolve its variability to - // the struct type's variability - ret = ret->ResolveUnboundVariability(variability); - - return isConst ? ret->GetAsConstType() : ret; + return finalElementTypes[i]; } diff --git a/type.h b/type.h index 6102e0dd..eb80281b 100644 --- a/type.h +++ b/type.h @@ -716,6 +716,8 @@ private: const bool isConst; const SourcePos pos; + mutable std::vector finalElementTypes; + mutable const StructType *oppositeConstStructType; };