Skip to content

Various cleanups for GenericEnvironment #5966

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 3 commits into from
Nov 28, 2016

Conversation

DougGregor
Copy link
Member

Two cleanups for GenericEnvironment:

  • Plug a memory leak: ASTContext-allocated types don't get destroyed unless registered explicitly, so we were leaking the TypeSubstitutionMap members of GenericEnvironment.

  • Eliminate nondeterministic behavior in the mapping from archetypes to interface types when two generic parameters map to the same archetype.

The uses of this function that want *all* nested types now go through
an entry point getAllNestedTypes(), and will need to be removed to
support recursive protocol constraints.

The uses of this function that only want to see what's been expanded
so far---dumpers and verifiers, mainly---can use
getKnownNestedTypes(), which may change type but is a reasonable
operation to continue using.
ASTContext-allocated objects don't get destructed unless they are
registered; make sure to do that for GenericEnvironments, because
we're leaking them like crazy.
GenericEnvironment walked its input mapping in DenseMap order while
populating a reverse mapping from archetypes to interface
types. However, this mapping is not unique, because two generic
parameters can end up mapping to the same archetype. In such cases,
which generic parameter we mapped to was nondeterministic.

Make this deterministic by preferring to map back to the earlier
generic parameter.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

@@ -53,6 +53,9 @@ GenericEnvironment::GenericEnvironment(
// FIXME: If multiple generic parameters map to the same archetype,
// the reverse mapping order is not deterministic.
}

// Make sure this generic environment gets destroyed.
signature->getASTContext().addDestructorCleanup(*this);
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes me unhappy; it seems like it could greatly increase the number of destructor cleanups we need. Since the mapping won't change during the lifetime of the generic environment, can we use some kind of sorted list and tail-allocate the storage instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Eventually, yes. The problem is that Type::subst(), which is used a lot with these mappings in GenericEnvironment, requires aTypeSubstitutionMap. So we'd be unpacking the mapping a whole lot of times.

A better approach would be to teach Type::subst() and related not to use a DenseMap as their way of providing mappings. That'll take a bit more work, but is the right thing to do long-term.

@swift-ci swift-ci merged commit 7358bf9 into swiftlang:master Nov 28, 2016
@DougGregor DougGregor deleted the generic-environment-cleanups branch November 28, 2016 23:04
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.

3 participants