Perf: cache connection between const/non-const struct variants.

In one very large program, we were spending quite a bit of time repeatedly
getting const variants of StructTypes.  This speeds up the front-end by
about 40% for that test case.

(This is something of a band-aid, pending uniquing types.)
This commit is contained in:
Matt Pharr
2012-05-04 10:36:44 -07:00
parent b7bef87a4d
commit 58bb2826b2
2 changed files with 26 additions and 10 deletions

View File

@@ -1754,8 +1754,10 @@ StructType::StructType(const std::string &n, const std::vector<const Type *> &el
const std::vector<std::string> &en, const std::vector<std::string> &en,
const std::vector<SourcePos> &ep, const std::vector<SourcePos> &ep,
bool ic, Variability v, SourcePos p) bool ic, Variability v, SourcePos p)
: name(n), elementTypes(elts), elementNames(en), elementPositions(ep), : CollectionType(STRUCT_TYPE), name(n), elementTypes(elts), elementNames(en),
variability(v), isConst(ic), pos(p) { elementPositions(ep), variability(v), isConst(ic), pos(p) {
oppositeConstStructType = NULL;
if (variability != Variability::Unbound) { if (variability != Variability::Unbound) {
// For structs with non-unbound variability, we'll create the // For structs with non-unbound variability, we'll create the
// correspoing LLVM struct type now, if one hasn't been made // correspoing LLVM struct type now, if one hasn't been made
@@ -1908,21 +1910,33 @@ StructType::ResolveUnboundVariability(Variability v) const {
const StructType * const StructType *
StructType::GetAsConstType() const { StructType::GetAsConstType() const {
if (IsConstType()) if (isConst == true)
return this; return this;
else else if (oppositeConstStructType != NULL)
return new StructType(name, elementTypes, elementNames, elementPositions, return oppositeConstStructType;
true, variability, pos); else {
oppositeConstStructType =
new StructType(name, elementTypes, elementNames, elementPositions,
true, variability, pos);
oppositeConstStructType->oppositeConstStructType = this;
return oppositeConstStructType;
}
} }
const StructType * const StructType *
StructType::GetAsNonConstType() const { StructType::GetAsNonConstType() const {
if (!IsConstType()) if (isConst == false)
return this; return this;
else else if (oppositeConstStructType != NULL)
return new StructType(name, elementTypes, elementNames, elementPositions, return oppositeConstStructType;
false, variability, pos); else {
oppositeConstStructType =
new StructType(name, elementTypes, elementNames, elementPositions,
false, variability, pos);
oppositeConstStructType->oppositeConstStructType = this;
return oppositeConstStructType;
}
} }

2
type.h
View File

@@ -732,6 +732,8 @@ private:
const Variability variability; const Variability variability;
const bool isConst; const bool isConst;
const SourcePos pos; const SourcePos pos;
mutable const StructType *oppositeConstStructType;
}; };