Skip to content

[libSyntax] Assert there are no reference-cycles in the SyntaxArenas #36232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2021

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Mar 2, 2021

As discussed here.

@ahoppen
Copy link
Member Author

ahoppen commented Mar 2, 2021

@swift-ci Please smoke test

@ahoppen ahoppen marked this pull request as ready for review March 2, 2021 17:27
@ahoppen ahoppen requested a review from rintaro March 2, 2021 17:27
Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traversing a whole tree just for an assertion looks to be an overkill, but I think it's OK because the number of SyntaxArena must be fairly low.

Comment on lines 77 to 88
if (PreviousNodes.count(this) > 0) {
return true;
}
for (auto ChildArena : ChildArenas) {
std::set<SyntaxArena *> NewPreviousNodes = PreviousNodes;
NewPreviousNodes.insert(this);
if (ChildArena->containsReferenceCycle(NewPreviousNodes)) {
return true;
}
}
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since PreviousNodes is already copied, we don't need to make a new copy.

Suggested change
if (PreviousNodes.count(this) > 0) {
return true;
}
for (auto ChildArena : ChildArenas) {
std::set<SyntaxArena *> NewPreviousNodes = PreviousNodes;
NewPreviousNodes.insert(this);
if (ChildArena->containsReferenceCycle(NewPreviousNodes)) {
return true;
}
}
return false;
if (!PreviousNodes.insert(this).second)
return true;
return llvm::any_of(ChildArenas, [&](SyntaxArena *Child) {
Child->containsReferenceCycle(PreviousNodes)
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah. For some reason I though I was adding a different node depending on which child node I was visiting. But of course I’m not. Also your implementation looks much cleaner. I changed it. Also renamed PreviousNodes to VisitedArenas.

auto DidInsert = ChildArenas.insert(Arena);
if (DidInsert.second) {
Arena->Retain();
assert(!containsReferenceCycle());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(!containsReferenceCycle());
assert(!Arena->containsReferenceCycle({this}));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and small performance improvement. I like it 👍🏽

}
}

bool containsReferenceCycle(std::set<SyntaxArena *> PreviousNodes = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const if possible.

@ahoppen
Copy link
Member Author

ahoppen commented Mar 2, 2021

Traversing a whole tree just for an assertion looks to be an overkill, but I think it's OK because the number of SyntaxArena must be fairly low.

I don’t expect there to ever be more than, say, 5 arenas, so the performance hit should be pretty marginal. Plus it’s only in assert builds, so I guess it should be fine.

@ahoppen ahoppen force-pushed the pr/no-arena-ref-cycles branch from a8ca2b8 to c7ba6d8 Compare March 2, 2021 21:38
@ahoppen ahoppen force-pushed the pr/no-arena-ref-cycles branch from c7ba6d8 to 7be84c1 Compare March 2, 2021 21:44
@ahoppen
Copy link
Member Author

ahoppen commented Mar 2, 2021

@swift-ci Please smoke test

@ahoppen ahoppen merged commit 782b21b into swiftlang:main Mar 3, 2021
@ahoppen ahoppen deleted the pr/no-arena-ref-cycles branch March 3, 2021 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants