Skip to content

Remove LazyMeta::min_size #92497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -62,27 +62,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;
Comment on lines -67 to -68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I had hoped to be able to predict certain sizes well enough to have a lot of single-byte LEB128 encodings, but in retrospect it doesn't seem doable without a bunch of custom derives. In most cases, distances under 128 wouldn't make a difference anyway, and I suspect that's most of what min_size could ever handle (outside of Lazy<[T]>, I suppose).

}

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 @@ -160,8 +147,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