Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ jobs:
- name: Compilation
shell: bash
run: cargo check
- name: Unit tests
shell: bash
run: cargo test
rust-codegen:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions rust/extractor/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl Display for HumanReadableDuration {
#[derive(Debug, Default, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct DurationsSummary {
/// Summed over all extraction threads, so with more than one thread these may exceed `total`.
#[serde(flatten)]
durations: HashMap<ExtractionStepKind, HumanReadableDuration>,
total: HumanReadableDuration,
Expand Down
118 changes: 92 additions & 26 deletions rust/extractor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::trap::TrapId;
use anyhow::Context;
use archive::Archiver;
use configuration::{DependencyFiles, dependency_files, manifest_parent};
use itertools::Itertools;
use ra_ap_hir::Semantics;
use ra_ap_ide_db::RootDatabase;
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
Expand All @@ -30,6 +31,7 @@ mod configuration;
mod crate_graph;
mod diagnostics;
pub mod generated;
mod parallel;
mod qltest;
mod rust_analyzer;
mod toolchain;
Expand Down Expand Up @@ -194,6 +196,33 @@ impl<'a> Extractor<'a> {
Ok(())
}

/// Runs `work` on `items` using up to `threads` workers, each with its own `Extractor` and
/// `Semantics` over a clone of `db` (sharing its query storage). Returns results in item order.
fn run_parallel<T: Sync, R: Send>(
&mut self,
db: &RootDatabase,
vfs: &Vfs,
threads: usize,
items: &[T],
work: impl for<'s> Fn(&mut Extractor<'a>, &'s Semantics<'s, RootDatabase>, &'s Vfs, &T) -> R
+ Sync,
) -> Vec<R> {
let (archiver, traps) = (self.archiver, self.traps);
let (steps, results) = parallel::parallel_map(
threads,
items,
|| db.clone(),
|db, drive| {
let semantics = Semantics::new(&db);
let mut extractor = Extractor::new(archiver, traps);
drive(&mut |item| work(&mut extractor, &semantics, vfs, item));
extractor.steps
},
);
self.steps.extend(steps.into_iter().flatten());
results
}

pub fn find_manifests(&mut self, files: &[PathBuf]) -> anyhow::Result<Vec<ProjectManifest>> {
let before = Instant::now();
let ret = rust_analyzer::find_project_manifests(files);
Expand Down Expand Up @@ -288,6 +317,8 @@ fn main() -> anyhow::Result<()> {
// [1]: https://github.com/rust-lang/rust-analyzer/issues/18894#issuecomment-2580014730
dunce::canonicalize(&file).unwrap_or(file)
})
// each file must be extracted at most once at a time, as trap and archive writes are not atomic
.unique()
.collect::<Vec<_>>();
let manifests = extractor.find_manifests(&files)?;
let workspaces = extractor.group_files(&manifests, &files);
Expand All @@ -305,6 +336,8 @@ fn main() -> anyhow::Result<()> {
SourceKind::Source
};
let source_files = HashSet::from_iter(files.iter().cloned());
let threads = parallel::extraction_threads();
info!("extraction threads: {threads}");

let mut dependencies = DependencyFiles::default();

Expand All @@ -321,19 +354,32 @@ fn main() -> anyhow::Result<()> {
extractor
.steps
.push(ExtractionStep::crate_graph(before_crate_graph));
let semantics = Semantics::new(&db);
for file in files {
match extractor.load_source(file, &semantics, &vfs) {
Ok(()) => extractor.extract_with_semantics(file, &semantics, &vfs, source_mode),
Err(e) => extractor.extract_without_semantics(file, source_mode, e),
}
}
let mut items: Vec<(&Path, bool)> = files.iter().map(|f| (*f, true)).collect();
// Fast path when there is no ambiguity in dependency extraction.
if dependency_extraction_ambiguity {
for (_, file) in dependency_files(&db, &vfs, &source_files) {
extractor.extract_with_semantics(file, &semantics, &vfs, library_mode);
}
} else {
items.extend(dependency_files(&db, &vfs, &source_files).map(|(_, f)| (f, false)));
}
debug_assert!(items.iter().map(|(f, _)| f).all_unique());
extractor.run_parallel(
&db,
&vfs,
threads,
&items,
|extractor, semantics, vfs, &(file, is_source)| {
if !is_source {
extractor.extract_with_semantics(file, semantics, vfs, library_mode);
return;
}
match extractor.load_source(file, semantics, vfs) {
Ok(()) => {
extractor.extract_with_semantics(file, semantics, vfs, source_mode);
}
Err(e) => extractor.extract_without_semantics(file, source_mode, e),
}
},
);
if !dependency_extraction_ambiguity {
let semantics = Semantics::new(&db);
dependencies.add_manifest(manifest, &semantics, &vfs, &source_files);
}
} else {
Expand All @@ -359,21 +405,41 @@ fn main() -> anyhow::Result<()> {
if let Some((db, vfs)) =
extractor.load_manifest(manifest, &cargo_config, &load_cargo_config)
{
let semantics = Semantics::new(&db);
for file in files {
// A richer configuration can disable a file when using cfg
// options like `#[cfg(not(feature = "foo"))]`. When this is
// the case `file_to_module_def` will return `None` for the
// file. When this is the case and there are alternatives we
// do not extract the file yet. Instead the file will be
// considered again in the next iteration of the loop.
if !dependencies.has_alternative(&file)
|| path_to_file_id(&file, &vfs)
.and_then(|id| semantics.file_to_module_def(id))
.is_some()
{
extractor.extract_with_semantics(&file, &semantics, &vfs, library_mode);
dependencies.extracted(&file);
// `has_alternative` is evaluated upfront, which is equivalent to evaluating it
// just before each extraction as each file occurs at most once in `selected`.
let items: Vec<_> = files
.into_iter()
.map(|file| {
let has_alternative = dependencies.has_alternative(&file);
(file, has_alternative)
})
.collect();
debug_assert!(items.iter().map(|(f, _)| f).all_unique());
let extracted = extractor.run_parallel(
&db,
&vfs,
threads,
&items,
|extractor, semantics, vfs, (file, has_alternative)| {
// A richer configuration can disable a file when using cfg
// options like `#[cfg(not(feature = "foo"))]`. When this is
// the case `file_to_module_def` will return `None` for the
// file. When this is the case and there are alternatives we
// do not extract the file yet. Instead the file will be
// considered again in the next iteration of the loop.
let extract = !has_alternative
|| path_to_file_id(file, vfs)
.and_then(|id| semantics.file_to_module_def(id))
.is_some();
if extract {
extractor.extract_with_semantics(file, semantics, vfs, library_mode);
}
extract
},
);
for ((file, _), extracted) in items.iter().zip(extracted) {
if extracted {
dependencies.extracted(file);
}
}
} else {
Expand Down
Loading
Loading