Skip to content

Commit 5307d71

Browse files
committed
[clang][bytecode] Lazily create DynamicAllocator
Due to all the tracking via map(s) and a BumpPtrAllocator, the creating and destroying the DynamicAllocator is rather expensive. Try to do it lazily and only create it when first calling InterpState::getAllocator().
1 parent eadde07 commit 5307d71

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

clang/lib/AST/ByteCode/InterpState.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ InterpState::~InterpState() {
5959
void InterpState::cleanup() {
6060
// As a last resort, make sure all pointers still pointing to a dead block
6161
// don't point to it anymore.
62-
Alloc.cleanup();
62+
if (Alloc)
63+
Alloc->cleanup();
6364
}
6465

6566
Frame *InterpState::getCurrentFrame() {
@@ -103,10 +104,13 @@ void InterpState::deallocate(Block *B) {
103104
}
104105

105106
bool InterpState::maybeDiagnoseDanglingAllocations() {
106-
bool NoAllocationsLeft = !Alloc.hasAllocations();
107+
if (!Alloc)
108+
return true;
109+
110+
bool NoAllocationsLeft = !Alloc->hasAllocations();
107111

108112
if (!checkingPotentialConstantExpression()) {
109-
for (const auto &[Source, Site] : Alloc.allocation_sites()) {
113+
for (const auto &[Source, Site] : Alloc->allocation_sites()) {
110114
assert(!Site.empty());
111115

112116
CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)

clang/lib/AST/ByteCode/InterpState.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ class InterpState final : public State, public SourceMapper {
118118

119119
void setEvalLocation(SourceLocation SL) { this->EvalLocation = SL; }
120120

121-
DynamicAllocator &getAllocator() { return Alloc; }
121+
DynamicAllocator &getAllocator() {
122+
if (!Alloc) {
123+
Alloc = std::make_unique<DynamicAllocator>();
124+
}
125+
126+
return *Alloc.get();
127+
}
122128

123129
/// Diagnose any dynamic allocations that haven't been freed yet.
124130
/// Will return \c false if there were any allocations to diagnose,
@@ -164,7 +170,7 @@ class InterpState final : public State, public SourceMapper {
164170
/// Reference to the offset-source mapping.
165171
SourceMapper *M;
166172
/// Allocator used for dynamic allocations performed via the program.
167-
DynamicAllocator Alloc;
173+
std::unique_ptr<DynamicAllocator> Alloc;
168174

169175
public:
170176
/// Reference to the module containing all bytecode.

0 commit comments

Comments
 (0)