Skip to content

Commit decfb4d

Browse files
committed
Use a lock-free datastructure for source_span
1 parent a04e787 commit decfb4d

File tree

10 files changed

+65
-12
lines changed

10 files changed

+65
-12
lines changed

Cargo.lock

+10
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,15 @@ dependencies = [
13781378
"zeroize",
13791379
]
13801380

1381+
[[package]]
1382+
name = "elsa"
1383+
version = "1.8.0"
1384+
source = "registry+https://github.com/rust-lang/crates.io-index"
1385+
checksum = "f74077c3c3aedb99a2683919698285596662518ea13e5eedcf8bdd43b0d0453b"
1386+
dependencies = [
1387+
"stable_deref_trait",
1388+
]
1389+
13811390
[[package]]
13821391
name = "ena"
13831392
version = "0.14.0"
@@ -3882,6 +3891,7 @@ dependencies = [
38823891
"arrayvec 0.7.0",
38833892
"bitflags",
38843893
"cfg-if",
3894+
"elsa",
38853895
"ena",
38863896
"indexmap",
38873897
"jobserver",

compiler/rustc_data_structures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ stacker = "0.1.15"
3131
tempfile = "3.2"
3232
thin-vec = "0.2.9"
3333
tracing = "0.1"
34+
elsa = "1.8"
3435

3536
[dependencies.parking_lot]
3637
version = "0.11"

compiler/rustc_data_structures/src/sync.rs

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
2626
pub use std::sync::atomic::Ordering;
2727
pub use std::sync::atomic::Ordering::SeqCst;
2828

29+
pub use vec::AppendOnlyVec;
30+
31+
mod vec;
32+
2933
cfg_if! {
3034
if #[cfg(not(parallel_compiler))] {
3135
pub auto trait Send {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::marker::PhantomData;
2+
3+
use rustc_index::vec::Idx;
4+
5+
pub struct AppendOnlyVec<I: Idx, T: Copy> {
6+
#[cfg(not(parallel_compiler))]
7+
vec: elsa::vec::FrozenVec<T>,
8+
#[cfg(parallel_compiler)]
9+
vec: elsa::sync::LockFreeFrozenVec<T>,
10+
_marker: PhantomData<fn(&I)>,
11+
}
12+
13+
impl<I: Idx, T: Copy> AppendOnlyVec<I, T> {
14+
pub fn new() -> Self {
15+
Self {
16+
#[cfg(not(parallel_compiler))]
17+
vec: elsa::vec::FrozenVec::new(),
18+
#[cfg(parallel_compiler)]
19+
vec: elsa::sync::LockFreeFrozenVec::new(),
20+
_marker: PhantomData,
21+
}
22+
}
23+
24+
pub fn push(&self, val: T) -> I {
25+
#[cfg(not(parallel_compiler))]
26+
let i = self.vec.len();
27+
#[cfg(not(parallel_compiler))]
28+
self.vec.push(val);
29+
#[cfg(parallel_compiler)]
30+
let i = self.vec.push(val);
31+
I::new(i)
32+
}
33+
34+
pub fn get(&self, i: I) -> Option<T> {
35+
let i = i.index();
36+
#[cfg(not(parallel_compiler))]
37+
return self.vec.get_copy(i);
38+
#[cfg(parallel_compiler)]
39+
return self.vec.get(i);
40+
}
41+
}

compiler/rustc_interface/src/queries.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ use rustc_codegen_ssa::traits::CodegenBackend;
77
use rustc_codegen_ssa::CodegenResults;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
10-
use rustc_data_structures::sync::{Lrc, OnceCell, RwLock, WorkerLocal};
10+
use rustc_data_structures::sync::{AppendOnlyVec, Lrc, OnceCell, RwLock, WorkerLocal};
1111
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
1212
use rustc_hir::definitions::Definitions;
1313
use rustc_incremental::DepGraphFuture;
14-
use rustc_index::vec::IndexVec;
1514
use rustc_lint::LintStore;
1615
use rustc_metadata::creader::CStore;
1716
use rustc_middle::arena::Arena;
@@ -195,10 +194,9 @@ impl<'tcx> Queries<'tcx> {
195194

196195
let cstore = RwLock::new(Box::new(CStore::new(sess)) as _);
197196
let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id()));
198-
let mut source_span = IndexVec::default();
197+
let source_span = AppendOnlyVec::new();
199198
let _id = source_span.push(krate.spans.inner_span);
200199
debug_assert_eq!(_id, CRATE_DEF_ID);
201-
let source_span = RwLock::new(source_span);
202200
let untracked = Untracked { cstore, source_span, definitions };
203201

204202
let qcx = passes::create_global_ctxt(

compiler/rustc_middle/src/ty/context.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ impl<'tcx> TyCtxt<'tcx> {
10301030
/// system if the result is otherwise tracked through queries
10311031
#[inline]
10321032
pub fn source_span_untracked(self, def_id: LocalDefId) -> Span {
1033-
self.untracked.source_span.read().get(def_id).copied().unwrap_or(DUMMY_SP)
1033+
self.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP)
10341034
}
10351035

10361036
#[inline(always)]
@@ -2521,6 +2521,5 @@ pub fn provide(providers: &mut ty::query::Providers) {
25212521
// We want to check if the panic handler was defined in this crate
25222522
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
25232523
};
2524-
providers.source_span =
2525-
|tcx, def_id| tcx.untracked.source_span.read().get(def_id).copied().unwrap_or(DUMMY_SP);
2524+
providers.source_span = |tcx, def_id| tcx.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP);
25262525
}

compiler/rustc_query_system/src/ich/hcx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
146146

147147
#[inline]
148148
fn def_span(&self, def_id: LocalDefId) -> Span {
149-
*self.untracked.source_span.read().get(def_id).unwrap_or(&DUMMY_SP)
149+
self.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP)
150150
}
151151

152152
#[inline]

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ impl<'tcx> Resolver<'_, 'tcx> {
11561156

11571157
// A relative span's parent must be an absolute span.
11581158
debug_assert_eq!(span.data_untracked().parent, None);
1159-
let _id = self.tcx.untracked().source_span.write().push(span);
1159+
let _id = self.tcx.untracked().source_span.push(span);
11601160
debug_assert_eq!(_id, def_id);
11611161

11621162
// Some things for which we allocate `LocalDefId`s don't correspond to

compiler/rustc_session/src/cstore.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ use crate::search_paths::PathKind;
66
use crate::utils::NativeLibKind;
77
use crate::Session;
88
use rustc_ast as ast;
9-
use rustc_data_structures::sync::{self, MetadataRef, RwLock};
9+
use rustc_data_structures::sync::{self, AppendOnlyVec, MetadataRef, RwLock};
1010
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
1111
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
12-
use rustc_index::vec::IndexVec;
1312
use rustc_span::hygiene::{ExpnHash, ExpnId};
1413
use rustc_span::symbol::Symbol;
1514
use rustc_span::Span;
@@ -255,6 +254,6 @@ pub type CrateStoreDyn = dyn CrateStore + sync::Sync + sync::Send;
255254
pub struct Untracked {
256255
pub cstore: RwLock<Box<CrateStoreDyn>>,
257256
/// Reference span for definitions.
258-
pub source_span: RwLock<IndexVec<LocalDefId, Span>>,
257+
pub source_span: AppendOnlyVec<LocalDefId, Span>,
259258
pub definitions: RwLock<Definitions>,
260259
}

src/tools/tidy/src/deps.rs

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
120120
"dissimilar",
121121
"dlmalloc",
122122
"either",
123+
"elsa",
123124
"ena",
124125
"expect-test",
125126
"fallible-iterator", // dependency of `thorin`

0 commit comments

Comments
 (0)