From 1e4241ab31372c7efb5d59b6b6a6711bcbae0e59 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 12 Oct 2016 16:59:53 +1100 Subject: [PATCH 1/2] Optimize `Substs::super_fold_with`. This speeds up several rustc-benchmarks by 1--4%. --- src/librustc/ty/subst.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 7d7bbd931225f..6beb2b968e2b5 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -304,8 +304,15 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> { impl<'tcx> TypeFoldable<'tcx> for &'tcx Substs<'tcx> { fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { - let params = self.iter().map(|k| k.fold_with(folder)).collect(); - folder.tcx().mk_substs(params) + let params: Vec<_> = self.iter().map(|k| k.fold_with(folder)).collect(); + + // If folding doesn't change the substs, it's faster to avoid calling + // `mk_substs` and instead reuse the existing substs. + if params[..] == self[..] { + self + } else { + folder.tcx().mk_substs(params) + } } fn fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { From ab5dcff1314a800b03fd179a22610f4640a9d071 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Wed, 19 Oct 2016 07:37:20 +0300 Subject: [PATCH 2/2] Format comment in a nicer way. Just kidding I'm doing this only to unstuck @bors/homu/buildbot. --- src/librustc/ty/subst.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 6beb2b968e2b5..e82da3c395831 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -306,8 +306,8 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx Substs<'tcx> { fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { let params: Vec<_> = self.iter().map(|k| k.fold_with(folder)).collect(); - // If folding doesn't change the substs, it's faster to avoid calling - // `mk_substs` and instead reuse the existing substs. + // If folding doesn't change the substs, it's faster to avoid + // calling `mk_substs` and instead reuse the existing substs. if params[..] == self[..] { self } else {