Skip to content

Commit ac0e10c

Browse files
committed
Encode and decode on the fly.
1 parent 47ce741 commit ac0e10c

File tree

1 file changed

+85
-74
lines changed

1 file changed

+85
-74
lines changed

compiler/rustc_query_system/src/dep_graph/serialized.rs

Lines changed: 85 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -509,70 +509,52 @@ impl<K: DepKind> CurrentDepGraph<K> {
509509
impl<E: Encoder, K: DepKind + Encodable<E>> Encodable<E> for CurrentDepGraph<K> {
510510
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
511511
let remap = self.compression_map();
512-
let node_count = remap.iter().flatten().count();
513-
514-
// Back-copy the nodes and fingerprints.
515-
let (nodes, fingerprints) = {
516-
let mut nodes: IndexVec<SerializedDepNodeIndex, DepNode<K>> =
517-
IndexVec::with_capacity(node_count);
518-
let mut fingerprints: IndexVec<SerializedDepNodeIndex, Fingerprint> =
519-
IndexVec::with_capacity(node_count);
520-
521-
for index in self.live_serialized_indices() {
522-
nodes.push(self.serialized.nodes[index]);
523-
fingerprints.push(self.serialized.fingerprints[index]);
524-
}
525-
nodes.extend(self.nodes.iter().copied());
526-
fingerprints.extend(self.fingerprints.iter().copied());
512+
let live_indices = || remap.iter_enumerated().filter_map(|(s, &n)| Some((s, n?)));
513+
let node_count = live_indices().count();
514+
let edge_count = self.edge_count();
527515

528-
(nodes, fingerprints)
529-
};
516+
e.emit_struct("SerializedDepGraph", 4, |e| {
517+
e.emit_struct_field("nodes", 0, |e| {
518+
e.emit_seq(node_count, |e| {
519+
for (index, new_index) in live_indices() {
520+
let node = self.dep_node_of(index);
521+
e.emit_seq_elt(new_index.index(), |e| node.encode(e))?;
522+
}
523+
Ok(())
524+
})
525+
})?;
526+
e.emit_struct_field("fingerprints", 1, |e| {
527+
e.emit_seq(node_count, |e| {
528+
for (index, new_index) in live_indices() {
529+
let node = self.fingerprint_of(index);
530+
e.emit_seq_elt(new_index.index(), |e| node.encode(e))?;
531+
}
532+
Ok(())
533+
})
534+
})?;
530535

531-
// Reconstruct the edges vector since it may be out of order.
532-
// We only store the start indices, since the end is the next's start.
533-
let (new_indices, new_edges) = {
536+
// Reconstruct the edges vector since it may be out of order.
537+
// We only store the start indices, since the end is the next's start.
534538
let mut new_indices: IndexVec<SerializedDepNodeIndex, u32> =
535-
IndexVec::with_capacity(node_count);
536-
let mut new_edges: Vec<SerializedDepNodeIndex> = Vec::with_capacity(
537-
self.serialized.edge_list_data.len() + self.edge_list_data.len(),
538-
);
539-
540-
for index in self.live_serialized_indices() {
541-
new_indices.push(new_edges.len().try_into().unwrap());
542-
let edges = self.serialized_edges(index);
543-
new_edges.extend(edges.iter().map(|i| {
544-
remap[*i]
545-
.unwrap_or_else(|| panic!("Unknown remap for {:?} while {:?}", *i, index))
546-
}));
547-
}
548-
for index in self.nodes.indices() {
549-
new_indices.push(new_edges.len().try_into().unwrap());
550-
new_edges.extend(self.new_edges(index).iter().map(|i| {
551-
remap[*i]
552-
.unwrap_or_else(|| panic!("Unknown remap for {:?} while {:?}", *i, index))
553-
}));
554-
}
555-
556-
(new_indices, new_edges)
557-
};
558-
559-
debug_assert_eq!(node_count, nodes.len());
560-
debug_assert_eq!(node_count, fingerprints.len());
561-
debug_assert_eq!(node_count, new_indices.len());
562-
563-
let mut index = FxHashMap::default();
564-
for (idx, &dep_node) in nodes.iter_enumerated() {
565-
debug!("ENCODE index={:?} node={:?}", idx, dep_node);
566-
let _o = index.insert(dep_node, idx);
567-
debug_assert_eq!(_o, None);
568-
}
569-
let _ = index;
570-
571-
e.emit_struct("SerializedDepGraph", 4, |e| {
572-
e.emit_struct_field("nodes", 0, |e| nodes.encode(e))?;
573-
e.emit_struct_field("fingerprints", 1, |e| fingerprints.encode(e))?;
574-
e.emit_struct_field("edge_list_indices", 2, |e| new_indices.encode(e))?;
575-
e.emit_struct_field("edge_list_data", 3, |e| new_edges.encode(e))?;
539+
IndexVec::from_elem_n(0u32, node_count);
540+
e.emit_struct_field("edge_list_data", 2, |e| {
541+
e.emit_seq(edge_count, |e| {
542+
let mut pos: u32 = 0;
543+
for (new_index, edges) in self.edge_map().enumerate() {
544+
// Reconstruct the edges vector since it may be out of order.
545+
// We only store the end indices, since the start can be reconstructed.
546+
for &edge in edges {
547+
let edge = remap[edge].unwrap();
548+
e.emit_seq_elt(pos as usize, |e| edge.encode(e))?;
549+
pos += 1;
550+
}
551+
new_indices[SerializedDepNodeIndex::new(new_index)] = pos;
552+
}
553+
debug_assert_eq!(pos as usize, edge_count);
554+
Ok(())
555+
})
556+
})?;
557+
e.emit_struct_field("edge_list_ends", 3, |e| new_indices.encode(e))?;
576558
Ok(())
577559
})
578560
}
@@ -602,19 +584,48 @@ impl<D: Decoder, K: DepKind + Decodable<D>> Decodable<D> for SerializedDepGraph<
602584
fn decode(d: &mut D) -> Result<SerializedDepGraph<K>, D::Error> {
603585
d.read_struct("SerializedDepGraph", 4, |d| {
604586
let nodes: IndexVec<SerializedDepNodeIndex, DepNode<K>> =
605-
d.read_struct_field("nodes", 0, Decodable::decode)?;
606-
let fingerprints: IndexVec<SerializedDepNodeIndex, Fingerprint> =
607-
d.read_struct_field("fingerprints", 1, Decodable::decode)?;
608-
let mut edge_list_indices: IndexVec<SerializedDepNodeIndex, u32> =
609-
d.read_struct_field("edge_list_indices", 2, Decodable::decode)?;
610-
let edge_list_data: Vec<DepNodeIndex> =
611-
d.read_struct_field("edge_list_data", 3, Decodable::decode)?;
612-
613-
edge_list_indices.push(edge_list_data.len().try_into().unwrap());
614-
let edge_list_indices = IndexVec::from_fn_n(
615-
|i| (ColorAndOffset::unknown(edge_list_indices[i]), edge_list_indices[i + 1]),
616-
edge_list_indices.len() - 1,
617-
);
587+
d.read_struct_field("nodes", 0, |d| {
588+
d.read_seq(|d, len| {
589+
let mut nodes = IndexVec::with_capacity(len);
590+
for i in 0..len {
591+
let node = d.read_seq_elt(i, Decodable::decode)?;
592+
nodes.push(node);
593+
}
594+
Ok(nodes)
595+
})
596+
})?;
597+
let fingerprints = d.read_struct_field("fingerprints", 1, |d| {
598+
d.read_seq(|d, len| {
599+
let mut fingerprints = IndexVec::with_capacity(len);
600+
for i in 0..len {
601+
let fingerprint = d.read_seq_elt(i, Decodable::decode)?;
602+
fingerprints.push(fingerprint);
603+
}
604+
Ok(fingerprints)
605+
})
606+
})?;
607+
let edge_list_data = d.read_struct_field("edge_list_data", 2, |d| {
608+
d.read_seq(|d, len| {
609+
let mut edges = Vec::with_capacity(len);
610+
for i in 0..len {
611+
let edge = d.read_seq_elt(i, Decodable::decode)?;
612+
edges.push(edge);
613+
}
614+
Ok(edges)
615+
})
616+
})?;
617+
let edge_list_indices = d.read_struct_field("edge_list_ends", 3, |d| {
618+
d.read_seq(|d, len| {
619+
let mut indices = IndexVec::with_capacity(len);
620+
let mut start: u32 = 0;
621+
for i in 0..len {
622+
let end: u32 = d.read_seq_elt(i, Decodable::decode)?;
623+
indices.push((ColorAndOffset::unknown(start), end));
624+
start = end;
625+
}
626+
Ok(indices)
627+
})
628+
})?;
618629

619630
Ok(SerializedDepGraph { nodes, fingerprints, edge_list_indices, edge_list_data })
620631
})

0 commit comments

Comments
 (0)