Skip to content

Commit 5c8fdfe

Browse files
committed
Introduce IndexArray.
1 parent 689936f commit 5c8fdfe

File tree

2 files changed

+164
-23
lines changed

2 files changed

+164
-23
lines changed

compiler/rustc_index/src/vec.rs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,5 +842,144 @@ impl<I: Idx> FnMut<(usize,)> for IntoIdx<I> {
842842
}
843843
}
844844

845+
#[derive(Clone, PartialEq, Eq, Hash)]
846+
pub struct IndexArray<I: Idx, T> {
847+
pub raw: Box<[T]>,
848+
_marker: PhantomData<fn(&I)>,
849+
}
850+
851+
// Whether `IndexArray` is `Send` depends only on the data,
852+
// not the phantom data.
853+
unsafe impl<I: Idx, T> Send for IndexArray<I, T> where T: Send {}
854+
855+
impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for IndexArray<I, T> {
856+
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
857+
Encodable::encode(&self.raw, s)
858+
}
859+
}
860+
861+
impl<S: Encoder, I: Idx, T: Encodable<S>> Encodable<S> for &IndexArray<I, T> {
862+
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
863+
Encodable::encode(&self.raw, s)
864+
}
865+
}
866+
867+
impl<D: Decoder, I: Idx, T: Decodable<D>> Decodable<D> for IndexArray<I, T> {
868+
fn decode(d: &mut D) -> Result<Self, D::Error> {
869+
Decodable::decode(d).map(|v| IndexArray { raw: v, _marker: PhantomData })
870+
}
871+
}
872+
873+
impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexArray<I, T> {
874+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
875+
fmt::Debug::fmt(&self.raw, fmt)
876+
}
877+
}
878+
879+
impl<I: Idx, T> IndexArray<I, T> {
880+
#[inline]
881+
pub fn new() -> Self {
882+
IndexArray { raw: Box::default(), _marker: PhantomData }
883+
}
884+
885+
#[inline]
886+
pub fn from_raw(raw: Box<[T]>) -> Self {
887+
IndexArray { raw, _marker: PhantomData }
888+
}
889+
890+
#[inline]
891+
pub fn from_vec(raw: Vec<T>) -> Self {
892+
IndexArray { raw: raw.into(), _marker: PhantomData }
893+
}
894+
895+
#[inline]
896+
pub fn len(&self) -> usize {
897+
self.raw.len()
898+
}
899+
900+
#[inline]
901+
pub fn is_empty(&self) -> bool {
902+
self.raw.is_empty()
903+
}
904+
905+
#[inline]
906+
pub fn iter(&self) -> slice::Iter<'_, T> {
907+
self.raw.iter()
908+
}
909+
910+
#[inline]
911+
pub fn iter_enumerated(&self) -> Enumerated<I, slice::Iter<'_, T>> {
912+
self.raw.iter().enumerate().map(IntoIdx { _marker: PhantomData })
913+
}
914+
915+
#[inline]
916+
pub fn indices(&self) -> iter::Map<Range<usize>, IntoIdx<I>> {
917+
(0..self.len()).map(IntoIdx { _marker: PhantomData })
918+
}
919+
920+
#[inline]
921+
pub fn iter_mut(&mut self) -> slice::IterMut<'_, T> {
922+
self.raw.iter_mut()
923+
}
924+
925+
#[inline]
926+
pub fn iter_enumerated_mut(&mut self) -> Enumerated<I, slice::IterMut<'_, T>> {
927+
self.raw.iter_mut().enumerate().map(IntoIdx { _marker: PhantomData })
928+
}
929+
}
930+
931+
impl<I: Idx, T> Index<I> for IndexArray<I, T> {
932+
type Output = T;
933+
934+
#[inline]
935+
fn index(&self, index: I) -> &T {
936+
&self.raw[index.index()]
937+
}
938+
}
939+
940+
impl<I: Idx, T> IndexMut<I> for IndexArray<I, T> {
941+
#[inline]
942+
fn index_mut(&mut self, index: I) -> &mut T {
943+
&mut self.raw[index.index()]
944+
}
945+
}
946+
947+
impl<I: Idx, T> Default for IndexArray<I, T> {
948+
#[inline]
949+
fn default() -> Self {
950+
Self::new()
951+
}
952+
}
953+
954+
impl<I: Idx, T> FromIterator<T> for IndexArray<I, T> {
955+
#[inline]
956+
fn from_iter<J>(iter: J) -> Self
957+
where
958+
J: IntoIterator<Item = T>,
959+
{
960+
IndexArray { raw: FromIterator::from_iter(iter), _marker: PhantomData }
961+
}
962+
}
963+
964+
impl<'a, I: Idx, T> IntoIterator for &'a IndexArray<I, T> {
965+
type Item = &'a T;
966+
type IntoIter = slice::Iter<'a, T>;
967+
968+
#[inline]
969+
fn into_iter(self) -> slice::Iter<'a, T> {
970+
self.raw.iter()
971+
}
972+
}
973+
974+
impl<'a, I: Idx, T> IntoIterator for &'a mut IndexArray<I, T> {
975+
type Item = &'a mut T;
976+
type IntoIter = slice::IterMut<'a, T>;
977+
978+
#[inline]
979+
fn into_iter(self) -> slice::IterMut<'a, T> {
980+
self.raw.iter_mut()
981+
}
982+
}
983+
845984
#[cfg(test)]
846985
mod tests;

compiler/rustc_query_system/src/dep_graph/serialized.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::query::DepGraphQuery;
44
use super::{DepKind, DepNode};
55
use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::FxHashMap;
7-
use rustc_index::vec::{Idx, IndexVec};
7+
use rustc_index::vec::{Idx, IndexArray, IndexVec};
88
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
99
use std::convert::TryInto;
1010
use std::ops::Range;
@@ -140,19 +140,17 @@ fn shrink_range(range: Range<usize>) -> Range<u32> {
140140
}
141141

142142
/// Data for use when recompiling the **previous crate**.
143-
///
144-
/// Those IndexVec are never pushed to, so as to avoid large reallocations.
145143
#[derive(Debug)]
146144
pub struct SerializedDepGraph<K: DepKind> {
147145
/// The set of all DepNodes in the graph
148-
nodes: IndexVec<SerializedDepNodeIndex, DepNode<K>>,
146+
nodes: IndexArray<SerializedDepNodeIndex, DepNode<K>>,
149147
/// The set of all Fingerprints in the graph. Each Fingerprint corresponds to
150148
/// the DepNode at the same index in the nodes vector.
151-
fingerprints: IndexVec<SerializedDepNodeIndex, Fingerprint>,
149+
fingerprints: IndexArray<SerializedDepNodeIndex, Fingerprint>,
152150
/// For each DepNode, stores the list of edges originating from that
153151
/// DepNode. Encoded as a [start, end) pair indexing into edge_list_data,
154152
/// which holds the actual DepNodeIndices of the target nodes.
155-
edge_list_indices: IndexVec<SerializedDepNodeIndex, ColorAndOffset>,
153+
edge_list_indices: IndexArray<SerializedDepNodeIndex, ColorAndOffset>,
156154
/// A flattened list of all edge targets in the graph. Edge sources are
157155
/// implicit in edge_list_indices.
158156
edge_list_data: Vec<DepNodeIndex>,
@@ -182,9 +180,9 @@ pub struct CurrentDepGraph<K: DepKind> {
182180
impl<K: DepKind> Default for SerializedDepGraph<K> {
183181
fn default() -> Self {
184182
Self {
185-
nodes: IndexVec::new(),
186-
fingerprints: IndexVec::new(),
187-
edge_list_indices: IndexVec::new(),
183+
nodes: IndexArray::new(),
184+
fingerprints: IndexArray::new(),
185+
edge_list_indices: IndexArray::new(),
188186
edge_list_data: Vec::new(),
189187
}
190188
}
@@ -606,20 +604,19 @@ fn edge_count_estimate(prev_graph_node_count: usize) -> usize {
606604
impl<D: Decoder, K: DepKind + Decodable<D>> Decodable<D> for SerializedDepGraph<K> {
607605
fn decode(d: &mut D) -> Result<SerializedDepGraph<K>, D::Error> {
608606
d.read_struct("SerializedDepGraph", 4, |d| {
609-
let nodes: IndexVec<SerializedDepNodeIndex, DepNode<K>> =
610-
d.read_struct_field("nodes", 0, |d| {
611-
d.read_seq(|d, len| {
612-
let mut nodes = IndexVec::with_capacity(len);
613-
for i in 0..len {
614-
let node = d.read_seq_elt(i, Decodable::decode)?;
615-
nodes.push(node);
616-
}
617-
Ok(nodes)
618-
})
619-
})?;
607+
let nodes = d.read_struct_field("nodes", 0, |d| {
608+
d.read_seq(|d, len| {
609+
let mut nodes = Vec::with_capacity(len);
610+
for i in 0..len {
611+
let node = d.read_seq_elt(i, Decodable::decode)?;
612+
nodes.push(node);
613+
}
614+
Ok(nodes)
615+
})
616+
})?;
620617
let fingerprints = d.read_struct_field("fingerprints", 1, |d| {
621618
d.read_seq(|d, len| {
622-
let mut fingerprints = IndexVec::with_capacity(len);
619+
let mut fingerprints = Vec::with_capacity(len);
623620
for i in 0..len {
624621
let fingerprint = d.read_seq_elt(i, Decodable::decode)?;
625622
fingerprints.push(fingerprint);
@@ -639,7 +636,7 @@ impl<D: Decoder, K: DepKind + Decodable<D>> Decodable<D> for SerializedDepGraph<
639636
})?;
640637
let edge_list_indices = d.read_struct_field("edge_list_ends", 3, |d| {
641638
d.read_seq(|d, len| {
642-
let mut indices = IndexVec::with_capacity(len);
639+
let mut indices = Vec::with_capacity(len);
643640
let mut start: u32 = 0;
644641
for i in 0..len {
645642
let end: u32 = d.read_seq_elt(i, Decodable::decode)?;
@@ -650,7 +647,12 @@ impl<D: Decoder, K: DepKind + Decodable<D>> Decodable<D> for SerializedDepGraph<
650647
})
651648
})?;
652649

653-
Ok(SerializedDepGraph { nodes, fingerprints, edge_list_indices, edge_list_data })
650+
Ok(SerializedDepGraph {
651+
nodes: IndexArray::from_vec(nodes),
652+
fingerprints: IndexArray::from_vec(fingerprints),
653+
edge_list_indices: IndexArray::from_vec(edge_list_indices),
654+
edge_list_data,
655+
})
654656
})
655657
}
656658
}

0 commit comments

Comments
 (0)