Skip to content

Commit 73456f6

Browse files
committed
Optimize for the case with on diagnostics
1 parent 5cd83dc commit 73456f6

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

src/librustc/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ impl DepGraph {
813813

814814
// Promote the previous diagnostics to the current session.
815815
tcx.queries.on_disk_cache
816-
.store_diagnostics(dep_node_index, diagnostics.clone());
816+
.store_diagnostics(dep_node_index, Box::new(diagnostics.clone()));
817817

818818
for diagnostic in diagnostics {
819819
DiagnosticBuilder::new_diagnostic(handle, diagnostic).emit();

src/librustc/ty/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,11 @@ pub mod tls {
19681968
with_context_opt(|icx| {
19691969
if let Some(icx) = icx {
19701970
if let Some(ref query) = icx.query {
1971-
query.diagnostics.lock().push(diagnostic.clone());
1971+
let mut diagnostics = query.diagnostics.lock();
1972+
if diagnostics.is_none() {
1973+
*diagnostics = Some(Box::new(Vec::new()));
1974+
}
1975+
diagnostics.as_mut().unwrap().push(diagnostic.clone());
19721976
}
19731977
}
19741978
})

src/librustc/ty/query/job.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct QueryJob<'tcx> {
6565
pub parent: Option<Lrc<QueryJob<'tcx>>>,
6666

6767
/// Diagnostic messages which are emitted while the query executes
68-
pub diagnostics: Lock<Vec<Diagnostic>>,
68+
pub diagnostics: Lock<Option<Box<Vec<Diagnostic>>>>,
6969

7070
/// The latch which is used to wait on this job
7171
#[cfg(parallel_queries)]
@@ -76,19 +76,20 @@ impl<'tcx> QueryJob<'tcx> {
7676
/// Creates a new query job
7777
pub fn new(info: QueryInfo<'tcx>, parent: Option<Lrc<QueryJob<'tcx>>>) -> Self {
7878
QueryJob {
79-
diagnostics: Lock::new(Vec::new()),
79+
diagnostics: Lock::new(None),
8080
info,
8181
parent,
8282
#[cfg(parallel_queries)]
8383
latch: QueryLatch::new(),
8484
}
8585
}
8686

87-
pub fn extract_diagnostics(&self) -> Vec<Diagnostic> {
87+
#[inline(always)]
88+
pub fn extract_diagnostics(&self) -> Option<Box<Vec<Diagnostic>>> {
8889
// FIXME: Find a way to remove this lock access since we should have
8990
// ownership of the content back now. Other crates may free the Lrc though
9091
// and the, but only after we replace this.
91-
mem::replace(&mut *self.diagnostics.lock(), Vec::new())
92+
mem::replace(&mut *self.diagnostics.lock(), None)
9293
}
9394

9495
/// Awaits for the query job to complete.

src/librustc/ty/query/on_disk_cache.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,13 @@ impl<'sess> OnDiskCache<'sess> {
351351
/// Store a diagnostic emitted during the current compilation session.
352352
/// Anything stored like this will be available via `load_diagnostics` in
353353
/// the next compilation session.
354+
#[inline(never)]
355+
#[cold]
354356
pub fn store_diagnostics(&self,
355357
dep_node_index: DepNodeIndex,
356-
diagnostics: Vec<Diagnostic>) {
358+
diagnostics: Box<Vec<Diagnostic>>) {
357359
let mut current_diagnostics = self.current_diagnostics.borrow_mut();
358-
let prev = current_diagnostics.insert(dep_node_index, diagnostics);
360+
let prev = current_diagnostics.insert(dep_node_index, *diagnostics);
359361
debug_assert!(prev.is_none());
360362
}
361363

@@ -377,13 +379,15 @@ impl<'sess> OnDiskCache<'sess> {
377379
/// Since many anonymous queries can share the same `DepNode`, we aggregate
378380
/// them -- as opposed to regular queries where we assume that there is a
379381
/// 1:1 relationship between query-key and `DepNode`.
382+
#[inline(never)]
383+
#[cold]
380384
pub fn store_diagnostics_for_anon_node(&self,
381385
dep_node_index: DepNodeIndex,
382-
mut diagnostics: Vec<Diagnostic>) {
386+
mut diagnostics: Box<Vec<Diagnostic>>) {
383387
let mut current_diagnostics = self.current_diagnostics.borrow_mut();
384388

385389
let x = current_diagnostics.entry(dep_node_index).or_insert_with(|| {
386-
mem::replace(&mut diagnostics, Vec::new())
390+
mem::replace(&mut *diagnostics, Vec::new())
387391
});
388392

389393
x.extend(diagnostics.into_iter());

src/librustc/ty/query/plumbing.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,10 @@ impl<'a, 'gcx> TyCtxt<'a, 'gcx, 'gcx> {
500500

501501
self.dep_graph.read_index(dep_node_index);
502502

503-
self.queries.on_disk_cache
504-
.store_diagnostics_for_anon_node(dep_node_index, job.job.extract_diagnostics());
503+
if let Some(diagnostics) = job.job.extract_diagnostics() {
504+
self.queries.on_disk_cache
505+
.store_diagnostics_for_anon_node(dep_node_index, diagnostics);
506+
}
505507

506508
job.complete(&result, dep_node_index);
507509

@@ -664,8 +666,10 @@ impl<'a, 'gcx> TyCtxt<'a, 'gcx, 'gcx> {
664666
}
665667

666668
if dep_node.kind != ::dep_graph::DepKind::Null {
667-
self.queries.on_disk_cache
668-
.store_diagnostics(dep_node_index, job.job.extract_diagnostics());
669+
if let Some(diagnostics) = job.job.extract_diagnostics() {
670+
self.queries.on_disk_cache
671+
.store_diagnostics(dep_node_index, diagnostics);
672+
}
669673
}
670674

671675
job.complete(&result, dep_node_index);

0 commit comments

Comments
 (0)