Skip to content

[Archetype builder] Make archetype construction more lazy #6007

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 8 commits into from
Dec 1, 2016

Conversation

DougGregor
Copy link
Member

Another checkpoint along the road toward lazily-constructed archetypes, which includes:

  • Teaching GenericEnvironment how to use an ArchetypeBuilder to lazily fill in context types, rather than computing them prior to creation of the GenericEnvironment.
  • Push the "create all the archetypes" logic as late as we can in ArchetypeBuilder::getGenericEnvironment(). To make it truly lazy, we need the archetype builder to persist for the life of the AST (which requires still more work)
  • Go through the normal substitution logic when mapping interface types to context types in the archetype builder, rather than a specialized Type::transform().
  • Adding a new variant of Type::subst() that uses an arbitrary callback to map substitutable types to their replacement types, which is necessary when the mapping might be lazily constructed (see GenericEnvironment's use of Type::subst().
  • As a bonus, the new variable of Type::subst() makes it easy to introduce alternate storage mechanisms for the substitutable type -> replacement type mapping, so use a tail-allocated flat array of context types in GenericEnvironment that is parallel with the generic parameters list.

This eliminates the really gross registration of archetype builders
within the ASTContext, and is another little step toward lazily
constructing archetypes.
…ater.

Force archetypes to be created when forming the generic environment,
making the operation slightly more lazy.
…n function

The "core" data structure used to record the substitutions to be
performed is a TypeSubstitutionMap, which is a DenseMap. This is a
fairly heavyweight, static data structure for something where

* We occasionally want a more dynamic, lazily-populated data structure, and
* We can usually provide more efficient storage than a DenseMap.

So, introduce a Type::subst() variant that takes a TypeSubstitutionFn,
which is just a function that maps a SubstitutableType * to a Type (or
nothing). Use this as the core variant of subst(). with an adapter for
existing TypeSubstitutionMaps. Over time, TypeSubstitutionMap should
go away.
Rather than storing a heavyweight DenseMap for the mapping from
interface types (which are always generic type parameters) to their
corresponding context types, tail-allocate the array of context types
as an array parallel to the generic type parameters array. Use
GenericParamKey's lookup facilities and the new
type-substitution-function-based version of Type::subst() to handle
queries efficiently.
Teach GenericEnvironment to lazily populate its mapping from generic
parameters to context types when queried (e.g., during
substitutition) rather than assuming that will be pre-populated before
any queries occur.

We're still forcing all of the archetypes when the generic environment
is created by the archetype builder; baby steps!
…o-context.

Now that the generic environment is lazily populated (within
ArchetypeBuilder::getGenericEnvironment(), of course!), we can use a
normal Type::subst() to map interface types recorded within the
potential archetypes into a particular environment.
Silences a warning on newer Clang versions.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

This code was trying and failing to solve a problem that we do not
have. The "problem" would have been that archetypes show up in
superclass constraints, and the attempted solution---mapping them out
to interface types---wouldn't actually work because resolveArchetype()
doesn't work that way.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit c3aea5b into swiftlang:master Dec 1, 2016
@DougGregor DougGregor deleted the lazier-archetypes branch December 1, 2016 20:54
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