refine comments, disambiguate len for array and tables
This commit is contained in:
parent
ca5e60b7fb
commit
14e5816f1b
4 changed files with 41 additions and 36 deletions
|
@ -269,7 +269,7 @@ impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> LazyValue<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DecodeIterator<'a, 'tcx, T> {
|
struct DecodeIterator<'a, 'tcx, T> {
|
||||||
range: std::ops::Range<usize>,
|
elem_counter: std::ops::Range<usize>,
|
||||||
dcx: DecodeContext<'a, 'tcx>,
|
dcx: DecodeContext<'a, 'tcx>,
|
||||||
_phantom: PhantomData<fn() -> T>,
|
_phantom: PhantomData<fn() -> T>,
|
||||||
}
|
}
|
||||||
|
@ -278,7 +278,7 @@ impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Iterator for DecodeIterato
|
||||||
type Item = T;
|
type Item = T;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
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>
|
for DecodeIterator<'a, 'tcx, T>
|
||||||
{
|
{
|
||||||
fn len(&self) -> usize {
|
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> {
|
fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> DecodeIterator<'a, 'tcx, T> {
|
||||||
let mut dcx = metadata.decoder(self.position.get());
|
let mut dcx = metadata.decoder(self.position.get());
|
||||||
dcx.lazy_state = LazyState::NodeStart(self.position);
|
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> {
|
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> {
|
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]
|
#[inline]
|
||||||
|
|
|
@ -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> {
|
impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> {
|
||||||
fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
|
fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
|
||||||
e.emit_usize(self.len)?;
|
e.emit_usize(self.num_elems)?;
|
||||||
if self.len == 0 {
|
if self.num_elems == 0 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
e.emit_lazy_distance(self.position)
|
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> {
|
impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> {
|
||||||
fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) -> opaque::EncodeResult {
|
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)
|
e.emit_lazy_distance(self.position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -421,7 +421,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
||||||
|
|
||||||
assert!(pos.get() <= self.position());
|
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) {
|
fn encode_info_for_items(&mut self) {
|
||||||
|
|
|
@ -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.
|
/// Distances start at 1, as 0-byte nodes are invalid.
|
||||||
/// Also invalid are nodes being referred in a different
|
/// Also invalid are nodes being referred in a different
|
||||||
/// order than they were encoded in.
|
/// 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]
|
#[must_use]
|
||||||
// FIXME(#59875) the `Meta` parameter only exists to dodge
|
|
||||||
// invariance wrt `T` (coming from the `meta: T::Meta` field).
|
|
||||||
struct LazyValue<T> {
|
struct LazyValue<T> {
|
||||||
position: NonZeroUsize,
|
position: NonZeroUsize,
|
||||||
_marker: PhantomData<fn() -> T>,
|
_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> {
|
struct LazyArray<T> {
|
||||||
position: NonZeroUsize,
|
position: NonZeroUsize,
|
||||||
len: usize,
|
num_elems: usize,
|
||||||
_marker: PhantomData<fn() -> T>,
|
_marker: PhantomData<fn() -> T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> LazyArray<T> {
|
impl<T> LazyArray<T> {
|
||||||
fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyArray<T> {
|
fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
|
||||||
LazyArray { position, len, _marker: PhantomData }
|
LazyArray { position, num_elems, _marker: PhantomData }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn empty() -> LazyArray<T> {
|
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
|
/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
|
||||||
/// `LazyArray<T>`, but without requiring encoding or decoding all the values
|
/// `LazyArray<T>`, but without requiring encoding or decoding all the values
|
||||||
/// eagerly and in-order.
|
/// eagerly and in-order.
|
||||||
struct LazyTable<I, T> {
|
struct LazyTable<I, T> {
|
||||||
position: NonZeroUsize,
|
position: NonZeroUsize,
|
||||||
len: usize,
|
encoded_size: usize,
|
||||||
_marker: PhantomData<fn(I) -> T>,
|
_marker: PhantomData<fn(I) -> T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, T> LazyTable<I, T> {
|
impl<I, T> LazyTable<I, T> {
|
||||||
fn from_position_and_len(position: NonZeroUsize, len: usize) -> LazyTable<I, T> {
|
fn from_position_and_encoded_size(
|
||||||
LazyTable { position, len, _marker: PhantomData }
|
position: NonZeroUsize,
|
||||||
|
encoded_size: usize,
|
||||||
|
) -> LazyTable<I, T> {
|
||||||
|
LazyTable { position, encoded_size, _marker: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 ([ref position_bytes, ref meta_bytes],[])= b.as_chunks::<4>() else { panic!() };
|
||||||
let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?;
|
let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?;
|
||||||
let len = u32::from_bytes(meta_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]
|
#[inline]
|
||||||
|
@ -239,7 +239,7 @@ impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
|
||||||
let position: u32 = position.try_into().unwrap();
|
let position: u32 = position.try_into().unwrap();
|
||||||
position.write_to_bytes(position_bytes);
|
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();
|
let len: u32 = len.try_into().unwrap();
|
||||||
len.write_to_bytes(meta_bytes);
|
len.write_to_bytes(meta_bytes);
|
||||||
}
|
}
|
||||||
|
@ -289,7 +289,10 @@ where
|
||||||
buf.emit_raw_bytes(block).unwrap();
|
buf.emit_raw_bytes(block).unwrap();
|
||||||
}
|
}
|
||||||
let num_bytes = self.blocks.len() * N;
|
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
|
where
|
||||||
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
|
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 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.as_chunks::<N>() else { panic!() };
|
||||||
let bytes = bytes.get(i.index())?;
|
let bytes = bytes.get(i.index())?;
|
||||||
FixedSizeEncoding::from_bytes(bytes)
|
FixedSizeEncoding::from_bytes(bytes)
|
||||||
|
@ -321,6 +324,6 @@ where
|
||||||
where
|
where
|
||||||
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
|
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
|
||||||
{
|
{
|
||||||
self.len / N
|
self.encoded_size / N
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue