@@ -5,13 +5,15 @@ use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
55use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
66use rustc_index:: vec:: IndexVec ;
77use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
8+ use smallvec:: SmallVec ;
89use std:: iter;
910use std:: ops:: { Deref , DerefMut , Index , IndexMut } ;
1011use std:: vec:: IntoIter ;
1112
1213#[ derive( Clone , Debug ) ]
1314pub 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
1719impl 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