Skip to content

Commit 60f92ed

Browse files
perf: Index crate items in parallel
When generating the JSON docs for a batch of crates, we index the item in each crate in parallel using rayon rather than one-by-one.
1 parent 86797ac commit 60f92ed

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

compiler/pavexc/src/rustdoc/queries.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -220,30 +220,42 @@ impl CrateCollection {
220220
self.package_graph.workspace().root().as_std_path(),
221221
)?;
222222

223-
for (package_id, krate) in results {
224-
let n_diagnostics = self.diagnostic_sink.len();
225-
let krate = Crate::index_raw(krate, package_id.to_owned(), &self.diagnostic_sink);
226-
227-
// No issues arose in the indexing phase.
228-
// Let's make sure to store them in the on-disk cache for next time.
229-
let cache_indexes = n_diagnostics == self.diagnostic_sink.len();
230-
let cache_key = RustdocCacheKey::new(&package_id, &self.package_graph);
231-
if let Err(e) =
232-
self.disk_cache
233-
.insert(&cache_key, &krate, cache_indexes, &self.package_graph)
234-
{
235-
log_error!(
236-
*e,
237-
level: tracing::Level::WARN,
238-
package_id = package_id.repr(),
239-
"Failed to store the computed JSON docs in the on-disk cache",
240-
);
223+
let package_graph = self.package_graph();
224+
let diagnostic_sink = &self.diagnostic_sink;
225+
for partial in results
226+
.into_par_iter()
227+
.map(move |(package_id, krate)| {
228+
let n_diagnostics = diagnostic_sink.len();
229+
let krate = Crate::index_raw(krate, package_id.to_owned(), diagnostic_sink);
230+
231+
// No issues arose in the indexing phase.
232+
// Let's make sure to store them in the on-disk cache for next time.
233+
//
234+
// TODO: Since we're indexing in parallel, the counter may have been incremented
235+
// by a different thread, signaling an issue with indexes for another crate.
236+
// It'd be enough to keep a thread-local counter to get an accurate yes/no,
237+
// but since we don't get false negatives it isn't a big deal.
238+
let cache_indexes = n_diagnostics == diagnostic_sink.len();
239+
(package_id, Box::new(krate), cache_indexes)
240+
})
241+
.collect_vec_list()
242+
{
243+
for (package_id, krate, cache_indexes) in partial {
244+
let cache_key = RustdocCacheKey::new(&package_id, package_graph);
245+
if let Err(e) =
246+
self.disk_cache
247+
.insert(&cache_key, &krate, cache_indexes, package_graph)
248+
{
249+
log_error!(
250+
*e,
251+
level: tracing::Level::WARN,
252+
package_id = package_id.repr(),
253+
"Failed to store the computed JSON docs in the on-disk cache",
254+
);
255+
}
256+
self.package_id2krate.insert(package_id, krate);
241257
}
242-
243-
self.package_id2krate
244-
.insert(package_id.to_owned(), Box::new(krate));
245258
}
246-
247259
Ok(())
248260
}
249261

0 commit comments

Comments
 (0)