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.
This commit is contained in:
Matt Pharr
2012-05-04 12:07:01 -07:00
parent 2791bd0015
commit 78d62705cc
2 changed files with 19 additions and 10 deletions

View File

@@ -1757,7 +1757,8 @@ StructType::StructType(const std::string &n, const std::vector<const Type *> &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];
}

2
type.h
View File

@@ -716,6 +716,8 @@ private:
const bool isConst;
const SourcePos pos;
mutable std::vector<const Type *> finalElementTypes;
mutable const StructType *oppositeConstStructType;
};