Skip to content

Commit ae6aa22

Browse files
committed
Auto merge of #78646 - tgnottingham:packed_fingerprints, r=nnethercote
Use PackedFingerprint in DepNode to reduce memory consumption
2 parents 172acf8 + 142932a commit ae6aa22

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

+59
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,67 @@ 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+
//
174+
// The wrapped `Fingerprint` is private to reduce the chance of a client
175+
// invoking undefined behavior by taking a reference to the packed field.
176+
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), repr(packed))]
177+
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
178+
pub struct PackedFingerprint(Fingerprint);
179+
180+
impl std::fmt::Display for PackedFingerprint {
181+
#[inline]
182+
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183+
// Copy to avoid taking reference to packed field.
184+
let copy = self.0;
185+
copy.fmt(formatter)
186+
}
187+
}
188+
189+
impl<E: rustc_serialize::Encoder> Encodable<E> for PackedFingerprint {
190+
#[inline]
191+
fn encode(&self, s: &mut E) -> Result<(), E::Error> {
192+
// Copy to avoid taking reference to packed field.
193+
let copy = self.0;
194+
copy.encode(s)
195+
}
196+
}
197+
198+
impl<D: rustc_serialize::Decoder> Decodable<D> for PackedFingerprint {
199+
#[inline]
200+
fn decode(d: &mut D) -> Result<Self, D::Error> {
201+
Fingerprint::decode(d).map(|f| PackedFingerprint(f))
202+
}
203+
}
204+
205+
impl From<Fingerprint> for PackedFingerprint {
206+
#[inline]
207+
fn from(f: Fingerprint) -> PackedFingerprint {
208+
PackedFingerprint(f)
209+
}
210+
}
211+
212+
impl From<PackedFingerprint> for Fingerprint {
213+
#[inline]
214+
fn from(f: PackedFingerprint) -> Fingerprint {
215+
f.0
216+
}
217+
}

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#![feature(once_cell)]
3232
#![feature(maybe_uninit_uninit_array)]
3333
#![allow(rustc::default_hash_types)]
34+
#![deny(unaligned_references)]
3435

3536
#[macro_use]
3637
extern crate tracing;

compiler/rustc_middle/src/dep_graph/dep_node.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -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: def_path_hash.0.into(),
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.into());
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: Fingerprint::ZERO.into() }
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: hash.into() };
7575

7676
#[cfg(debug_assertions)]
7777
{

compiler/rustc_query_system/src/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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: self.anon_id_seed.combine(hasher.finish()).into(),
980980
};
981981

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

0 commit comments

Comments
 (0)