Skip to content

Commit 14e5816

Browse files
refine comments, disambiguate len for array and tables
1 parent ca5e60b commit 14e5816

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> LazyValue<T> {
269269
}
270270

271271
struct DecodeIterator<'a, 'tcx, T> {
272-
range: std::ops::Range<usize>,
272+
elem_counter: std::ops::Range<usize>,
273273
dcx: DecodeContext<'a, 'tcx>,
274274
_phantom: PhantomData<fn() -> T>,
275275
}
@@ -278,23 +278,23 @@ impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Iterator for DecodeIterato
278278
type Item = T;
279279

280280
fn next(&mut self) -> Option<Self::Item> {
281-
self.range.next().map(|_| T::decode(&mut self.dcx))
281+
self.elem_counter.next().map(|_| T::decode(&mut self.dcx))
282282
}
283283
}
284284

285285
impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> ExactSizeIterator
286286
for DecodeIterator<'a, 'tcx, T>
287287
{
288288
fn len(&self) -> usize {
289-
self.range.len()
289+
self.elem_counter.len()
290290
}
291291
}
292292

293293
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable<DecodeContext<'a, 'tcx>>> LazyArray<T> {
294294
fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> DecodeIterator<'a, 'tcx, T> {
295295
let mut dcx = metadata.decoder(self.position.get());
296296
dcx.lazy_state = LazyState::NodeStart(self.position);
297-
DecodeIterator { range: (0..self.len), dcx, _phantom: PhantomData }
297+
DecodeIterator { elem_counter: (0..self.num_elems), dcx, _phantom: PhantomData }
298298
}
299299
}
300300

@@ -342,11 +342,11 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
342342
}
343343

344344
fn read_lazy_array<T>(&mut self, len: usize) -> LazyArray<T> {
345-
self.read_lazy_offset_then(|pos| LazyArray::from_position_and_len(pos, len))
345+
self.read_lazy_offset_then(|pos| LazyArray::from_position_and_num_elems(pos, len))
346346
}
347347

348348
fn read_lazy_table<I, T>(&mut self, len: usize) -> LazyTable<I, T> {
349-
self.read_lazy_offset_then(|pos| LazyTable::from_position_and_len(pos, len))
349+
self.read_lazy_offset_then(|pos| LazyTable::from_position_and_encoded_size(pos, len))
350350
}
351351

352352
#[inline]

compiler/rustc_metadata/src/rmeta/encoder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyValue<T> {
132132

133133
impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> {
134134
fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
135-
e.emit_usize(self.len)?;
136-
if self.len == 0 {
135+
e.emit_usize(self.num_elems)?;
136+
if self.num_elems == 0 {
137137
return Ok(());
138138
}
139139
e.emit_lazy_distance(self.position)
@@ -142,7 +142,7 @@ impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> {
142142

143143
impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> {
144144
fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
145-
e.emit_usize(self.len)?;
145+
e.emit_usize(self.encoded_size)?;
146146
e.emit_lazy_distance(self.position)
147147
}
148148
}
@@ -421,7 +421,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
421421

422422
assert!(pos.get() <= self.position());
423423

424-
LazyArray::from_position_and_len(pos, len)
424+
LazyArray::from_position_and_num_elems(pos, len)
425425
}
426426

427427
fn encode_info_for_items(&mut self) {

compiler/rustc_metadata/src/rmeta/mod.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,7 @@ pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_V
7777
/// Distances start at 1, as 0-byte nodes are invalid.
7878
/// Also invalid are nodes being referred in a different
7979
/// order than they were encoded in.
80-
///
81-
/// # Sequences (`LazyArray<T>`)
82-
///
83-
/// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
84-
/// position, not at the position, which means that the length
85-
/// doesn't need to be known before encoding all the elements.
86-
///
87-
/// If the length is 0, no position is encoded, but otherwise,
88-
/// the encoding is that of `Lazy`, with the distinction that
89-
/// the minimal distance the length of the sequence, i.e.
90-
/// it's assumed there's no 0-byte element in the sequence.
9180
#[must_use]
92-
// FIXME(#59875) the `Meta` parameter only exists to dodge
93-
// invariance wrt `T` (coming from the `meta: T::Meta` field).
9481
struct LazyValue<T> {
9582
position: NonZeroUsize,
9683
_marker: PhantomData<fn() -> T>,
@@ -102,34 +89,49 @@ impl<T> LazyValue<T> {
10289
}
10390
}
10491

92+
/// A list of lazily-decoded values.
93+
///
94+
/// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
95+
/// position, not at the position, which means that the length
96+
/// doesn't need to be known before encoding all the elements.
97+
///
98+
/// If the length is 0, no position is encoded, but otherwise,
99+
/// the encoding is that of `Lazy`, with the distinction that
100+
/// the minimal distance the length of the sequence, i.e.
101+
/// it's assumed there's no 0-byte element in the sequence.
105102
struct LazyArray<T> {
106103
position: NonZeroUsize,
107-
len: usize,
104+
num_elems: usize,
108105
_marker: PhantomData<fn() -> T>,
109106
}
110107

111108
impl<T> LazyArray<T> {
112-
fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyArray<T> {
113-
LazyArray { position, len, _marker: PhantomData }
109+
fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
110+
LazyArray { position, num_elems, _marker: PhantomData }
114111
}
115112

116113
fn empty() -> LazyArray<T> {
117-
LazyArray::from_position_and_len(NonZeroUsize::new(1).unwrap(), 0)
114+
LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
118115
}
119116
}
120117

118+
/// A list of lazily-decoded values, with the added capability of random access.
119+
///
121120
/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
122121
/// `LazyArray<T>`, but without requiring encoding or decoding all the values
123122
/// eagerly and in-order.
124123
struct LazyTable<I, T> {
125124
position: NonZeroUsize,
126-
len: usize,
125+
encoded_size: usize,
127126
_marker: PhantomData<fn(I) -> T>,
128127
}
129128

130129
impl<I, T> LazyTable<I, T> {
131-
fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyTable<I, T> {
132-
LazyTable { position, len, _marker: PhantomData }
130+
fn from_position_and_encoded_size(
131+
position: NonZeroUsize,
132+
encoded_size: usize,
133+
) -> LazyTable<I, T> {
134+
LazyTable { position, encoded_size, _marker: PhantomData }
133135
}
134136
}
135137

compiler/rustc_metadata/src/rmeta/table.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
228228
let ([ref position_bytes, ref meta_bytes],[])= b.as_chunks::<4>() else { panic!() };
229229
let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?;
230230
let len = u32::from_bytes(meta_bytes) as usize;
231-
Some(LazyArray::from_position_and_len(position, len))
231+
Some(LazyArray::from_position_and_num_elems(position, len))
232232
}
233233

234234
#[inline]
@@ -239,7 +239,7 @@ impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
239239
let position: u32 = position.try_into().unwrap();
240240
position.write_to_bytes(position_bytes);
241241

242-
let len = self.map_or(0, |lazy| lazy.len);
242+
let len = self.map_or(0, |lazy| lazy.num_elems);
243243
let len: u32 = len.try_into().unwrap();
244244
len.write_to_bytes(meta_bytes);
245245
}
@@ -289,7 +289,10 @@ where
289289
buf.emit_raw_bytes(block).unwrap();
290290
}
291291
let num_bytes = self.blocks.len() * N;
292-
LazyTable::from_position_and_len(NonZeroUsize::new(pos as usize).unwrap(), num_bytes)
292+
LazyTable::from_position_and_encoded_size(
293+
NonZeroUsize::new(pos as usize).unwrap(),
294+
num_bytes,
295+
)
293296
}
294297
}
295298

@@ -307,10 +310,10 @@ where
307310
where
308311
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
309312
{
310-
debug!("LazyTable::lookup: index={:?} len={:?}", i, self.len);
313+
debug!("LazyTable::lookup: index={:?} len={:?}", i, self.encoded_size);
311314

312315
let start = self.position.get();
313-
let bytes = &metadata.blob()[start..start + self.len];
316+
let bytes = &metadata.blob()[start..start + self.encoded_size];
314317
let (bytes, []) = bytes.as_chunks::<N>() else { panic!() };
315318
let bytes = bytes.get(i.index())?;
316319
FixedSizeEncoding::from_bytes(bytes)
@@ -321,6 +324,6 @@ where
321324
where
322325
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
323326
{
324-
self.len / N
327+
self.encoded_size / N
325328
}
326329
}

0 commit comments

Comments
 (0)