Skip to content
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

Type walker small vector #37760

Merged
merged 1 commit into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use syntax::symbol::{Symbol, InternedString};
use syntax_pos::{DUMMY_SP, Span};

use rustc_const_math::ConstInt;
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;

use hir;
use hir::itemlikevisit::ItemLikeVisitor;
Expand Down Expand Up @@ -1887,7 +1888,7 @@ impl<'tcx> TyS<'tcx> {
/// Iterator that walks the immediate children of `self`. Hence
/// `Foo<Bar<i32>, u32>` yields the sequence `[Bar<i32>, u32]`
/// (but not `i32`, like `walk`).
pub fn walk_shallow(&'tcx self) -> IntoIter<Ty<'tcx>> {
pub fn walk_shallow(&'tcx self) -> AccIntoIter<walk::TypeWalkerArray<'tcx>> {
walk::walk_shallow(self)
}

Expand Down
21 changes: 13 additions & 8 deletions src/librustc/ty/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@
//! WARNING: this does not keep track of the region depth.
use ty::{self, Ty};
use std::iter::Iterator;
use std::vec::IntoIter;
use rustc_data_structures::small_vec::SmallVec;
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;

// The TypeWalker's stack is hot enough that it's worth going to some effort to
// avoid heap allocations.
pub type TypeWalkerArray<'tcx> = [Ty<'tcx>; 8];
pub type TypeWalkerStack<'tcx> = SmallVec<TypeWalkerArray<'tcx>>;

pub struct TypeWalker<'tcx> {
stack: Vec<Ty<'tcx>>,
stack: TypeWalkerStack<'tcx>,
last_subtree: usize,
}

impl<'tcx> TypeWalker<'tcx> {
pub fn new(ty: Ty<'tcx>) -> TypeWalker<'tcx> {
TypeWalker { stack: vec![ty], last_subtree: 1, }
TypeWalker { stack: SmallVec::one(ty), last_subtree: 1, }
}

/// Skips the subtree of types corresponding to the last type
Expand Down Expand Up @@ -61,8 +66,8 @@ impl<'tcx> Iterator for TypeWalker<'tcx> {
}
}

pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> IntoIter<Ty<'tcx>> {
let mut stack = vec![];
pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> AccIntoIter<TypeWalkerArray<'tcx>> {
let mut stack = SmallVec::new();
push_subtypes(&mut stack, ty);
stack.into_iter()
}
Expand All @@ -73,7 +78,7 @@ pub fn walk_shallow<'tcx>(ty: Ty<'tcx>) -> IntoIter<Ty<'tcx>> {
// known to be significant to any code, but it seems like the
// natural order one would expect (basically, the order of the
// types as they are written).
fn push_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, parent_ty: Ty<'tcx>) {
fn push_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent_ty: Ty<'tcx>) {
match parent_ty.sty {
ty::TyBool | ty::TyChar | ty::TyInt(_) | ty::TyUint(_) | ty::TyFloat(_) |
ty::TyStr | ty::TyInfer(_) | ty::TyParam(_) | ty::TyNever | ty::TyError => {
Expand Down Expand Up @@ -112,7 +117,7 @@ fn push_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, parent_ty: Ty<'tcx>) {
}
}

fn push_sig_subtypes<'tcx>(stack: &mut Vec<Ty<'tcx>>, sig: &ty::PolyFnSig<'tcx>) {
fn push_sig_subtypes<'tcx>(stack: &mut TypeWalkerStack<'tcx>, sig: &ty::PolyFnSig<'tcx>) {
stack.push(sig.0.output);
stack.extend(sig.0.inputs.iter().cloned().rev());
}
12 changes: 12 additions & 0 deletions src/librustc_data_structures/small_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ impl<A: Array> SmallVec<A> {
self.set_len(len + 1);
}
}

pub fn truncate(&mut self, len: usize) {
unsafe {
while len < self.len() {
// Decrement len before the drop_in_place(), so a panic on Drop
// doesn't re-drop the just-failed value.
let newlen = self.len() - 1;
self.set_len(newlen);
::std::ptr::drop_in_place(self.get_unchecked_mut(newlen));
}
}
}
}

impl<A: Array> Deref for SmallVec<A> {
Expand Down