Skip to content

Commit

Permalink
Auto merge of rust-lang#92497 - bjorn3:remove_lazy_meta_min_size, r=e…
Browse files Browse the repository at this point in the history
…ddyb

Remove LazyMeta::min_size

It is extremely conservative and as such barely reduces the size of encoded Lazy distances, but does increase complexity.
  • Loading branch information
bors committed Jan 9, 2022
2 parents 23ce5fc + 717d4b3 commit 02fe61b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 32 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,17 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
&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_with_meta: outside of a metadata node"),
LazyState::NodeStart(start) => {
let start = start.get();
assert!(distance + min_size <= start);
start - distance - min_size
assert!(distance <= start);
start - distance
}
LazyState::Previous(last_min_end) => last_min_end.get() + distance,
LazyState::Previous(last_pos) => last_pos.get() + distance,
};
self.lazy_state = LazyState::Previous(NonZeroUsize::new(position + min_size).unwrap());
self.lazy_state = LazyState::Previous(NonZeroUsize::new(position).unwrap());
Ok(Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta))
}

Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,24 +404,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
&mut self,
lazy: Lazy<T>,
) -> Result<(), <Self as Encoder>::Error> {
let min_end = lazy.position.get() + T::min_size(lazy.meta);
let pos = lazy.position.get();
let distance = match self.lazy_state {
LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
LazyState::NodeStart(start) => {
let start = start.get();
assert!(min_end <= start);
start - min_end
assert!(pos <= start);
start - pos
}
LazyState::Previous(last_min_end) => {
LazyState::Previous(last_pos) => {
assert!(
last_min_end <= lazy.position,
last_pos <= lazy.position,
"make sure that the calls to `lazy*` \
are in the same order as the metadata fields",
);
lazy.position.get() - last_min_end.get()
lazy.position.get() - last_pos.get()
}
};
self.lazy_state = LazyState::Previous(NonZeroUsize::new(min_end).unwrap());
self.lazy_state = LazyState::Previous(NonZeroUsize::new(pos).unwrap());
self.emit_usize(distance)
}

Expand All @@ -436,7 +436,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let meta = value.encode_contents_for_lazy(self);
self.lazy_state = LazyState::NoNode;

assert!(pos.get() + <T>::min_size(meta) <= self.position());
assert!(pos.get() <= self.position());

Lazy::from_position_and_meta(pos, meta)
}
Expand Down
16 changes: 1 addition & 15 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,14 @@ pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_V
/// e.g. for `Lazy<[T]>`, this is the length (count of `T` values).
trait LazyMeta {
type Meta: Copy + 'static;

/// Returns the minimum encoded size.
// FIXME(eddyb) Give better estimates for certain types.
fn min_size(meta: Self::Meta) -> usize;
}

impl<T> LazyMeta for T {
type Meta = ();

fn min_size(_: ()) -> usize {
assert_ne!(std::mem::size_of::<T>(), 0);
1
}
}

impl<T> LazyMeta for [T] {
type Meta = usize;

fn min_size(len: usize) -> usize {
len * T::min_size(())
}
}

/// A value of type T referred to by its absolute position
Expand Down Expand Up @@ -161,8 +148,7 @@ enum LazyState {
NodeStart(NonZeroUsize),

/// Inside a metadata node, with a previous `Lazy`.
/// The position is a conservative estimate of where that
/// previous `Lazy` would end (see their comments).
/// The position is where that previous `Lazy` would start.
Previous(NonZeroUsize),
}

Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ where
Option<T>: FixedSizeEncoding,
{
type Meta = usize;

fn min_size(len: usize) -> usize {
len
}
}

impl<I: Idx, T> Lazy<Table<I, T>>
Expand Down

0 comments on commit 02fe61b

Please sign in to comment.