Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass the varint path when encoding InitMask #110343

Merged
merged 1 commit into from
Apr 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::hash;
use std::iter;
use std::ops::Range;

use rustc_serialize::{Decodable, Encodable};
use rustc_target::abi::Size;
use rustc_type_ir::{TyDecoder, TyEncoder};

use super::AllocRange;

Expand Down Expand Up @@ -182,11 +184,39 @@ impl InitMask {
/// The actual materialized blocks of the bitmask, when we can't keep the `InitMask` lazy.
// Note: for performance reasons when interning, some of the fields can be partially
// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
#[derive(Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, HashStable)]
#[derive(Clone, Debug, Eq, PartialEq, HashStable)]
struct InitMaskMaterialized {
blocks: Vec<Block>,
}

// `Block` is a `u64`, but it is a bitmask not a numeric value. If we were to just derive
// Encodable and Decodable we would apply varint encoding to the bitmasks, which is slower
// and also produces more output when the high bits of each `u64` are occupied.
// Note: There is probably a remaining optimization for masks that do not use an entire
// `Block`.
impl<E: TyEncoder> Encodable<E> for InitMaskMaterialized {
fn encode(&self, encoder: &mut E) {
encoder.emit_usize(self.blocks.len());
for block in &self.blocks {
encoder.emit_raw_bytes(&block.to_le_bytes());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to note: I'm not familiar with the endianness rules for encoding/decoding, I had idly wondered before if we should prefer native endianness in incr data, but I think we use LE everywhere so 👍 .

}
}
}

// This implementation is deliberately not derived, see the matching `Encodable` impl.
impl<D: TyDecoder> Decodable<D> for InitMaskMaterialized {
fn decode(decoder: &mut D) -> Self {
let num_blocks = decoder.read_usize();
let mut blocks = Vec::with_capacity(num_blocks);
for _ in 0..num_blocks {
let bytes = decoder.read_raw_bytes(8);
let block = u64::from_le_bytes(bytes.try_into().unwrap());
blocks.push(block);
}
InitMaskMaterialized { blocks }
}
}

// Const allocations are only hashed for interning. However, they can be large, making the hashing
// expensive especially since it uses `FxHash`: it's better suited to short keys, not potentially
// big buffers like the allocation's init mask. We can partially hash some fields when they're
Expand Down