diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index cf60cf501d0..6042d0e49c3 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -269,7 +269,7 @@ impl<'a, 'tcx, T: Decodable>> LazyValue { } struct DecodeIterator<'a, 'tcx, T> { - range: std::ops::Range, + elem_counter: std::ops::Range, dcx: DecodeContext<'a, 'tcx>, _phantom: PhantomData T>, } @@ -278,7 +278,7 @@ impl<'a, 'tcx, T: Decodable>> Iterator for DecodeIterato type Item = T; fn next(&mut self) -> Option { - self.range.next().map(|_| T::decode(&mut self.dcx)) + self.elem_counter.next().map(|_| T::decode(&mut self.dcx)) } } @@ -286,7 +286,7 @@ impl<'a, 'tcx, T: Decodable>> ExactSizeIterator for DecodeIterator<'a, 'tcx, T> { fn len(&self) -> usize { - self.range.len() + self.elem_counter.len() } } @@ -294,7 +294,7 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable>> LazyArray { fn decode>(self, metadata: M) -> DecodeIterator<'a, 'tcx, T> { let mut dcx = metadata.decoder(self.position.get()); dcx.lazy_state = LazyState::NodeStart(self.position); - DecodeIterator { range: (0..self.len), dcx, _phantom: PhantomData } + DecodeIterator { elem_counter: (0..self.num_elems), dcx, _phantom: PhantomData } } } @@ -342,11 +342,11 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { } fn read_lazy_array(&mut self, len: usize) -> LazyArray { - self.read_lazy_offset_then(|pos| LazyArray::from_position_and_len(pos, len)) + self.read_lazy_offset_then(|pos| LazyArray::from_position_and_num_elems(pos, len)) } fn read_lazy_table(&mut self, len: usize) -> LazyTable { - self.read_lazy_offset_then(|pos| LazyTable::from_position_and_len(pos, len)) + self.read_lazy_offset_then(|pos| LazyTable::from_position_and_encoded_size(pos, len)) } #[inline] diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 598e011820e..28f289f06ec 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -132,8 +132,8 @@ impl<'a, 'tcx, T> Encodable> for LazyValue { impl<'a, 'tcx, T> Encodable> for LazyArray { fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - e.emit_usize(self.len)?; - if self.len == 0 { + e.emit_usize(self.num_elems)?; + if self.num_elems == 0 { return Ok(()); } e.emit_lazy_distance(self.position) @@ -142,7 +142,7 @@ impl<'a, 'tcx, T> Encodable> for LazyArray { impl<'a, 'tcx, I, T> Encodable> for LazyTable { fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult { - e.emit_usize(self.len)?; + e.emit_usize(self.encoded_size)?; e.emit_lazy_distance(self.position) } } @@ -421,7 +421,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { assert!(pos.get() <= self.position()); - LazyArray::from_position_and_len(pos, len) + LazyArray::from_position_and_num_elems(pos, len) } fn encode_info_for_items(&mut self) { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 8e3985e59ac..00c4dc3955d 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -77,20 +77,7 @@ pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_V /// Distances start at 1, as 0-byte nodes are invalid. /// Also invalid are nodes being referred in a different /// order than they were encoded in. -/// -/// # Sequences (`LazyArray`) -/// -/// Unlike `Lazy>`, the length is encoded next to the -/// position, not at the position, which means that the length -/// doesn't need to be known before encoding all the elements. -/// -/// If the length is 0, no position is encoded, but otherwise, -/// the encoding is that of `Lazy`, with the distinction that -/// the minimal distance the length of the sequence, i.e. -/// it's assumed there's no 0-byte element in the sequence. #[must_use] -// FIXME(#59875) the `Meta` parameter only exists to dodge -// invariance wrt `T` (coming from the `meta: T::Meta` field). struct LazyValue { position: NonZeroUsize, _marker: PhantomData T>, @@ -102,34 +89,49 @@ impl LazyValue { } } +/// A list of lazily-decoded values. +/// +/// Unlike `Lazy>`, the length is encoded next to the +/// position, not at the position, which means that the length +/// doesn't need to be known before encoding all the elements. +/// +/// If the length is 0, no position is encoded, but otherwise, +/// the encoding is that of `Lazy`, with the distinction that +/// the minimal distance the length of the sequence, i.e. +/// it's assumed there's no 0-byte element in the sequence. struct LazyArray { position: NonZeroUsize, - len: usize, + num_elems: usize, _marker: PhantomData T>, } impl LazyArray { - fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyArray { - LazyArray { position, len, _marker: PhantomData } + fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray { + LazyArray { position, num_elems, _marker: PhantomData } } fn empty() -> LazyArray { - LazyArray::from_position_and_len(NonZeroUsize::new(1).unwrap(), 0) + LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0) } } +/// A list of lazily-decoded values, with the added capability of random access. +/// /// Random-access table (i.e. offering constant-time `get`/`set`), similar to /// `LazyArray`, but without requiring encoding or decoding all the values /// eagerly and in-order. struct LazyTable { position: NonZeroUsize, - len: usize, + encoded_size: usize, _marker: PhantomData T>, } impl LazyTable { - fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyTable { - LazyTable { position, len, _marker: PhantomData } + fn from_position_and_encoded_size( + position: NonZeroUsize, + encoded_size: usize, + ) -> LazyTable { + LazyTable { position, encoded_size, _marker: PhantomData } } } diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 83d4c48e4ea..60c5143a6c3 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -228,7 +228,7 @@ impl FixedSizeEncoding for Option> { let ([ref position_bytes, ref meta_bytes],[])= b.as_chunks::<4>() else { panic!() }; let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?; let len = u32::from_bytes(meta_bytes) as usize; - Some(LazyArray::from_position_and_len(position, len)) + Some(LazyArray::from_position_and_num_elems(position, len)) } #[inline] @@ -239,7 +239,7 @@ impl FixedSizeEncoding for Option> { let position: u32 = position.try_into().unwrap(); position.write_to_bytes(position_bytes); - let len = self.map_or(0, |lazy| lazy.len); + let len = self.map_or(0, |lazy| lazy.num_elems); let len: u32 = len.try_into().unwrap(); len.write_to_bytes(meta_bytes); } @@ -289,7 +289,10 @@ where buf.emit_raw_bytes(block).unwrap(); } let num_bytes = self.blocks.len() * N; - LazyTable::from_position_and_len(NonZeroUsize::new(pos as usize).unwrap(), num_bytes) + LazyTable::from_position_and_encoded_size( + NonZeroUsize::new(pos as usize).unwrap(), + num_bytes, + ) } } @@ -307,10 +310,10 @@ where where Option: FixedSizeEncoding, { - debug!("LazyTable::lookup: index={:?} len={:?}", i, self.len); + debug!("LazyTable::lookup: index={:?} len={:?}", i, self.encoded_size); let start = self.position.get(); - let bytes = &metadata.blob()[start..start + self.len]; + let bytes = &metadata.blob()[start..start + self.encoded_size]; let (bytes, []) = bytes.as_chunks::() else { panic!() }; let bytes = bytes.get(i.index())?; FixedSizeEncoding::from_bytes(bytes) @@ -321,6 +324,6 @@ where where Option: FixedSizeEncoding, { - self.len / N + self.encoded_size / N } }