Skip to content

Commit e97a6e7

Browse files
authored
Rollup merge of #70876 - nnethercote:smallvec-predecessors, r=estebank
Use a `SmallVec` for `Cache::predecessors`. This is a very small win.
2 parents b9bb126 + a90661a commit e97a6e7

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/librustc_middle/mir/cache.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
55
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
66
use rustc_index::vec::IndexVec;
77
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
8+
use smallvec::SmallVec;
89
use std::iter;
910
use std::ops::{Deref, DerefMut, Index, IndexMut};
1011
use std::vec::IntoIter;
1112

1213
#[derive(Clone, Debug)]
1314
pub struct Cache {
14-
predecessors: Option<IndexVec<BasicBlock, Vec<BasicBlock>>>,
15+
// Typically 95%+ of the inner vectors have 4 or fewer elements.
16+
predecessors: Option<IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>>>,
1517
}
1618

1719
impl rustc_serialize::Encodable for Cache {
@@ -44,7 +46,7 @@ impl Cache {
4446

4547
pub fn ensure_predecessors(&mut self, body: &Body<'_>) {
4648
if self.predecessors.is_none() {
47-
let mut result = IndexVec::from_elem(vec![], body.basic_blocks());
49+
let mut result = IndexVec::from_elem(smallvec![], body.basic_blocks());
4850
for (bb, data) in body.basic_blocks().iter_enumerated() {
4951
if let Some(ref term) = data.terminator {
5052
for &tgt in term.successors() {
@@ -58,7 +60,11 @@ impl Cache {
5860
}
5961

6062
/// This will recompute the predecessors cache if it is not available
61-
fn predecessors(&mut self, body: &Body<'_>) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
63+
// njn: typedef?
64+
fn predecessors(
65+
&mut self,
66+
body: &Body<'_>,
67+
) -> &IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>> {
6268
self.ensure_predecessors(body);
6369
self.predecessors.as_ref().unwrap()
6470
}
@@ -137,7 +143,7 @@ impl BodyAndCache<'tcx> {
137143
self.cache.ensure_predecessors(&self.body);
138144
}
139145

140-
pub fn predecessors(&mut self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
146+
pub fn predecessors(&mut self) -> &IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>> {
141147
self.cache.predecessors(&self.body)
142148
}
143149

@@ -199,7 +205,7 @@ impl ReadOnlyBodyAndCache<'a, 'tcx> {
199205
Self { body, cache }
200206
}
201207

202-
pub fn predecessors(&self) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
208+
pub fn predecessors(&self) -> &IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>> {
203209
self.cache.predecessors.as_ref().unwrap()
204210
}
205211

0 commit comments

Comments
 (0)