Use PackedFingerprint in DepNode to reduce memory consumption
This commit is contained in:
parent
8d2d001492
commit
f09d474836
4 changed files with 60 additions and 9 deletions
|
@ -151,8 +151,50 @@ impl<D: rustc_serialize::Decoder> FingerprintDecoder for D {
|
||||||
panic!("Cannot decode `Fingerprint` with `{}`", std::any::type_name::<D>());
|
panic!("Cannot decode `Fingerprint` with `{}`", std::any::type_name::<D>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FingerprintDecoder for opaque::Decoder<'_> {
|
impl FingerprintDecoder for opaque::Decoder<'_> {
|
||||||
fn decode_fingerprint(&mut self) -> Result<Fingerprint, String> {
|
fn decode_fingerprint(&mut self) -> Result<Fingerprint, String> {
|
||||||
Fingerprint::decode_opaque(self)
|
Fingerprint::decode_opaque(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `PackedFingerprint` wraps a `Fingerprint`. Its purpose is to, on certain
|
||||||
|
// architectures, behave like a `Fingerprint` without alignment requirements.
|
||||||
|
// This behavior is only enabled on x86 and x86_64, where the impact of
|
||||||
|
// unaligned accesses is tolerable in small doses.
|
||||||
|
//
|
||||||
|
// This may be preferable to use in large collections of structs containing
|
||||||
|
// fingerprints, as it can reduce memory consumption by preventing the padding
|
||||||
|
// that the more strictly-aligned `Fingerprint` can introduce. An application of
|
||||||
|
// this is in the query dependency graph, which contains a large collection of
|
||||||
|
// `DepNode`s. As of this writing, the size of a `DepNode` decreases by ~30%
|
||||||
|
// (from 24 bytes to 17) by using the packed representation here, which
|
||||||
|
// noticeably decreases total memory usage when compiling large crates.
|
||||||
|
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), repr(packed))]
|
||||||
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
|
||||||
|
pub struct PackedFingerprint(pub Fingerprint);
|
||||||
|
|
||||||
|
impl std::fmt::Display for PackedFingerprint {
|
||||||
|
#[inline]
|
||||||
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
// Copy to avoid taking reference to packed field.
|
||||||
|
let copy = self.0;
|
||||||
|
copy.fmt(formatter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: rustc_serialize::Encoder> Encodable<E> for PackedFingerprint {
|
||||||
|
#[inline]
|
||||||
|
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
|
||||||
|
// Copy to avoid taking reference to packed field.
|
||||||
|
let copy = self.0;
|
||||||
|
copy.encode(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<D: rustc_serialize::Decoder> Decodable<D> for PackedFingerprint {
|
||||||
|
#[inline]
|
||||||
|
fn decode(d: &mut D) -> Result<Self, D::Error> {
|
||||||
|
Fingerprint::decode(d).map(|f| PackedFingerprint(f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ use crate::traits::query::{
|
||||||
use crate::ty::subst::{GenericArg, SubstsRef};
|
use crate::ty::subst::{GenericArg, SubstsRef};
|
||||||
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
|
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
|
||||||
|
|
||||||
use rustc_data_structures::fingerprint::Fingerprint;
|
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
|
||||||
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
|
||||||
use rustc_hir::definitions::DefPathHash;
|
use rustc_hir::definitions::DefPathHash;
|
||||||
use rustc_hir::HirId;
|
use rustc_hir::HirId;
|
||||||
|
@ -193,6 +193,15 @@ macro_rules! define_dep_nodes {
|
||||||
|
|
||||||
pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
|
pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
|
||||||
|
|
||||||
|
// We keep a lot of `DepNode`s in memory during compilation. It's not
|
||||||
|
// required that their size stay the same, but we don't want to change
|
||||||
|
// it inadvertently. This assert just ensures we're aware of any change.
|
||||||
|
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||||
|
static_assert_size!(DepNode, 17);
|
||||||
|
|
||||||
|
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
|
||||||
|
static_assert_size!(DepNode, 24);
|
||||||
|
|
||||||
pub trait DepNodeExt: Sized {
|
pub trait DepNodeExt: Sized {
|
||||||
/// Construct a DepNode from the given DepKind and DefPathHash. This
|
/// Construct a DepNode from the given DepKind and DefPathHash. This
|
||||||
/// method will assert that the given DepKind actually requires a
|
/// method will assert that the given DepKind actually requires a
|
||||||
|
@ -227,7 +236,7 @@ macro_rules! define_dep_nodes {
|
||||||
debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
|
debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
|
||||||
DepNode {
|
DepNode {
|
||||||
kind,
|
kind,
|
||||||
hash: def_path_hash.0,
|
hash: PackedFingerprint(def_path_hash.0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,7 +252,7 @@ macro_rules! define_dep_nodes {
|
||||||
/// has been removed.
|
/// has been removed.
|
||||||
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
|
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
|
||||||
if self.kind.can_reconstruct_query_key() {
|
if self.kind.can_reconstruct_query_key() {
|
||||||
let def_path_hash = DefPathHash(self.hash);
|
let def_path_hash = DefPathHash(self.hash.0);
|
||||||
tcx.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
|
tcx.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
|
@ -44,7 +44,7 @@
|
||||||
|
|
||||||
use super::{DepContext, DepKind};
|
use super::{DepContext, DepKind};
|
||||||
|
|
||||||
use rustc_data_structures::fingerprint::Fingerprint;
|
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
|
||||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -53,7 +53,7 @@ use std::hash::Hash;
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
|
||||||
pub struct DepNode<K> {
|
pub struct DepNode<K> {
|
||||||
pub kind: K,
|
pub kind: K,
|
||||||
pub hash: Fingerprint,
|
pub hash: PackedFingerprint,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K: DepKind> DepNode<K> {
|
impl<K: DepKind> DepNode<K> {
|
||||||
|
@ -62,7 +62,7 @@ impl<K: DepKind> DepNode<K> {
|
||||||
/// does not require any parameters.
|
/// does not require any parameters.
|
||||||
pub fn new_no_params(kind: K) -> DepNode<K> {
|
pub fn new_no_params(kind: K) -> DepNode<K> {
|
||||||
debug_assert!(!kind.has_params());
|
debug_assert!(!kind.has_params());
|
||||||
DepNode { kind, hash: Fingerprint::ZERO }
|
DepNode { kind, hash: PackedFingerprint(Fingerprint::ZERO) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn construct<Ctxt, Key>(tcx: Ctxt, kind: K, arg: &Key) -> DepNode<K>
|
pub fn construct<Ctxt, Key>(tcx: Ctxt, kind: K, arg: &Key) -> DepNode<K>
|
||||||
|
@ -71,7 +71,7 @@ impl<K: DepKind> DepNode<K> {
|
||||||
Key: DepNodeParams<Ctxt>,
|
Key: DepNodeParams<Ctxt>,
|
||||||
{
|
{
|
||||||
let hash = arg.to_fingerprint(tcx);
|
let hash = arg.to_fingerprint(tcx);
|
||||||
let dep_node = DepNode { kind, hash };
|
let dep_node = DepNode { kind, hash: PackedFingerprint(hash) };
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use rustc_data_structures::fingerprint::Fingerprint;
|
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||||
use rustc_data_structures::profiling::QueryInvocationId;
|
use rustc_data_structures::profiling::QueryInvocationId;
|
||||||
use rustc_data_structures::sharded::{self, Sharded};
|
use rustc_data_structures::sharded::{self, Sharded};
|
||||||
|
@ -976,7 +976,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
|
||||||
// Fingerprint::combine() is faster than sending Fingerprint
|
// Fingerprint::combine() is faster than sending Fingerprint
|
||||||
// through the StableHasher (at least as long as StableHasher
|
// through the StableHasher (at least as long as StableHasher
|
||||||
// is so slow).
|
// is so slow).
|
||||||
hash: self.anon_id_seed.combine(hasher.finish()),
|
hash: PackedFingerprint(self.anon_id_seed.combine(hasher.finish())),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.intern_node(target_dep_node, task_deps.reads, Fingerprint::ZERO)
|
self.intern_node(target_dep_node, task_deps.reads, Fingerprint::ZERO)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue