Skip to content

Commit 9ac628f

Browse files
committed
Auto merge of rust-lang#127288 - lqd:typelen-cache, r=<try>
cache type sizes in type-size limit visitor This is rust-lang#125507 (comment) as lcnr can't open the PR now. Locally it reduces the `itertools` regression by quite a bit, "only +50%" compared to nightly. ```console Benchmark 1: cargo +stage1 build --release Time (mean ± σ): 2.721 s ± 0.009 s [User: 2.446 s, System: 0.325 s] Range (min … max): 2.710 s … 2.738 s 10 runs Benchmark 2: cargo +nightly build --release Time (mean ± σ): 1.784 s ± 0.005 s [User: 1.540 s, System: 0.279 s] Range (min … max): 1.778 s … 1.792 s 10 runs Summary cargo +nightly build --release ran 1.52 ± 0.01 times faster than cargo +stage1 build --release ``` On master, it's from 34s to the 2.7s above. r? compiler-errors just as a cc, as they said they might open the same PR later Opening as draft to do a perf run to see `deeply-nested-multi` (which should be fixed on the perf server now), and validate bootstrap times.
2 parents 1cfd47f + 96312fb commit 9ac628f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

compiler/rustc_middle/src/ty/instance.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::ty::{
55
self, EarlyBinder, GenericArgs, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
66
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
77
};
8+
use rustc_data_structures::fx::FxHashMap;
89
use rustc_errors::ErrorGuaranteed;
910
use rustc_hir as hir;
1011
use rustc_hir::def::Namespace;
@@ -388,21 +389,32 @@ impl<'tcx> InstanceKind<'tcx> {
388389
}
389390

390391
fn type_length<'tcx>(item: impl TypeVisitable<TyCtxt<'tcx>>) -> usize {
391-
struct Visitor {
392+
struct Visitor<'tcx> {
392393
type_length: usize,
394+
cache: FxHashMap<Ty<'tcx>, usize>,
393395
}
394-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor {
396+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor<'tcx> {
395397
fn visit_ty(&mut self, t: Ty<'tcx>) {
398+
if let Some(&value) = self.cache.get(&t) {
399+
self.type_length += value;
400+
}
401+
402+
let prev = self.type_length;
396403
self.type_length += 1;
397404
t.super_visit_with(self);
405+
406+
// We don't try to use the cache if the type is fairly small.
407+
if self.type_length > 16 {
408+
self.cache.insert(t, self.type_length - prev);
409+
}
398410
}
399411

400412
fn visit_const(&mut self, ct: ty::Const<'tcx>) {
401413
self.type_length += 1;
402414
ct.super_visit_with(self);
403415
}
404416
}
405-
let mut visitor = Visitor { type_length: 0 };
417+
let mut visitor = Visitor { type_length: 0, cache: Default::default() };
406418
item.visit_with(&mut visitor);
407419

408420
visitor.type_length

0 commit comments

Comments
 (0)