diff options
| author | Michael Goulet <michael@errs.io> | 2022-05-22 16:46:23 -0700 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2022-05-23 19:39:10 -0700 |
| commit | 14e5816f1bf051d922e8c46fa6e2414664cc3a22 (patch) | |
| tree | ea160bdd9ebebeba3450ebaa070af52363355912 /compiler | |
| parent | ca5e60b7fb8867a59529e3120c305574a1e3e991 (diff) | |
| download | rust-14e5816f1bf051d922e8c46fa6e2414664cc3a22.tar.gz rust-14e5816f1bf051d922e8c46fa6e2414664cc3a22.zip | |
refine comments, disambiguate len for array and tables
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/decoder.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/mod.rs | 42 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/table.rs | 15 |
4 files changed, 41 insertions, 36 deletions
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<DecodeContext<'a, 'tcx>>> LazyValue<T> { } struct DecodeIterator<'a, 'tcx, T> { - range: std::ops::Range<usize>, + elem_counter: std::ops::Range<usize>, dcx: DecodeContext<'a, 'tcx>, _phantom: PhantomData<fn() -> T>, } @@ -278,7 +278,7 @@ impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Iterator for DecodeIterato type Item = T; fn next(&mut self) -> Option<Self::Item> { - 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<DecodeContext<'a, 'tcx>>> 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<DecodeContext<'a, 'tcx>>> LazyArray<T> { fn decode<M: Metadata<'a, 'tcx>>(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<T>(&mut self, len: usize) -> LazyArray<T> { - 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<I, T>(&mut self, len: usize) -> LazyTable<I, T> { - 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<EncodeContext<'a, 'tcx>> for LazyValue<T> { impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> { 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<EncodeContext<'a, 'tcx>> for LazyArray<T> { impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> { 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<T>`) -/// -/// Unlike `Lazy<Vec<T>>`, 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<T> { position: NonZeroUsize, _marker: PhantomData<fn() -> T>, @@ -102,34 +89,49 @@ impl<T> LazyValue<T> { } } +/// A list of lazily-decoded values. +/// +/// Unlike `Lazy<Vec<T>>`, 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<T> { position: NonZeroUsize, - len: usize, + num_elems: usize, _marker: PhantomData<fn() -> T>, } impl<T> LazyArray<T> { - fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyArray<T> { - LazyArray { position, len, _marker: PhantomData } + fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> { + LazyArray { position, num_elems, _marker: PhantomData } } fn empty() -> LazyArray<T> { - 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<T>`, but without requiring encoding or decoding all the values /// eagerly and in-order. struct LazyTable<I, T> { position: NonZeroUsize, - len: usize, + encoded_size: usize, _marker: PhantomData<fn(I) -> T>, } impl<I, T> LazyTable<I, T> { - fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyTable<I, T> { - LazyTable { position, len, _marker: PhantomData } + fn from_position_and_encoded_size( + position: NonZeroUsize, + encoded_size: usize, + ) -> LazyTable<I, T> { + 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<T> FixedSizeEncoding for Option<LazyArray<T>> { 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<T> FixedSizeEncoding for Option<LazyArray<T>> { 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<T>: FixedSizeEncoding<ByteArray = [u8; N]>, { - 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::<N>() else { panic!() }; let bytes = bytes.get(i.index())?; FixedSizeEncoding::from_bytes(bytes) @@ -321,6 +324,6 @@ where where Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>, { - self.len / N + self.encoded_size / N } } |
