Skip to content

Commit a1d20cf

Browse files
committed
Another AppendOnlyVec
1 parent 7edd1d8 commit a1d20cf

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

compiler/rustc_data_structures/src/sync/vec.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,31 @@ impl<T: Copy> AppendOnlyVec<T> {
7575
#[cfg(parallel_compiler)]
7676
return self.vec.get(i);
7777
}
78+
79+
pub fn iter_enumerated(&self) -> impl Iterator<Item = (usize, T)> + '_ {
80+
(0..)
81+
.map(|i| (i, self.get(i)))
82+
.take_while(|(_, o)| o.is_some())
83+
.filter_map(|(i, o)| Some((i, o?)))
84+
}
85+
86+
pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
87+
(0..).map(|i| self.get(i)).take_while(|o| o.is_some()).filter_map(|o| o)
88+
}
7889
}
7990

8091
impl<T: Copy + PartialEq> AppendOnlyVec<T> {
8192
pub fn contains(&self, val: T) -> bool {
82-
for i in 0.. {
83-
match self.get(i) {
84-
None => return false,
85-
Some(v) => {
86-
if val == v {
87-
return true;
88-
}
89-
}
90-
}
93+
self.iter_enumerated().any(|(_, v)| v == val)
94+
}
95+
}
96+
97+
impl<A: Copy> FromIterator<A> for AppendOnlyVec<A> {
98+
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
99+
let this = Self::new();
100+
for val in iter {
101+
this.push(val);
91102
}
92-
false
103+
this
93104
}
94105
}

compiler/rustc_metadata/src/creader.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl CStore {
185185
fn push_dependencies_in_postorder(&self, deps: &mut Vec<CrateNum>, cnum: CrateNum) {
186186
if !deps.contains(&cnum) {
187187
let data = self.get_crate_data(cnum);
188-
for &dep in data.dependencies().iter() {
188+
for dep in data.dependencies() {
189189
if dep != cnum {
190190
self.push_dependencies_in_postorder(deps, dep);
191191
}
@@ -605,7 +605,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
605605
if cmeta.update_extern_crate(extern_crate) {
606606
// Propagate the extern crate info to dependencies if it was updated.
607607
let extern_crate = ExternCrate { dependency_of: cnum, ..extern_crate };
608-
for &dep_cnum in cmeta.dependencies().iter() {
608+
for dep_cnum in cmeta.dependencies() {
609609
self.update_extern_crate(dep_cnum, extern_crate);
610610
}
611611
}

compiler/rustc_metadata/src/rmeta/decoder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast as ast;
77
use rustc_data_structures::captures::Captures;
88
use rustc_data_structures::fx::FxHashMap;
99
use rustc_data_structures::svh::Svh;
10-
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
10+
use rustc_data_structures::sync::{AppendOnlyVec, Lock, Lrc, OnceCell};
1111
use rustc_data_structures::unhash::UnhashMap;
1212
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
1313
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
@@ -109,7 +109,7 @@ pub(crate) struct CrateMetadata {
109109
/// IDs as they are seen from the current compilation session.
110110
cnum_map: CrateNumMap,
111111
/// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
112-
dependencies: Lock<Vec<CrateNum>>,
112+
dependencies: AppendOnlyVec<CrateNum>,
113113
/// How to link (or not link) this crate to the currently compiled crate.
114114
dep_kind: Lock<CrateDepKind>,
115115
/// Filesystem location of this crate.
@@ -1594,7 +1594,7 @@ impl CrateMetadata {
15941594
.collect();
15951595
let alloc_decoding_state =
15961596
AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
1597-
let dependencies = Lock::new(cnum_map.iter().cloned().collect());
1597+
let dependencies = cnum_map.iter().copied().collect();
15981598

15991599
// Pre-decode the DefPathHash->DefIndex table. This is a cheap operation
16001600
// that does not copy any data. It just does some data verification.
@@ -1634,12 +1634,12 @@ impl CrateMetadata {
16341634
cdata
16351635
}
16361636

1637-
pub(crate) fn dependencies(&self) -> LockGuard<'_, Vec<CrateNum>> {
1638-
self.dependencies.borrow()
1637+
pub(crate) fn dependencies(&self) -> impl Iterator<Item = CrateNum> + '_ {
1638+
self.dependencies.iter()
16391639
}
16401640

16411641
pub(crate) fn add_dependency(&self, cnum: CrateNum) {
1642-
self.dependencies.borrow_mut().push(cnum);
1642+
self.dependencies.push(cnum);
16431643
}
16441644

16451645
pub(crate) fn update_extern_crate(&self, new_extern_crate: ExternCrate) -> bool {

0 commit comments

Comments
 (0)