From 58bb2826b28c35b6ab114d61db9fbe45dfbdbd65 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Fri, 4 May 2012 10:36:44 -0700 Subject: [PATCH] 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.) --- type.cpp | 34 ++++++++++++++++++++++++---------- type.h | 2 ++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/type.cpp b/type.cpp index 64e832bb..95877122 100644 --- a/type.cpp +++ b/type.cpp @@ -1754,8 +1754,10 @@ StructType::StructType(const std::string &n, const std::vector &el const std::vector &en, const std::vector &ep, bool ic, Variability v, SourcePos p) - : name(n), elementTypes(elts), elementNames(en), elementPositions(ep), - variability(v), isConst(ic), pos(p) { + : CollectionType(STRUCT_TYPE), name(n), elementTypes(elts), elementNames(en), + elementPositions(ep), variability(v), isConst(ic), pos(p) { + oppositeConstStructType = 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 @@ -1908,21 +1910,33 @@ StructType::ResolveUnboundVariability(Variability v) const { const StructType * StructType::GetAsConstType() const { - if (IsConstType()) + if (isConst == true) return this; - else - return new StructType(name, elementTypes, elementNames, elementPositions, - true, variability, pos); + else if (oppositeConstStructType != NULL) + return oppositeConstStructType; + else { + oppositeConstStructType = + new StructType(name, elementTypes, elementNames, elementPositions, + true, variability, pos); + oppositeConstStructType->oppositeConstStructType = this; + return oppositeConstStructType; + } } const StructType * StructType::GetAsNonConstType() const { - if (!IsConstType()) + if (isConst == false) return this; - else - return new StructType(name, elementTypes, elementNames, elementPositions, - false, variability, pos); + else if (oppositeConstStructType != NULL) + return oppositeConstStructType; + else { + oppositeConstStructType = + new StructType(name, elementTypes, elementNames, elementPositions, + false, variability, pos); + oppositeConstStructType->oppositeConstStructType = this; + return oppositeConstStructType; + } } diff --git a/type.h b/type.h index e0560ce5..82e5a611 100644 --- a/type.h +++ b/type.h @@ -732,6 +732,8 @@ private: const Variability variability; const bool isConst; const SourcePos pos; + + mutable const StructType *oppositeConstStructType; };