Skip to content

Use a SmallVec for Cache::predecessors. #70876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/librustc_middle/mir/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use rustc_data_structures::graph::{self, GraphPredecessors, GraphSuccessors};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_index::vec::IndexVec;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use smallvec::SmallVec;
use std::iter;
use std::ops::{Deref, DerefMut, Index, IndexMut};
use std::vec::IntoIter;

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

impl rustc_serialize::Encodable for Cache {
Expand Down Expand Up @@ -44,7 +46,7 @@ impl Cache {

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

/// This will recompute the predecessors cache if it is not available
fn predecessors(&mut self, body: &Body<'_>) -> &IndexVec<BasicBlock, Vec<BasicBlock>> {
// njn: typedef?
fn predecessors(
&mut self,
body: &Body<'_>,
) -> &IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>> {
self.ensure_predecessors(body);
self.predecessors.as_ref().unwrap()
}
Expand Down Expand Up @@ -137,7 +143,7 @@ impl BodyAndCache<'tcx> {
self.cache.ensure_predecessors(&self.body);
}

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

Expand Down Expand Up @@ -199,7 +205,7 @@ impl ReadOnlyBodyAndCache<'a, 'tcx> {
Self { body, cache }
}

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

Expand Down