Skip to content

Commit b1f9c10

Browse files
committed
ASTScope: Small optimization to ASTScopeImpl memory layout
Combine the wasExpanded flag with the parent pointer, and remove the haveAddedCleanup flag since it's no longer necessary now that "re-expansion" is gone.
1 parent 0fa84c1 commit b1f9c10

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

include/swift/AST/ASTScope.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,16 @@ class ASTScopeImpl {
136136
/// storage declaration or is directly descended from it.
137137

138138
private:
139-
/// Always set by the constructor, so that when creating a child
140-
/// the parent chain is available.
141-
ASTScopeImpl *parent = nullptr; // null at the root
139+
/// The pointer:
140+
/// - Always set by the constructor, so that when creating a child
141+
/// the parent chain is available. Null at the root.
142+
/// The int:
143+
/// - A flag indicating if the scope has been expanded yet or not.
144+
llvm::PointerIntPair<ASTScopeImpl *, 1> parentAndWasExpanded;
142145

143146
/// Child scopes, sorted by source range.
144147
Children storedChildren;
145148

146-
bool wasExpanded = false;
147-
148-
/// Can clear storedChildren, so must remember this
149-
bool haveAddedCleanup = false;
150-
151149
mutable Optional<CharSourceRange> cachedCharSourceRange;
152150

153151
#pragma mark - constructor / destructor
@@ -177,8 +175,12 @@ class ASTScopeImpl {
177175

178176
#pragma mark - tree declarations
179177
protected:
180-
NullablePtr<ASTScopeImpl> getParent() { return parent; }
181-
NullablePtr<const ASTScopeImpl> getParent() const { return parent; }
178+
NullablePtr<ASTScopeImpl> getParent() {
179+
return parentAndWasExpanded.getPointer();
180+
}
181+
NullablePtr<const ASTScopeImpl> getParent() const {
182+
return parentAndWasExpanded.getPointer();
183+
}
182184

183185
const Children &getChildren() const { return storedChildren; }
184186

@@ -247,13 +249,13 @@ class ASTScopeImpl {
247249

248250
#pragma mark - Scope tree creation
249251
public:
250-
/// Expand the scope. Asserts if it was already expanded.
252+
/// Expand the scope if unexpanded.
251253
ASTScopeImpl *expandAndBeCurrent(ScopeCreator &);
252254

253-
bool getWasExpanded() const { return wasExpanded; }
255+
bool getWasExpanded() const { return parentAndWasExpanded.getInt(); }
254256

255257
protected:
256-
void setWasExpanded() { wasExpanded = true; }
258+
void setWasExpanded() { parentAndWasExpanded.setInt(1); }
257259
virtual ASTScopeImpl *expandSpecifically(ScopeCreator &) = 0;
258260

259261
public:
@@ -354,7 +356,7 @@ class ASTScopeImpl {
354356
/// what obtaines for scoping. However, guards are different. The scope after
355357
/// the guard else must hop into the innermoset scope of the guard condition.
356358
virtual NullablePtr<const ASTScopeImpl> getLookupParent() const {
357-
return parent;
359+
return getParent();
358360
}
359361

360362
#pragma mark - - lookup- local bindings

lib/AST/ASTScopeCreation.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,19 +579,17 @@ ScopeCreator::addPatternBindingToScopeTree(PatternBindingDecl *patternBinding,
579579

580580
void ASTScopeImpl::addChild(ASTScopeImpl *child, ASTContext &ctx) {
581581
ASTScopeAssert(!child->getParent(), "child should not already have parent");
582-
child->parent = this;
582+
child->parentAndWasExpanded.setPointer(this);
583583

584584
#ifndef NDEBUG
585585
checkSourceRangeBeforeAddingChild(child, ctx);
586586
#endif
587587

588588
// If this is the first time we've added children, notify the ASTContext
589589
// that there's a SmallVector that needs to be cleaned up.
590-
// FIXME: If we had access to SmallVector::isSmall(), we could do better.
591-
if (storedChildren.empty() && !haveAddedCleanup) {
590+
if (storedChildren.empty())
592591
ctx.addDestructorCleanup(storedChildren);
593-
haveAddedCleanup = true;
594-
}
592+
595593
storedChildren.push_back(child);
596594
}
597595

0 commit comments

Comments
 (0)