1
Fork 0

Move MiniSet to data_structures

remove the need for T to be copy from MiniSet as was done for MiniMap
This commit is contained in:
Andreas Jonson 2020-09-23 08:09:16 +02:00
parent e62323df22
commit 6586c37bec
9 changed files with 46 additions and 50 deletions

View file

@ -28,6 +28,5 @@ rustc_ast = { path = "../rustc_ast" }
rustc_span = { path = "../rustc_span" }
chalk-ir = "0.21.0"
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
arrayvec = { version = "0.5.1", default-features = false }
measureme = "0.7.1"
rustc_session = { path = "../rustc_session" }

View file

@ -3,8 +3,8 @@
// RFC for reference.
use crate::ty::subst::{GenericArg, GenericArgKind};
use crate::ty::walk::MiniSet;
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc_data_structures::mini_set::MiniSet;
use smallvec::SmallVec;
#[derive(Debug)]

View file

@ -2,9 +2,9 @@ use crate::ty::subst::{GenericArg, Subst};
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::mini_set::MiniSet;
use rustc_hir::def_id::{CrateNum, DefId};
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_middle::ty::walk::MiniSet;
// `pretty` is a separate module only for organization.
mod pretty;

View file

@ -3,50 +3,8 @@
use crate::ty;
use crate::ty::subst::{GenericArg, GenericArgKind};
use arrayvec::ArrayVec;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::mini_set::MiniSet;
use smallvec::{self, SmallVec};
use std::hash::Hash;
/// Small-storage-optimized implementation of a set
/// made specifically for walking type tree.
///
/// Stores elements in a small array up to a certain length
/// and switches to `HashSet` when that length is exceeded.
pub enum MiniSet<T> {
Array(ArrayVec<[T; 8]>),
Set(FxHashSet<T>),
}
impl<T: Eq + Hash + Copy> MiniSet<T> {
/// Creates an empty `MiniSet`.
pub fn new() -> Self {
MiniSet::Array(ArrayVec::new())
}
/// Adds a value to the set.
///
/// If the set did not have this value present, true is returned.
///
/// If the set did have this value present, false is returned.
pub fn insert(&mut self, elem: T) -> bool {
match self {
MiniSet::Array(array) => {
if array.iter().any(|e| *e == elem) {
false
} else {
if array.try_push(elem).is_err() {
let mut set: FxHashSet<T> = array.iter().copied().collect();
set.insert(elem);
*self = MiniSet::Set(set);
}
true
}
}
MiniSet::Set(set) => set.insert(elem),
}
}
}
// The TypeWalker's stack is hot enough that it's worth going to some effort to
// avoid heap allocations.