rustc_metadata: replace Lazy<[Table<T>]> with Lazy<Table<T>>.
This commit is contained in:
parent
ffd18fc22c
commit
dd1264e90a
4 changed files with 46 additions and 18 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use crate::cstore::{self, CrateMetadata, MetadataBlob};
|
||||
use crate::schema::*;
|
||||
use crate::table::Table;
|
||||
|
||||
use rustc_index::vec::IndexVec;
|
||||
use rustc_data_structures::sync::{Lrc, ReadGuard};
|
||||
|
|
@ -28,7 +29,7 @@ use std::mem;
|
|||
use std::num::NonZeroUsize;
|
||||
use std::u32;
|
||||
|
||||
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
|
||||
use rustc_serialize::{Decodable, Decoder, Encodable, SpecializedDecoder, opaque};
|
||||
use syntax::attr;
|
||||
use syntax::ast::{self, Ident};
|
||||
use syntax::source_map::{self, respan, Spanned};
|
||||
|
|
@ -130,7 +131,7 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx, T: Decodable> Lazy<T> {
|
||||
impl<'a, 'tcx, T: Encodable + Decodable> Lazy<T> {
|
||||
crate fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> T {
|
||||
let mut dcx = meta.decoder(self.position.get());
|
||||
dcx.lazy_state = LazyState::NodeStart(self.position);
|
||||
|
|
@ -138,7 +139,7 @@ impl<'a, 'tcx, T: Decodable> Lazy<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> {
|
||||
impl<'a: 'x, 'tcx: 'x, 'x, T: Encodable + Decodable> Lazy<[T]> {
|
||||
crate fn decode<M: Metadata<'a, 'tcx>>(
|
||||
self,
|
||||
meta: M,
|
||||
|
|
@ -237,13 +238,13 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
|
||||
impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
|
||||
fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
|
||||
self.read_lazy_with_meta(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
|
||||
impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
|
||||
fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> {
|
||||
let len = self.read_usize()?;
|
||||
if len == 0 {
|
||||
|
|
@ -254,6 +255,14 @@ impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<Table<T>>> for DecodeContext<'a, 'tcx>
|
||||
where T: LazyMeta<Meta = ()>,
|
||||
{
|
||||
fn specialized_decode(&mut self) -> Result<Lazy<Table<T>>, Self::Error> {
|
||||
let len = self.read_usize()?;
|
||||
self.read_lazy_with_meta(len)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> SpecializedDecoder<DefId> for DecodeContext<'a, 'tcx> {
|
||||
#[inline]
|
||||
|
|
|
|||
|
|
@ -98,13 +98,13 @@ impl<'tcx> Encoder for EncodeContext<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
|
||||
impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
|
||||
fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> {
|
||||
self.emit_lazy_distance(*lazy)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
|
||||
impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
|
||||
fn specialized_encode(&mut self, lazy: &Lazy<[T]>) -> Result<(), Self::Error> {
|
||||
self.emit_usize(lazy.meta)?;
|
||||
if lazy.meta == 0 {
|
||||
|
|
@ -114,6 +114,15 @@ impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx, T> SpecializedEncoder<Lazy<Table<T>>> for EncodeContext<'tcx>
|
||||
where T: LazyMeta<Meta = ()>,
|
||||
{
|
||||
fn specialized_encode(&mut self, lazy: &Lazy<Table<T>>) -> Result<(), Self::Error> {
|
||||
self.emit_usize(lazy.meta)?;
|
||||
self.emit_lazy_distance(*lazy)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'tcx> {
|
||||
#[inline]
|
||||
fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
|
||||
|
|
@ -258,7 +267,7 @@ impl<T: Encodable> EncodeContentsForLazy<T> for T {
|
|||
}
|
||||
}
|
||||
|
||||
impl<I, T> EncodeContentsForLazy<[T]> for I
|
||||
impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I
|
||||
where I: IntoIterator,
|
||||
I::Item: EncodeContentsForLazy<T>,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple};
|
|||
use rustc_index::vec::IndexVec;
|
||||
use rustc_data_structures::svh::Svh;
|
||||
|
||||
use rustc_serialize::Encodable;
|
||||
use syntax::{ast, attr};
|
||||
use syntax::edition::Edition;
|
||||
use syntax::symbol::Symbol;
|
||||
|
|
@ -53,7 +54,7 @@ crate trait LazyMeta {
|
|||
fn min_size(meta: Self::Meta) -> usize;
|
||||
}
|
||||
|
||||
impl<T> LazyMeta for T {
|
||||
impl<T: Encodable> LazyMeta for T {
|
||||
type Meta = ();
|
||||
|
||||
fn min_size(_: ()) -> usize {
|
||||
|
|
@ -62,7 +63,7 @@ impl<T> LazyMeta for T {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> LazyMeta for [T] {
|
||||
impl<T: Encodable> LazyMeta for [T] {
|
||||
type Meta = usize;
|
||||
|
||||
fn min_size(len: usize) -> usize {
|
||||
|
|
@ -118,13 +119,13 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Lazy<T> {
|
||||
impl<T: Encodable> Lazy<T> {
|
||||
crate fn from_position(position: NonZeroUsize) -> Lazy<T> {
|
||||
Lazy::from_position_and_meta(position, ())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Lazy<[T]> {
|
||||
impl<T: Encodable> Lazy<[T]> {
|
||||
crate fn empty() -> Lazy<[T]> {
|
||||
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
|
||||
}
|
||||
|
|
@ -160,6 +161,7 @@ crate enum LazyState {
|
|||
// manually, instead of relying on the default, to get the correct variance.
|
||||
// Only needed when `T` itself contains a parameter (e.g. `'tcx`).
|
||||
macro_rules! Lazy {
|
||||
(Table<$T:ty>) => {Lazy<Table<$T>, usize>};
|
||||
([$T:ty]) => {Lazy<[$T], usize>};
|
||||
($T:ty) => {Lazy<$T, ()>};
|
||||
}
|
||||
|
|
@ -194,7 +196,7 @@ crate struct CrateRoot<'tcx> {
|
|||
pub exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]),
|
||||
pub interpret_alloc_index: Lazy<[u32]>,
|
||||
|
||||
pub entries_table: Lazy!([Table<Entry<'tcx>>]),
|
||||
pub entries_table: Lazy!(Table<Entry<'tcx>>),
|
||||
|
||||
/// The DefIndex's of any proc macros delcared by
|
||||
/// this crate
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::schema::*;
|
||||
|
||||
use rustc::hir::def_id::{DefId, DefIndex};
|
||||
use rustc_serialize::opaque::Encoder;
|
||||
use rustc_serialize::{Encodable, opaque::Encoder};
|
||||
use std::convert::TryInto;
|
||||
use std::marker::PhantomData;
|
||||
use std::num::NonZeroUsize;
|
||||
|
|
@ -73,12 +73,12 @@ impl FixedSizeEncoding for u32 {
|
|||
/// (e.g. while visiting the definitions of a crate), and on-demand decoding
|
||||
/// of specific indices (e.g. queries for per-definition data).
|
||||
/// Similar to `Vec<Lazy<T>>`, but with zero-copy decoding.
|
||||
crate struct Table<T> {
|
||||
crate struct Table<T: LazyMeta<Meta = ()>> {
|
||||
positions: Vec<u8>,
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> Table<T> {
|
||||
impl<T: LazyMeta<Meta = ()>> Table<T> {
|
||||
crate fn new(max_index: usize) -> Self {
|
||||
Table {
|
||||
positions: vec![0; max_index * 4],
|
||||
|
|
@ -105,7 +105,7 @@ impl<T> Table<T> {
|
|||
position.write_to_bytes_at(positions, array_index)
|
||||
}
|
||||
|
||||
crate fn encode(&self, buf: &mut Encoder) -> Lazy<[Self]> {
|
||||
crate fn encode(&self, buf: &mut Encoder) -> Lazy<Self> {
|
||||
let pos = buf.position();
|
||||
buf.emit_raw_bytes(&self.positions);
|
||||
Lazy::from_position_and_meta(
|
||||
|
|
@ -115,7 +115,15 @@ impl<T> Table<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Lazy<[Table<T>]> {
|
||||
impl<T: LazyMeta<Meta = ()>> LazyMeta for Table<T> {
|
||||
type Meta = usize;
|
||||
|
||||
fn min_size(len: usize) -> usize {
|
||||
len * 4
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Encodable> Lazy<Table<T>> {
|
||||
/// Given the metadata, extract out the offset of a particular
|
||||
/// DefIndex (if any).
|
||||
#[inline(never)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue