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:
bjorn3 2022-01-02 18:20:37 +01:00
parent 03360be6b7
commit 717d4b35f8
4 changed files with 13 additions and 32 deletions

View file

@ -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))
}

View file

@ -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)
}
@ -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)
}

View file

@ -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;
}
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
@ -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),
}

View file

@ -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>>