-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Resolver: Parallelize the import resolution loop #158845
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,12 @@ | |
|
|
||
| use std::cmp::Ordering; | ||
| use std::mem; | ||
| use std::sync::atomic::{self, AtomicUsize}; | ||
|
|
||
| use rustc_ast::NodeId; | ||
| use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; | ||
| use rustc_data_structures::intern::Interned; | ||
| use rustc_data_structures::sync::Lock; | ||
| use rustc_errors::{Applicability, BufferedEarlyLint, Diagnostic}; | ||
| use rustc_expand::base::SyntaxExtensionKind; | ||
| use rustc_hir::def::{self, DefKind, PartialRes}; | ||
|
|
@@ -50,6 +52,7 @@ pub(crate) enum PendingDecl<'ra> { | |
| } | ||
|
|
||
| enum ImportResolutionKind<'ra> { | ||
| // these are the decls the import imports, not the import declarations themselves | ||
| Single(PerNS<PendingDecl<'ra>>), | ||
| Glob(Vec<(Decl<'ra>, BindingKey, Span /* orig_ident_span */)>), | ||
| } | ||
|
|
@@ -775,31 +778,44 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| let mut indeterminate_count = self.indeterminate_imports.len() * 3; | ||
| while indeterminate_count < prev_indeterminate_count { | ||
| prev_indeterminate_count = indeterminate_count; | ||
| indeterminate_count = 0; | ||
| let mut resolutions = Vec::new(); | ||
| let atomic_indeterminate_count = AtomicUsize::new(0); | ||
|
|
||
| self.assert_speculative = true; | ||
| for import in mem::take(&mut self.indeterminate_imports) { | ||
| let (resolution, import_indeterminate_count) = self.cm().resolve_import(import); | ||
| indeterminate_count += import_indeterminate_count; | ||
|
|
||
| let to_resolve_imports = mem::take(&mut self.indeterminate_imports); | ||
| let cm_resolver = self.cm(); | ||
|
|
||
| let determined_imports = Lock::new(Vec::new()); | ||
| let indeterminate_imports = Lock::new(Vec::new()); | ||
|
petrochenkov marked this conversation as resolved.
|
||
| let resolutions = rustc_data_structures::sync::par_map(to_resolve_imports, |import| { | ||
|
Contributor
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. It probably makes sense to add
Contributor
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. The partition into The number of |
||
| let (resolution, import_indeterminate_count) = | ||
| cm_resolver.reborrow_ref().resolve_import(import); | ||
| atomic_indeterminate_count | ||
| .fetch_add(import_indeterminate_count, atomic::Ordering::Relaxed); | ||
| match import_indeterminate_count { | ||
| 0 => self.determined_imports.push(import), | ||
| _ => self.indeterminate_imports.push(import), | ||
| } | ||
| if let Some(resolution) = resolution { | ||
| resolutions.push((import, resolution)); | ||
| 0 => determined_imports.lock().push(import), | ||
| _ => indeterminate_imports.lock().push(import), | ||
| } | ||
| } | ||
| (import, resolution) | ||
| }); | ||
| indeterminate_count = atomic_indeterminate_count.into_inner(); | ||
| self.determined_imports.extend(determined_imports.into_inner()); | ||
| self.indeterminate_imports.extend(indeterminate_imports.into_inner()); | ||
|
|
||
| self.assert_speculative = false; | ||
|
|
||
| self.write_import_resolutions(resolutions); | ||
| } | ||
| } | ||
|
|
||
| fn write_import_resolutions( | ||
| &mut self, | ||
| import_resolutions: Vec<(Import<'ra>, ImportResolution<'ra>)>, | ||
| import_resolutions: Vec<(Import<'ra>, Option<ImportResolution<'ra>>)>, | ||
| ) { | ||
| for (import, resolution) in &import_resolutions { | ||
| let ImportResolution { imported_module, .. } = resolution; | ||
| let Some(ImportResolution { imported_module, .. }) = resolution else { | ||
| continue; | ||
| }; | ||
| import.imported_module.set(Some(*imported_module), self); | ||
|
|
||
| if import.is_glob() | ||
|
|
@@ -812,7 +828,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| } | ||
|
|
||
| for (import, resolution) in import_resolutions { | ||
| let ImportResolution { imported_module, kind: resolution_kind } = resolution; | ||
| let Some(ImportResolution { imported_module, kind: resolution_kind }) = resolution | ||
| else { | ||
| continue; | ||
| }; | ||
|
|
||
| match (&import.kind, resolution_kind) { | ||
| ( | ||
|
|
@@ -1139,7 +1158,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| _ => unreachable!(), | ||
| }; | ||
|
|
||
| let mut import_decls = PerNS::default(); | ||
| let mut decls = PerNS::default(); | ||
| let mut indeterminate_count = 0; | ||
| self.per_ns_cm(|mut this, ns| { | ||
| if bindings[ns].get() != PendingDecl::Pending { | ||
|
|
@@ -1160,12 +1179,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
| PendingDecl::Pending | ||
| } | ||
| }; | ||
| import_decls[ns] = pending_decl; | ||
| decls[ns] = pending_decl; | ||
| }); | ||
| let import_resolution = ImportResolution { | ||
| imported_module: module, | ||
| kind: ImportResolutionKind::Single(import_decls), | ||
| }; | ||
| let import_resolution = | ||
| ImportResolution { imported_module: module, kind: ImportResolutionKind::Single(decls) }; | ||
|
|
||
| (Some(import_resolution), indeterminate_count) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
The FIXME was lost.
View changes since the review
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.
The FIXME is still lost.