Skip to content

Commit 86b4172

Browse files
committed
Auto merge of rust-lang#77028 - andjo403:mini, r=matthewjasper
Move MiniSet to data_structures remove the need for T to be copy from MiniSet as was done for MiniMap MiniMap and MiniSet was added by rust-lang#72412 think that this can be used in rust-lang#68828
2 parents 5562bb6 + 6586c37 commit 86b4172

File tree

9 files changed

+46
-50
lines changed

9 files changed

+46
-50
lines changed

Cargo.lock

-2
Original file line numberDiff line numberDiff line change
@@ -3638,7 +3638,6 @@ dependencies = [
36383638
name = "rustc_infer"
36393639
version = "0.0.0"
36403640
dependencies = [
3641-
"arrayvec",
36423641
"rustc_ast",
36433642
"rustc_data_structures",
36443643
"rustc_errors",
@@ -3778,7 +3777,6 @@ dependencies = [
37783777
name = "rustc_middle"
37793778
version = "0.0.0"
37803779
dependencies = [
3781-
"arrayvec",
37823780
"bitflags",
37833781
"chalk-ir",
37843782
"measureme",

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub mod work_queue;
102102
pub use atomic_ref::AtomicRef;
103103
pub mod frozen;
104104
pub mod mini_map;
105+
pub mod mini_set;
105106
pub mod tagged_ptr;
106107
pub mod temp_dir;
107108
pub mod unhash;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::fx::FxHashSet;
2+
use arrayvec::ArrayVec;
3+
use std::hash::Hash;
4+
/// Small-storage-optimized implementation of a set.
5+
///
6+
/// Stores elements in a small array up to a certain length
7+
/// and switches to `HashSet` when that length is exceeded.
8+
pub enum MiniSet<T> {
9+
Array(ArrayVec<[T; 8]>),
10+
Set(FxHashSet<T>),
11+
}
12+
13+
impl<T: Eq + Hash> MiniSet<T> {
14+
/// Creates an empty `MiniSet`.
15+
pub fn new() -> Self {
16+
MiniSet::Array(ArrayVec::new())
17+
}
18+
19+
/// Adds a value to the set.
20+
///
21+
/// If the set did not have this value present, true is returned.
22+
///
23+
/// If the set did have this value present, false is returned.
24+
pub fn insert(&mut self, elem: T) -> bool {
25+
match self {
26+
MiniSet::Array(array) => {
27+
if array.iter().any(|e| *e == elem) {
28+
false
29+
} else {
30+
if let Err(error) = array.try_push(elem) {
31+
let mut set: FxHashSet<T> = array.drain(..).collect();
32+
set.insert(error.element());
33+
*self = MiniSet::Set(set);
34+
}
35+
true
36+
}
37+
}
38+
MiniSet::Set(set) => set.insert(elem),
39+
}
40+
}
41+
}

compiler/rustc_infer/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ rustc_serialize = { path = "../rustc_serialize" }
2121
rustc_span = { path = "../rustc_span" }
2222
rustc_target = { path = "../rustc_target" }
2323
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
24-
arrayvec = { version = "0.5.1", default-features = false }
2524
rustc_ast = { path = "../rustc_ast" }

compiler/rustc_infer/src/infer/outlives/verify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::infer::outlives::env::RegionBoundPairs;
22
use crate::infer::{GenericKind, VerifyBound};
33
use rustc_data_structures::captures::Captures;
4+
use rustc_data_structures::mini_set::MiniSet;
45
use rustc_hir::def_id::DefId;
56
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
6-
use rustc_middle::ty::walk::MiniSet;
77
use rustc_middle::ty::{self, Ty, TyCtxt};
88

99
/// The `TypeOutlives` struct has the job of "lowering" a `T: 'a`

compiler/rustc_middle/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ rustc_ast = { path = "../rustc_ast" }
2828
rustc_span = { path = "../rustc_span" }
2929
chalk-ir = "0.21.0"
3030
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
31-
arrayvec = { version = "0.5.1", default-features = false }
3231
measureme = "0.7.1"
3332
rustc_session = { path = "../rustc_session" }

compiler/rustc_middle/src/ty/outlives.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// RFC for reference.
44

55
use crate::ty::subst::{GenericArg, GenericArgKind};
6-
use crate::ty::walk::MiniSet;
76
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
7+
use rustc_data_structures::mini_set::MiniSet;
88
use smallvec::SmallVec;
99

1010
#[derive(Debug)]

compiler/rustc_middle/src/ty/print/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::ty::subst::{GenericArg, Subst};
22
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
33

44
use rustc_data_structures::fx::FxHashSet;
5+
use rustc_data_structures::mini_set::MiniSet;
56
use rustc_hir::def_id::{CrateNum, DefId};
67
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
7-
use rustc_middle::ty::walk::MiniSet;
88

99
// `pretty` is a separate module only for organization.
1010
mod pretty;

compiler/rustc_middle/src/ty/walk.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,8 @@
33
44
use crate::ty;
55
use crate::ty::subst::{GenericArg, GenericArgKind};
6-
use arrayvec::ArrayVec;
7-
use rustc_data_structures::fx::FxHashSet;
6+
use rustc_data_structures::mini_set::MiniSet;
87
use smallvec::{self, SmallVec};
9-
use std::hash::Hash;
10-
11-
/// Small-storage-optimized implementation of a set
12-
/// made specifically for walking type tree.
13-
///
14-
/// Stores elements in a small array up to a certain length
15-
/// and switches to `HashSet` when that length is exceeded.
16-
pub enum MiniSet<T> {
17-
Array(ArrayVec<[T; 8]>),
18-
Set(FxHashSet<T>),
19-
}
20-
21-
impl<T: Eq + Hash + Copy> MiniSet<T> {
22-
/// Creates an empty `MiniSet`.
23-
pub fn new() -> Self {
24-
MiniSet::Array(ArrayVec::new())
25-
}
26-
27-
/// Adds a value to the set.
28-
///
29-
/// If the set did not have this value present, true is returned.
30-
///
31-
/// If the set did have this value present, false is returned.
32-
pub fn insert(&mut self, elem: T) -> bool {
33-
match self {
34-
MiniSet::Array(array) => {
35-
if array.iter().any(|e| *e == elem) {
36-
false
37-
} else {
38-
if array.try_push(elem).is_err() {
39-
let mut set: FxHashSet<T> = array.iter().copied().collect();
40-
set.insert(elem);
41-
*self = MiniSet::Set(set);
42-
}
43-
true
44-
}
45-
}
46-
MiniSet::Set(set) => set.insert(elem),
47-
}
48-
}
49-
}
508

519
// The TypeWalker's stack is hot enough that it's worth going to some effort to
5210
// avoid heap allocations.

0 commit comments

Comments
 (0)