Skip to content

Commit

Permalink
Rollup merge of #63750 - eddyb:lazy-seq, r=Zoxc
Browse files Browse the repository at this point in the history
rustc_metadata: replace LazySeq<T> with Lazy<[T]>.

Part of #59953, split out for separate landing (see #59953 (comment)).

r? @Zoxc
  • Loading branch information
Centril committed Aug 20, 2019
2 parents 234614a + e7ceaa9 commit e3e8b7f
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 296 deletions.
8 changes: 4 additions & 4 deletions src/librustc_metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ pub struct CrateMetadata {
pub alloc_decoding_state: AllocDecodingState,

// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this
// lifetime is only used behind `Lazy` / `LazySeq`, and therefore
// acts like an universal (`for<'tcx>`), that is paired up with
// whichever `TyCtxt` is being used to decode those values.
// lifetime is only used behind `Lazy`, and therefore acts like an
// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
// is being used to decode those values.
pub root: schema::CrateRoot<'static>,

/// For each definition in this crate, we encode a key. When the
Expand All @@ -80,7 +80,7 @@ pub struct CrateMetadata {
/// compilation support.
pub def_path_table: Lrc<DefPathTable>,

pub trait_impls: FxHashMap<(u32, DefIndex), schema::LazySeq<DefIndex>>,
pub trait_impls: FxHashMap<(u32, DefIndex), schema::Lazy<[DefIndex]>>,

pub dep_kind: Lock<DepKind>,
pub source: CrateSource,
Expand Down
35 changes: 19 additions & 16 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ impl<'a, 'tcx, T: Decodable> Lazy<T> {
}
}

impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> LazySeq<T> {
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> {
pub fn decode<M: Metadata<'a, 'tcx>>(
self,
meta: M,
) -> impl ExactSizeIterator<Item = T> + Captures<'a> + Captures<'tcx> + 'x {
let mut dcx = meta.decoder(self.position);
dcx.lazy_state = LazyState::NodeStart(self.position);
(0..self.len).map(move |_| T::decode(&mut dcx).unwrap())
(0..self.meta).map(move |_| T::decode(&mut dcx).unwrap())
}
}

Expand All @@ -154,18 +154,22 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
self.cdata.expect("missing CrateMetadata in DecodeContext")
}

fn read_lazy_distance(&mut self, min_size: usize) -> Result<usize, <Self as Decoder>::Error> {
fn read_lazy_with_meta<T: ?Sized + LazyMeta>(
&mut self,
meta: T::Meta,
) -> Result<Lazy<T>, <Self as Decoder>::Error> {
let min_size = T::min_size(meta);
let distance = self.read_usize()?;
let position = match self.lazy_state {
LazyState::NoNode => bug!("read_lazy_distance: outside of a metadata node"),
LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
LazyState::NodeStart(start) => {
assert!(distance + min_size <= start);
start - distance - min_size
}
LazyState::Previous(last_min_end) => last_min_end + distance,
};
self.lazy_state = LazyState::Previous(position + min_size);
Ok(position)
Ok(Lazy::from_position_and_meta(position, meta))
}
}

Expand Down Expand Up @@ -230,19 +234,18 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {

impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
Ok(Lazy::with_position(self.read_lazy_distance(Lazy::<T>::min_size())?))
self.read_lazy_with_meta(())
}
}

impl<'a, 'tcx, T> SpecializedDecoder<LazySeq<T>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<LazySeq<T>, Self::Error> {
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> {
let len = self.read_usize()?;
let position = if len == 0 {
0
if len == 0 {
Ok(Lazy::empty())
} else {
self.read_lazy_distance(LazySeq::<T>::min_size(len))?
};
Ok(LazySeq::with_position_and_length(position, len))
self.read_lazy_with_meta(len)
}
}
}

Expand Down Expand Up @@ -378,7 +381,7 @@ impl<'tcx> MetadataBlob {
}

pub fn get_rustc_version(&self) -> String {
Lazy::with_position(METADATA_HEADER.len() + 4).decode(self)
Lazy::<String>::from_position(METADATA_HEADER.len() + 4).decode(self)
}

pub fn get_root(&self) -> CrateRoot<'tcx> {
Expand All @@ -387,7 +390,7 @@ impl<'tcx> MetadataBlob {
let pos = (((slice[offset + 0] as u32) << 24) | ((slice[offset + 1] as u32) << 16) |
((slice[offset + 2] as u32) << 8) |
((slice[offset + 3] as u32) << 0)) as usize;
Lazy::with_position(pos).decode(self)
Lazy::<CrateRoot<'tcx>>::from_position(pos).decode(self)
}

pub fn list_crate_metadata(&self,
Expand Down Expand Up @@ -1140,7 +1143,7 @@ impl<'a, 'tcx> CrateMetadata {
EntryKind::Fn(data) |
EntryKind::ForeignFn(data) => data.decode(self).arg_names,
EntryKind::Method(data) => data.decode(self).fn_data.arg_names,
_ => LazySeq::empty(),
_ => Lazy::empty(),
};
arg_names.decode(self).collect()
}
Expand Down
Loading

0 comments on commit e3e8b7f

Please sign in to comment.