Skip to content

Commit 014931b

Browse files
committed
Rollup merge of rust-lang#47656 - ishitatsuyuki:patch-1, r=nikomatsakis
[perf] Use std based dedup in projection Unstable sort was added recently, and the code that is being modified is 3 years old. As quicksort doesn't allocate it will likely perform as well as, or better than linear search. I didn't benchmark. Have a perf run.
2 parents 8dd36af + c6772b4 commit 014931b

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

src/librustc/traits/project.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub struct MismatchedProjectionTypes<'tcx> {
101101
pub err: ty::error::TypeError<'tcx>
102102
}
103103

104-
#[derive(PartialEq, Eq, Debug)]
104+
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
105105
enum ProjectionTyCandidate<'tcx> {
106106
// from a where-clause in the env or object type
107107
ParamEnv(ty::PolyProjectionPredicate<'tcx>),
@@ -838,21 +838,12 @@ fn project_type<'cx, 'gcx, 'tcx>(
838838
// Drop duplicates.
839839
//
840840
// Note: `candidates.vec` seems to be on the critical path of the
841-
// compiler. Replacing it with an hash set was also tried, which would
842-
// render the following dedup unnecessary. It led to cleaner code but
843-
// prolonged compiling time of `librustc` from 5m30s to 6m in one test, or
844-
// ~9% performance lost.
845-
if candidates.vec.len() > 1 {
846-
let mut i = 0;
847-
while i < candidates.vec.len() {
848-
let has_dup = (0..i).any(|j| candidates.vec[i] == candidates.vec[j]);
849-
if has_dup {
850-
candidates.vec.swap_remove(i);
851-
} else {
852-
i += 1;
853-
}
854-
}
855-
}
841+
// compiler. Replacing it with an HashSet was also tried, which would
842+
// render the following dedup unnecessary. The original comment indicated
843+
// that it was 9% slower, but that data is now obsolete and a new
844+
// benchmark should be performed.
845+
candidates.vec.sort_unstable();
846+
candidates.vec.dedup();
856847

857848
// Prefer where-clauses. As in select, if there are multiple
858849
// candidates, we prefer where-clause candidates over impls. This

src/librustc/ty/mod.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use serialize::{self, Encodable, Encoder};
4141
use std::cell::RefCell;
4242
use std::collections::BTreeMap;
4343
use std::cmp;
44+
use std::cmp::Ordering;
4445
use std::fmt;
4546
use std::hash::{Hash, Hasher};
4647
use std::iter::FromIterator;
@@ -499,6 +500,20 @@ impl<'tcx> Hash for TyS<'tcx> {
499500
}
500501
}
501502

503+
impl<'tcx> Ord for TyS<'tcx> {
504+
#[inline]
505+
fn cmp(&self, other: &TyS<'tcx>) -> Ordering {
506+
// (self as *const _).cmp(other as *const _)
507+
(self as *const TyS<'tcx>).cmp(&(other as *const TyS<'tcx>))
508+
}
509+
}
510+
impl<'tcx> PartialOrd for TyS<'tcx> {
511+
#[inline]
512+
fn partial_cmp(&self, other: &TyS<'tcx>) -> Option<Ordering> {
513+
Some(self.cmp(other))
514+
}
515+
}
516+
502517
impl<'tcx> TyS<'tcx> {
503518
pub fn is_primitive_ty(&self) -> bool {
504519
match self.sty {
@@ -568,6 +583,19 @@ impl<T> PartialEq for Slice<T> {
568583
}
569584
impl<T> Eq for Slice<T> {}
570585

586+
impl<T> Ord for Slice<T> {
587+
#[inline]
588+
fn cmp(&self, other: &Slice<T>) -> Ordering {
589+
(&self.0 as *const [T]).cmp(&(&other.0 as *const [T]))
590+
}
591+
}
592+
impl<T> PartialOrd for Slice<T> {
593+
#[inline]
594+
fn partial_cmp(&self, other: &Slice<T>) -> Option<Ordering> {
595+
Some(self.cmp(other))
596+
}
597+
}
598+
571599
impl<T> Hash for Slice<T> {
572600
fn hash<H: Hasher>(&self, s: &mut H) {
573601
(self.as_ptr(), self.len()).hash(s)
@@ -1103,7 +1131,7 @@ pub type PolySubtypePredicate<'tcx> = ty::Binder<SubtypePredicate<'tcx>>;
11031131
/// equality between arbitrary types. Processing an instance of
11041132
/// Form #2 eventually yields one of these `ProjectionPredicate`
11051133
/// instances to normalize the LHS.
1106-
#[derive(Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1134+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
11071135
pub struct ProjectionPredicate<'tcx> {
11081136
pub projection_ty: ProjectionTy<'tcx>,
11091137
pub ty: Ty<'tcx>,

src/librustc/ty/sty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ impl<'tcx> PolyExistentialTraitRef<'tcx> {
638638
/// erase, or otherwise "discharge" these bound regions, we change the
639639
/// type from `Binder<T>` to just `T` (see
640640
/// e.g. `liberate_late_bound_regions`).
641-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
641+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
642642
pub struct Binder<T>(pub T);
643643

644644
impl<T> Binder<T> {
@@ -738,7 +738,7 @@ impl<T> Binder<T> {
738738

739739
/// Represents the projection of an associated type. In explicit UFCS
740740
/// form this would be written `<T as Trait<..>>::N`.
741-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
741+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
742742
pub struct ProjectionTy<'tcx> {
743743
/// The parameters of the associated item.
744744
pub substs: &'tcx Substs<'tcx>,

src/librustc/ty/subst.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::mem;
2929
/// To reduce memory usage, a `Kind` is a interned pointer,
3030
/// with the lowest 2 bits being reserved for a tag to
3131
/// indicate the type (`Ty` or `Region`) it points to.
32-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
32+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3333
pub struct Kind<'tcx> {
3434
ptr: NonZero<usize>,
3535
marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>)>

0 commit comments

Comments
 (0)