Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 39b306a

Browse files
committed
Do not allocate in decoder.
1 parent 6bfaf3a commit 39b306a

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

compiler/rustc_query_system/src/dep_graph/serialized.rs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use super::{DepKind, DepNode, DepNodeIndex};
55
use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_data_structures::sync::{AtomicU32, Lock, Lrc, Ordering};
8-
use rustc_index::vec::{Idx, IndexVec};
8+
use rustc_index::vec::IndexVec;
99
use rustc_serialize::opaque::{self, FileEncodeResult, FileEncoder, IntEncodedWithFixedSize};
10-
use rustc_serialize::{Decodable, Encodable};
10+
use rustc_serialize::{Decodable, Decoder, Encodable};
1111
use smallvec::SmallVec;
1212
use std::convert::TryInto;
1313

@@ -85,31 +85,41 @@ impl<'a, K: DepKind + Decodable<opaque::Decoder<'a>>> Decodable<opaque::Decoder<
8585
let mut edge_list_data = Vec::with_capacity(edge_count);
8686

8787
for _index in 0..node_count {
88-
let node = NodeInfo::<K, SerializedDepNodeIndex>::decode(d)?;
89-
debug!(?_index, ?node);
90-
91-
let _i: SerializedDepNodeIndex = nodes.push(node.node);
92-
debug_assert_eq!(_i.index(), _index);
93-
let _i: SerializedDepNodeIndex = fingerprints.push(node.fingerprint);
94-
debug_assert_eq!(_i.index(), _index);
95-
96-
let start = edge_list_data.len().try_into().unwrap();
97-
edge_list_data.extend(node.edges.into_iter());
98-
let end = edge_list_data.len().try_into().unwrap();
99-
100-
let _i: SerializedDepNodeIndex = edge_list_indices.push((start, end));
101-
debug_assert_eq!(_i.index(), _index);
88+
d.read_struct("NodeInfo", 3, |d| {
89+
let dep_node: DepNode<K> = d.read_struct_field("node", 0, Decodable::decode)?;
90+
let _i: SerializedDepNodeIndex = nodes.push(dep_node);
91+
debug_assert_eq!(_i.index(), _index);
92+
93+
let fingerprint: Fingerprint =
94+
d.read_struct_field("fingerprint", 1, Decodable::decode)?;
95+
let _i: SerializedDepNodeIndex = fingerprints.push(fingerprint);
96+
debug_assert_eq!(_i.index(), _index);
97+
98+
d.read_struct_field("edges", 2, |d| {
99+
d.read_seq(|d, len| {
100+
let start = edge_list_data.len().try_into().unwrap();
101+
for e in 0..len {
102+
let edge = d.read_seq_elt(e, Decodable::decode)?;
103+
edge_list_data.push(edge);
104+
}
105+
let end = edge_list_data.len().try_into().unwrap();
106+
let _i: SerializedDepNodeIndex = edge_list_indices.push((start, end));
107+
debug_assert_eq!(_i.index(), _index);
108+
Ok(())
109+
})
110+
})
111+
})?;
102112
}
103113

104114
Ok(SerializedDepGraph { nodes, fingerprints, edge_list_indices, edge_list_data })
105115
}
106116
}
107117

108118
#[derive(Debug, Encodable, Decodable)]
109-
pub struct NodeInfo<K: DepKind, I: Idx> {
119+
pub struct NodeInfo<K: DepKind> {
110120
node: DepNode<K>,
111121
fingerprint: Fingerprint,
112-
edges: SmallVec<[I; 8]>,
122+
edges: SmallVec<[DepNodeIndex; 8]>,
113123
}
114124

115125
struct Stat<K: DepKind> {
@@ -128,7 +138,7 @@ struct Stats<K: DepKind> {
128138
fn encode_node<K: DepKind>(
129139
encoder: &mut FileEncoder,
130140
_index: DepNodeIndex,
131-
node: &NodeInfo<K, DepNodeIndex>,
141+
node: &NodeInfo<K>,
132142
_record_graph: &Option<Lrc<Lock<DepGraphQuery<K>>>>,
133143
record_stats: &Option<Lrc<Lock<Stats<K>>>>,
134144
) -> FileEncodeResult {
@@ -181,7 +191,7 @@ pub struct GraphEncoder<K: DepKind> {
181191

182192
#[cfg(parallel_compiler)]
183193
pub struct GraphEncoder<K: DepKind> {
184-
send: WorkerLocal<mpsc::Sender<(DepNodeIndex, NodeInfo<K, DepNodeIndex>)>>,
194+
send: WorkerLocal<mpsc::Sender<(DepNodeIndex, NodeInfo<K>)>>,
185195
thread: thread::JoinHandle<FileEncodeResult>,
186196
counter: AtomicU32,
187197
record_graph: Option<Lrc<Lock<DepGraphQuery<K>>>>,
@@ -350,8 +360,8 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
350360
#[instrument(skip(encoder, recv, process))]
351361
fn encode_graph<K: DepKind + Encodable<FileEncoder>>(
352362
mut encoder: FileEncoder,
353-
recv: mpsc::Receiver<(DepNodeIndex, NodeInfo<K, DepNodeIndex>)>,
354-
process: impl Fn(&mut FileEncoder, DepNodeIndex, &NodeInfo<K, DepNodeIndex>) -> FileEncodeResult,
363+
recv: mpsc::Receiver<(DepNodeIndex, NodeInfo<K>)>,
364+
process: impl Fn(&mut FileEncoder, DepNodeIndex, &NodeInfo<K>) -> FileEncodeResult,
355365
) -> FileEncodeResult {
356366
let mut edge_count: usize = 0;
357367
let node_count: usize = ordered_recv(recv, |index, node| {
@@ -366,8 +376,8 @@ fn encode_graph<K: DepKind + Encodable<FileEncoder>>(
366376
/// the messages may not arrive in order. This function sorts them as they come.
367377
#[cfg(parallel_compiler)]
368378
fn ordered_recv<K: DepKind + Encodable<opaque::FileEncoder>>(
369-
recv: mpsc::Receiver<(DepNodeIndex, NodeInfo<K, DepNodeIndex>)>,
370-
mut f: impl FnMut(DepNodeIndex, &NodeInfo<K, DepNodeIndex>) -> FileEncodeResult,
379+
recv: mpsc::Receiver<(DepNodeIndex, NodeInfo<K>)>,
380+
mut f: impl FnMut(DepNodeIndex, &NodeInfo<K>) -> FileEncodeResult,
371381
) -> Result<usize, std::io::Error> {
372382
let mut pending = Vec::<(DepNodeIndex, _)>::new();
373383
let mut expected = DepNodeIndex::new(0);

0 commit comments

Comments
 (0)