Skip to content

Commit f09d474

Browse files
committed
Use PackedFingerprint in DepNode to reduce memory consumption
1 parent 8d2d001 commit f09d474

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

+42
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,50 @@ impl<D: rustc_serialize::Decoder> FingerprintDecoder for D {
151151
panic!("Cannot decode `Fingerprint` with `{}`", std::any::type_name::<D>());
152152
}
153153
}
154+
154155
impl FingerprintDecoder for opaque::Decoder<'_> {
155156
fn decode_fingerprint(&mut self) -> Result<Fingerprint, String> {
156157
Fingerprint::decode_opaque(self)
157158
}
158159
}
160+
161+
// `PackedFingerprint` wraps a `Fingerprint`. Its purpose is to, on certain
162+
// architectures, behave like a `Fingerprint` without alignment requirements.
163+
// This behavior is only enabled on x86 and x86_64, where the impact of
164+
// unaligned accesses is tolerable in small doses.
165+
//
166+
// This may be preferable to use in large collections of structs containing
167+
// fingerprints, as it can reduce memory consumption by preventing the padding
168+
// that the more strictly-aligned `Fingerprint` can introduce. An application of
169+
// this is in the query dependency graph, which contains a large collection of
170+
// `DepNode`s. As of this writing, the size of a `DepNode` decreases by ~30%
171+
// (from 24 bytes to 17) by using the packed representation here, which
172+
// noticeably decreases total memory usage when compiling large crates.
173+
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), repr(packed))]
174+
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
175+
pub struct PackedFingerprint(pub Fingerprint);
176+
177+
impl std::fmt::Display for PackedFingerprint {
178+
#[inline]
179+
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180+
// Copy to avoid taking reference to packed field.
181+
let copy = self.0;
182+
copy.fmt(formatter)
183+
}
184+
}
185+
186+
impl<E: rustc_serialize::Encoder> Encodable<E> for PackedFingerprint {
187+
#[inline]
188+
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
189+
// Copy to avoid taking reference to packed field.
190+
let copy = self.0;
191+
copy.encode(s)
192+
}
193+
}
194+
195+
impl<D: rustc_serialize::Decoder> Decodable<D> for PackedFingerprint {
196+
#[inline]
197+
fn decode(d: &mut D) -> Result<Self, D::Error> {
198+
Fingerprint::decode(d).map(|f| PackedFingerprint(f))
199+
}
200+
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::traits::query::{
6161
use crate::ty::subst::{GenericArg, SubstsRef};
6262
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
6363

64-
use rustc_data_structures::fingerprint::Fingerprint;
64+
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
6565
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6666
use rustc_hir::definitions::DefPathHash;
6767
use rustc_hir::HirId;
@@ -193,6 +193,15 @@ macro_rules! define_dep_nodes {
193193

194194
pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
195195

196+
// We keep a lot of `DepNode`s in memory during compilation. It's not
197+
// required that their size stay the same, but we don't want to change
198+
// it inadvertently. This assert just ensures we're aware of any change.
199+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
200+
static_assert_size!(DepNode, 17);
201+
202+
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
203+
static_assert_size!(DepNode, 24);
204+
196205
pub trait DepNodeExt: Sized {
197206
/// Construct a DepNode from the given DepKind and DefPathHash. This
198207
/// method will assert that the given DepKind actually requires a
@@ -227,7 +236,7 @@ macro_rules! define_dep_nodes {
227236
debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
228237
DepNode {
229238
kind,
230-
hash: def_path_hash.0,
239+
hash: PackedFingerprint(def_path_hash.0),
231240
}
232241
}
233242

@@ -243,7 +252,7 @@ macro_rules! define_dep_nodes {
243252
/// has been removed.
244253
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
245254
if self.kind.can_reconstruct_query_key() {
246-
let def_path_hash = DefPathHash(self.hash);
255+
let def_path_hash = DefPathHash(self.hash.0);
247256
tcx.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
248257
} else {
249258
None

compiler/rustc_query_system/src/dep_graph/dep_node.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
4545
use super::{DepContext, DepKind};
4646

47-
use rustc_data_structures::fingerprint::Fingerprint;
47+
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
4848
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4949

5050
use std::fmt;
@@ -53,7 +53,7 @@ use std::hash::Hash;
5353
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
5454
pub struct DepNode<K> {
5555
pub kind: K,
56-
pub hash: Fingerprint,
56+
pub hash: PackedFingerprint,
5757
}
5858

5959
impl<K: DepKind> DepNode<K> {
@@ -62,7 +62,7 @@ impl<K: DepKind> DepNode<K> {
6262
/// does not require any parameters.
6363
pub fn new_no_params(kind: K) -> DepNode<K> {
6464
debug_assert!(!kind.has_params());
65-
DepNode { kind, hash: Fingerprint::ZERO }
65+
DepNode { kind, hash: PackedFingerprint(Fingerprint::ZERO) }
6666
}
6767

6868
pub fn construct<Ctxt, Key>(tcx: Ctxt, kind: K, arg: &Key) -> DepNode<K>
@@ -71,7 +71,7 @@ impl<K: DepKind> DepNode<K> {
7171
Key: DepNodeParams<Ctxt>,
7272
{
7373
let hash = arg.to_fingerprint(tcx);
74-
let dep_node = DepNode { kind, hash };
74+
let dep_node = DepNode { kind, hash: PackedFingerprint(hash) };
7575

7676
#[cfg(debug_assertions)]
7777
{

compiler/rustc_query_system/src/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::fingerprint::Fingerprint;
1+
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33
use rustc_data_structures::profiling::QueryInvocationId;
44
use rustc_data_structures::sharded::{self, Sharded};
@@ -976,7 +976,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
976976
// Fingerprint::combine() is faster than sending Fingerprint
977977
// through the StableHasher (at least as long as StableHasher
978978
// is so slow).
979-
hash: self.anon_id_seed.combine(hasher.finish()),
979+
hash: PackedFingerprint(self.anon_id_seed.combine(hasher.finish())),
980980
};
981981

982982
self.intern_node(target_dep_node, task_deps.reads, Fingerprint::ZERO)

0 commit comments

Comments
 (0)