1
Fork 0

Auto merge of #92497 - bjorn3:remove_lazy_meta_min_size, r=eddyb

Remove LazyMeta::min_size

It is extremely conservative and as such barely reduces the size of encoded Lazy distances, but does increase complexity.
This commit is contained in:
bors 2022-01-09 01:29:22 +00:00
commit 02fe61b381
4 changed files with 13 additions and 32 deletions

View file

@ -304,18 +304,17 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
&mut self, &mut self,
meta: T::Meta, meta: T::Meta,
) -> Result<Lazy<T>, <Self as Decoder>::Error> { ) -> Result<Lazy<T>, <Self as Decoder>::Error> {
let min_size = T::min_size(meta);
let distance = self.read_usize()?; let distance = self.read_usize()?;
let position = match self.lazy_state { let position = match self.lazy_state {
LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"), LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
LazyState::NodeStart(start) => { LazyState::NodeStart(start) => {
let start = start.get(); let start = start.get();
assert!(distance + min_size <= start); assert!(distance <= start);
start - distance - min_size 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)) Ok(Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta))
} }

View file

@ -404,24 +404,24 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
&mut self, &mut self,
lazy: Lazy<T>, lazy: Lazy<T>,
) -> Result<(), <Self as Encoder>::Error> { ) -> 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 { let distance = match self.lazy_state {
LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"), LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
LazyState::NodeStart(start) => { LazyState::NodeStart(start) => {
let start = start.get(); let start = start.get();
assert!(min_end <= start); assert!(pos <= start);
start - min_end start - pos
} }
LazyState::Previous(last_min_end) => { LazyState::Previous(last_pos) => {
assert!( assert!(
last_min_end <= lazy.position, last_pos <= lazy.position,
"make sure that the calls to `lazy*` \ "make sure that the calls to `lazy*` \
are in the same order as the metadata fields", 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) self.emit_usize(distance)
} }
@ -436,7 +436,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let meta = value.encode_contents_for_lazy(self); let meta = value.encode_contents_for_lazy(self);
self.lazy_state = LazyState::NoNode; 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) Lazy::from_position_and_meta(pos, meta)
} }

View file

@ -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). /// e.g. for `Lazy<[T]>`, this is the length (count of `T` values).
trait LazyMeta { trait LazyMeta {
type Meta: Copy + 'static; 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 { impl<T> LazyMeta for T {
type Meta = (); type Meta = ();
fn min_size(_: ()) -> usize {
assert_ne!(std::mem::size_of::<T>(), 0);
1
}
} }
impl<T> LazyMeta for [T] { impl<T> LazyMeta for [T] {
type Meta = usize; type Meta = usize;
fn min_size(len: usize) -> usize {
len * T::min_size(())
}
} }
/// A value of type T referred to by its absolute position /// A value of type T referred to by its absolute position
@ -161,8 +148,7 @@ enum LazyState {
NodeStart(NonZeroUsize), NodeStart(NonZeroUsize),
/// Inside a metadata node, with a previous `Lazy`. /// Inside a metadata node, with a previous `Lazy`.
/// The position is a conservative estimate of where that /// The position is where that previous `Lazy` would start.
/// previous `Lazy` would end (see their comments).
Previous(NonZeroUsize), Previous(NonZeroUsize),
} }

View file

@ -183,10 +183,6 @@ where
Option<T>: FixedSizeEncoding, Option<T>: FixedSizeEncoding,
{ {
type Meta = usize; type Meta = usize;
fn min_size(len: usize) -> usize {
len
}
} }
impl<I: Idx, T> Lazy<Table<I, T>> impl<I: Idx, T> Lazy<Table<I, T>>