]> git.lizzy.rs Git - rust.git/commitdiff
Optimize `Substs::super_fold_with`.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 12 Oct 2016 05:59:53 +0000 (16:59 +1100)
committerNicholas Nethercote <nnethercote@mozilla.com>
Tue, 18 Oct 2016 09:55:23 +0000 (20:55 +1100)
This speeds up several rustc-benchmarks by 1--4%.

src/librustc/ty/subst.rs

index 7d7bbd931225f5593f542bf6fec727f827a34060..6beb2b968e2b534c734d5969ecd3ff29d5d6a6c1 100644 (file)
@@ -304,8 +304,15 @@ pub fn rebase_onto(&self, tcx: TyCtxt<'a, 'gcx, '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 {