Skip to content

Commit

Permalink
use a peekable iterator to check the first chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdesjardins committed Aug 25, 2021
1 parent c07a2eb commit adf3b01
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions compiler/rustc_middle/src/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,14 @@ pub enum InitChunk {
}

impl InitChunk {
#[inline]
pub fn is_init(&self) -> bool {
match self {
Self::Init(_) => true,
Self::Uninit(_) => false,
}
}

#[inline]
pub fn range(&self) -> Range<Size> {
match self {
Expand Down Expand Up @@ -1035,7 +1043,7 @@ impl InitMaskCompressed {

/// Transferring the initialization mask to other allocations.
impl<Tag, Extra> Allocation<Tag, Extra> {
/// Creates a run-length encoding of the initialization mask.
/// Creates a run-length encoding of the initialization mask; panics if range is empty.
///
/// This is essentially a more space-efficient version of
/// `InitMask::range_as_init_chunks(...).collect::<Vec<_>>()`.
Expand All @@ -1053,10 +1061,13 @@ impl<Tag, Extra> Allocation<Tag, Extra> {
// where each element toggles the state.

let mut ranges = smallvec::SmallVec::<[u64; 1]>::new();
let initial = self.init_mask.get(range.start);

let mut chunks = self.init_mask.range_as_init_chunks(range.start, range.end()).peekable();

let initial = chunks.peek().expect("range should be nonempty").is_init();

// Here we rely on `range_as_init_chunks` to yield alternating init/uninit chunks.
for chunk in self.init_mask.range_as_init_chunks(range.start, range.end()) {
for chunk in chunks {
let len = chunk.range().end.bytes() - chunk.range().start.bytes();
ranges.push(len);
}
Expand Down

0 comments on commit adf3b01

Please sign in to comment.