-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
rustc_metadata: Encode list of all crate's traits into metadata #92244
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -614,8 +614,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
|
||
// Encode the def IDs of impls, for coherence checking. | ||
i = self.position(); | ||
let impls = self.encode_impls(); | ||
let impl_bytes = self.position() - i; | ||
let (traits, impls) = self.encode_traits_and_impls(); | ||
let traits_and_impls_bytes = self.position() - i; | ||
|
||
let tcx = self.tcx; | ||
|
||
|
@@ -727,6 +727,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
foreign_modules, | ||
source_map, | ||
impls, | ||
traits, | ||
exported_symbols, | ||
interpret_alloc_index, | ||
tables, | ||
|
@@ -753,7 +754,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
eprintln!(" diagnostic item bytes: {}", diagnostic_item_bytes); | ||
eprintln!(" native bytes: {}", native_lib_bytes); | ||
eprintln!(" source_map bytes: {}", source_map_bytes); | ||
eprintln!(" impl bytes: {}", impl_bytes); | ||
eprintln!("traits and impls bytes: {}", traits_and_impls_bytes); | ||
eprintln!(" exp. symbols bytes: {}", exported_symbols_bytes); | ||
eprintln!(" def-path table bytes: {}", def_path_table_bytes); | ||
eprintln!(" def-path hashes bytes: {}", def_path_hash_map_bytes); | ||
|
@@ -1784,16 +1785,23 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
} | ||
|
||
/// Encodes an index, mapping each trait to its (local) implementations. | ||
fn encode_impls(&mut self) -> Lazy<[TraitImpls]> { | ||
empty_proc_macro!(self); | ||
debug!("EncodeContext::encode_impls()"); | ||
fn encode_traits_and_impls(&mut self) -> (Lazy<[DefIndex]>, Lazy<[TraitImpls]>) { | ||
if self.is_proc_macro { | ||
return (Lazy::empty(), Lazy::empty()); | ||
} | ||
debug!("EncodeContext::encode_traits_and_impls()"); | ||
let tcx = self.tcx; | ||
let mut visitor = ImplVisitor { tcx, impls: FxHashMap::default() }; | ||
let mut visitor = | ||
TraitsAndImplsVisitor { tcx, impls: FxHashMap::default(), traits: Default::default() }; | ||
tcx.hir().visit_all_item_likes(&mut visitor); | ||
|
||
let mut all_traits = visitor.traits; | ||
let mut all_impls: Vec<_> = visitor.impls.into_iter().collect(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing: same question with |
||
|
||
// Bring everything into deterministic order for hashing | ||
all_traits.sort_by_cached_key(|&local_def_index| { | ||
tcx.hir().def_path_hash(LocalDefId { local_def_index }) | ||
}); | ||
all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id)); | ||
|
||
let all_impls: Vec<_> = all_impls | ||
|
@@ -1811,7 +1819,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
}) | ||
.collect(); | ||
|
||
self.lazy(&all_impls) | ||
(self.lazy(&all_traits), self.lazy(&all_impls)) | ||
} | ||
|
||
// Encodes all symbols exported from this crate into the metadata. | ||
|
@@ -2033,27 +2041,34 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { | |
} | ||
} | ||
|
||
struct ImplVisitor<'tcx> { | ||
struct TraitsAndImplsVisitor<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
traits: Vec<DefIndex>, | ||
impls: FxHashMap<DefId, Vec<(DefIndex, Option<fast_reject::SimplifiedType>)>>, | ||
} | ||
|
||
impl<'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'tcx> { | ||
impl<'tcx, 'v> ItemLikeVisitor<'v> for TraitsAndImplsVisitor<'tcx> { | ||
fn visit_item(&mut self, item: &hir::Item<'_>) { | ||
if let hir::ItemKind::Impl { .. } = item.kind { | ||
if let Some(trait_ref) = self.tcx.impl_trait_ref(item.def_id.to_def_id()) { | ||
let simplified_self_ty = fast_reject::simplify_type( | ||
self.tcx, | ||
trait_ref.self_ty(), | ||
SimplifyParams::No, | ||
StripReferences::No, | ||
); | ||
match item.kind { | ||
hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) => { | ||
self.traits.push(item.def_id.local_def_index); | ||
} | ||
hir::ItemKind::Impl(..) => { | ||
if let Some(trait_ref) = self.tcx.impl_trait_ref(item.def_id.to_def_id()) { | ||
let simplified_self_ty = fast_reject::simplify_type( | ||
self.tcx, | ||
trait_ref.self_ty(), | ||
SimplifyParams::No, | ||
StripReferences::No, | ||
); | ||
|
||
self.impls | ||
.entry(trait_ref.def_id) | ||
.or_default() | ||
.push((item.def_id.local_def_index, simplified_self_ty)); | ||
self.impls | ||
.entry(trait_ref.def_id) | ||
.or_default() | ||
.push((item.def_id.local_def_index, simplified_self_ty)); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be implemented using
tcx.trait_in_crate(LOCAL_CRATE)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some performance trade-off here.
From performance point of view all the stuff in
fn encode_crate_root
should be encoded by walking the crate once, collecting everything, and then encoding everything, instead of walking the crate many times byLOCAL_CRATE
queries collecting their specific pieces of data.But the code would certainly be cleaner and with less duplication with the queries.
I can make the refactoring you suggest later in a separate PR, and we'll land it if doesn't cause observable changes in benchmarks.