Another AppendOnlyVec

This commit is contained in:
Oli Scherer 2023-03-14 12:30:16 +00:00
parent 7edd1d8799
commit a1d20cf7a2
3 changed files with 30 additions and 19 deletions

View File

@ -75,20 +75,31 @@ impl<T: Copy> AppendOnlyVec<T> {
#[cfg(parallel_compiler)]
return self.vec.get(i);
}
pub fn iter_enumerated(&self) -> impl Iterator<Item = (usize, T)> + '_ {
(0..)
.map(|i| (i, self.get(i)))
.take_while(|(_, o)| o.is_some())
.filter_map(|(i, o)| Some((i, o?)))
}
pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
(0..).map(|i| self.get(i)).take_while(|o| o.is_some()).filter_map(|o| o)
}
}
impl<T: Copy + PartialEq> AppendOnlyVec<T> {
pub fn contains(&self, val: T) -> bool {
for i in 0.. {
match self.get(i) {
None => return false,
Some(v) => {
if val == v {
return true;
}
}
}
}
false
self.iter_enumerated().any(|(_, v)| v == val)
}
}
impl<A: Copy> FromIterator<A> for AppendOnlyVec<A> {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
let this = Self::new();
for val in iter {
this.push(val);
}
this
}
}

View File

@ -185,7 +185,7 @@ impl CStore {
fn push_dependencies_in_postorder(&self, deps: &mut Vec<CrateNum>, cnum: CrateNum) {
if !deps.contains(&cnum) {
let data = self.get_crate_data(cnum);
for &dep in data.dependencies().iter() {
for dep in data.dependencies() {
if dep != cnum {
self.push_dependencies_in_postorder(deps, dep);
}
@ -605,7 +605,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
if cmeta.update_extern_crate(extern_crate) {
// Propagate the extern crate info to dependencies if it was updated.
let extern_crate = ExternCrate { dependency_of: cnum, ..extern_crate };
for &dep_cnum in cmeta.dependencies().iter() {
for dep_cnum in cmeta.dependencies() {
self.update_extern_crate(dep_cnum, extern_crate);
}
}

View File

@ -7,7 +7,7 @@ use rustc_ast as ast;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell};
use rustc_data_structures::sync::{AppendOnlyVec, Lock, Lrc, OnceCell};
use rustc_data_structures::unhash::UnhashMap;
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
@ -109,7 +109,7 @@ pub(crate) struct CrateMetadata {
/// IDs as they are seen from the current compilation session.
cnum_map: CrateNumMap,
/// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
dependencies: Lock<Vec<CrateNum>>,
dependencies: AppendOnlyVec<CrateNum>,
/// How to link (or not link) this crate to the currently compiled crate.
dep_kind: Lock<CrateDepKind>,
/// Filesystem location of this crate.
@ -1594,7 +1594,7 @@ impl CrateMetadata {
.collect();
let alloc_decoding_state =
AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
let dependencies = Lock::new(cnum_map.iter().cloned().collect());
let dependencies = cnum_map.iter().copied().collect();
// Pre-decode the DefPathHash->DefIndex table. This is a cheap operation
// that does not copy any data. It just does some data verification.
@ -1634,12 +1634,12 @@ impl CrateMetadata {
cdata
}
pub(crate) fn dependencies(&self) -> LockGuard<'_, Vec<CrateNum>> {
self.dependencies.borrow()
pub(crate) fn dependencies(&self) -> impl Iterator<Item = CrateNum> + '_ {
self.dependencies.iter()
}
pub(crate) fn add_dependency(&self, cnum: CrateNum) {
self.dependencies.borrow_mut().push(cnum);
self.dependencies.push(cnum);
}
pub(crate) fn update_extern_crate(&self, new_extern_crate: ExternCrate) -> bool {