Skip to content

Commit daee746

Browse files
committed
Add a usize-indexed append-only-vec
1 parent c7a3a94 commit daee746

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

compiler/rustc_data_structures/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
5151
pub use std::sync::atomic::Ordering;
5252
pub use std::sync::atomic::Ordering::SeqCst;
5353

54-
pub use vec::AppendOnlyVec;
54+
pub use vec::{AppendOnlyIndexVec, AppendOnlyVec};
5555

5656
mod vec;
5757

compiler/rustc_data_structures/src/sync/vec.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use std::marker::PhantomData;
22

33
use rustc_index::vec::Idx;
44

5-
pub struct AppendOnlyVec<I: Idx, T: Copy> {
5+
pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
66
#[cfg(not(parallel_compiler))]
77
vec: elsa::vec::FrozenVec<T>,
88
#[cfg(parallel_compiler)]
99
vec: elsa::sync::LockFreeFrozenVec<T>,
1010
_marker: PhantomData<fn(&I)>,
1111
}
1212

13-
impl<I: Idx, T: Copy> AppendOnlyVec<I, T> {
13+
impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
1414
pub fn new() -> Self {
1515
Self {
1616
#[cfg(not(parallel_compiler))]
@@ -39,3 +39,35 @@ impl<I: Idx, T: Copy> AppendOnlyVec<I, T> {
3939
return self.vec.get(i);
4040
}
4141
}
42+
43+
pub struct AppendOnlyVec<T: Copy> {
44+
#[cfg(not(parallel_compiler))]
45+
vec: elsa::vec::FrozenVec<T>,
46+
#[cfg(parallel_compiler)]
47+
vec: elsa::sync::LockFreeFrozenVec<T>,
48+
}
49+
50+
impl<T: Copy> AppendOnlyVec<T> {
51+
pub fn new() -> Self {
52+
Self {
53+
#[cfg(not(parallel_compiler))]
54+
vec: elsa::vec::FrozenVec::new(),
55+
#[cfg(parallel_compiler)]
56+
vec: elsa::sync::LockFreeFrozenVec::new(),
57+
}
58+
}
59+
60+
pub fn push(&self, val: T) {
61+
#[cfg(not(parallel_compiler))]
62+
self.vec.push(val);
63+
#[cfg(parallel_compiler)]
64+
self.vec.push(val)
65+
}
66+
67+
pub fn get(&self, i: usize) -> Option<T> {
68+
#[cfg(not(parallel_compiler))]
69+
return self.vec.get_copy(i);
70+
#[cfg(parallel_compiler)]
71+
return self.vec.get(i);
72+
}
73+
}

compiler/rustc_interface/src/queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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::{AppendOnlyVec, Lrc, OnceCell, RwLock, WorkerLocal};
10+
use rustc_data_structures::sync::{AppendOnlyIndexVec, 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;
@@ -215,7 +215,7 @@ impl<'tcx> Queries<'tcx> {
215215

216216
let cstore = RwLock::new(Box::new(CStore::new(sess)) as _);
217217
let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id()));
218-
let source_span = AppendOnlyVec::new();
218+
let source_span = AppendOnlyIndexVec::new();
219219
let _id = source_span.push(krate.spans.inner_span);
220220
debug_assert_eq!(_id, CRATE_DEF_ID);
221221
let untracked = Untracked { cstore, source_span, definitions };

compiler/rustc_session/src/cstore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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, AppendOnlyVec, MetadataRef, RwLock};
9+
use rustc_data_structures::sync::{self, AppendOnlyIndexVec, MetadataRef, RwLock};
1010
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
1111
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
1212
use rustc_span::hygiene::{ExpnHash, ExpnId};
@@ -257,6 +257,6 @@ pub type CrateStoreDyn = dyn CrateStore + sync::Sync + sync::Send;
257257
pub struct Untracked {
258258
pub cstore: RwLock<Box<CrateStoreDyn>>,
259259
/// Reference span for definitions.
260-
pub source_span: AppendOnlyVec<LocalDefId, Span>,
260+
pub source_span: AppendOnlyIndexVec<LocalDefId, Span>,
261261
pub definitions: RwLock<Definitions>,
262262
}

0 commit comments

Comments
 (0)