Skip to content

Commit 836349e

Browse files
Paolo TranquilliCopilot
andcommitted
Rust: Prime only def maps needed by extracted files
Also synchronize the panic test on worker unwinding instead of a sleep. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 5d1bdb3a2b3c43ce80bf533edd8fc893
1 parent f4f037a commit 836349e

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

‎rust/extractor/src/main.rs‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,27 +259,43 @@ impl<'a> Extractor<'a> {
259259
}
260260
}
261261

262-
/// Computes the def maps of all crates in dependency order on `threads` threads.
262+
/// Computes the def maps of the crates of `files` and their dependencies in dependency order on
263+
/// `threads` threads.
263264
///
264265
/// Otherwise extraction workers compute them lazily, recursively including dependencies, which
265266
/// mostly serializes the workers on them.
266-
fn prime_def_maps(db: &RootDatabase, threads: usize) {
267+
fn prime_def_maps<'a>(
268+
db: &RootDatabase,
269+
vfs: &Vfs,
270+
files: impl IntoIterator<Item = &'a Path>,
271+
threads: usize,
272+
) {
267273
if threads <= 1 {
268274
return;
269275
}
270276
let before = Instant::now();
271-
let crates = ra_ap_hir::Crate::all(db);
272-
let index: HashMap<_, _> = crates.iter().enumerate().map(|(i, c)| (*c, i)).collect();
273-
let deps: Vec<Vec<usize>> = crates
274-
.iter()
275-
.map(|krate| {
276-
krate
277-
.dependencies(db)
278-
.iter()
279-
.filter_map(|dep| index.get(&dep.krate).copied())
280-
.collect()
281-
})
277+
let mut crates: Vec<ra_ap_hir::Crate> = files
278+
.into_iter()
279+
.filter_map(|file| path_to_file_id(file, vfs))
280+
.flat_map(|file_id| ra_ap_base_db::relevant_crates(db, file_id))
281+
.map(|&krate| krate.into())
282+
.unique()
282283
.collect();
284+
let mut index: HashMap<_, _> = crates.iter().enumerate().map(|(i, c)| (*c, i)).collect();
285+
let mut deps: Vec<Vec<usize>> = Vec::new();
286+
while deps.len() < crates.len() {
287+
let krate_deps = crates[deps.len()]
288+
.dependencies(db)
289+
.into_iter()
290+
.map(|dep| {
291+
*index.entry(dep.krate).or_insert_with(|| {
292+
crates.push(dep.krate);
293+
crates.len() - 1
294+
})
295+
})
296+
.collect();
297+
deps.push(krate_deps);
298+
}
283299
parallel::for_each_in_dependency_order(
284300
threads,
285301
&deps,
@@ -393,12 +409,12 @@ fn main() -> anyhow::Result<()> {
393409
extractor
394410
.steps
395411
.push(ExtractionStep::crate_graph(before_crate_graph));
396-
prime_def_maps(&db, threads);
397412
let mut items: Vec<(&Path, bool)> = files.iter().map(|f| (*f, true)).collect();
398413
// Fast path when there is no ambiguity in dependency extraction.
399414
if dependency_extraction_ambiguity {
400415
items.extend(dependency_files(&db, &vfs, &source_files).map(|(_, f)| (f, false)));
401416
}
417+
prime_def_maps(&db, &vfs, items.iter().map(|(f, _)| *f), threads);
402418
debug_assert!(items.iter().map(|(f, _)| f).all_unique());
403419
extractor.run_parallel(
404420
&db,

‎rust/extractor/src/parallel.rs‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -398,28 +398,35 @@ mod tests {
398398

399399
#[test]
400400
fn dependency_order_panic_propagates() {
401+
/// Flags when a worker's state is dropped by unwinding, which happens after the
402+
/// scheduler has recorded the panic.
403+
struct OnUnwind<'a>(&'a AtomicBool);
404+
impl Drop for OnUnwind<'_> {
405+
fn drop(&mut self) {
406+
if std::thread::panicking() {
407+
self.0.store(true, Ordering::SeqCst);
408+
}
409+
}
410+
}
401411
// `49` is started first, and the other ready nodes are queued behind it
402412
let deps: Vec<_> = (0..100)
403413
.map(|i| if i < 50 { vec![] } else { vec![49] })
404414
.collect();
405415
let started = AtomicUsize::new(0);
406-
let panicking = AtomicBool::new(false);
416+
let unwound = AtomicBool::new(false);
407417
let result = std::panic::catch_unwind(|| {
408418
for_each_in_dependency_order(
409419
2,
410420
&deps,
411-
|| (),
412-
|(), node| {
421+
|| OnUnwind(&unwound),
422+
|_, node| {
413423
started.fetch_add(1, Ordering::SeqCst);
414424
if node == 49 {
415-
panicking.store(true, Ordering::SeqCst);
416425
std::panic::panic_any(node);
417426
}
418-
// give the panic time to reach the scheduler
419-
while !panicking.load(Ordering::SeqCst) {
427+
while !unwound.load(Ordering::SeqCst) {
420428
std::thread::yield_now();
421429
}
422-
std::thread::sleep(std::time::Duration::from_millis(200));
423430
},
424431
);
425432
});

0 commit comments

Comments
 (0)