Skip to content

Commit f174fd7

Browse files
committed
Auto merge of #139287 - compiler-errors:folder-experiment-1, r=lqd
Folder experiment: Monomorphize region resolver **NOTE:** This is one of a series of perf experiments that I've come up with while sick in bed. I'm assigning them to lqd b/c you're a good reviewer and you'll hopefully be awake when these experiments finish, lol. r? lqd This is actually two tweaks to the `RegionFolder`, monomorphizing its callback and accounting for flags to avoid folding unnecessarily.
2 parents 9e14530 + 60b742d commit f174fd7

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

compiler/rustc_type_ir/src/fold.rs

+42-11
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use tracing::{debug, instrument};
5454

5555
use crate::inherent::*;
5656
use crate::visit::{TypeVisitable, TypeVisitableExt as _};
57-
use crate::{self as ty, Interner};
57+
use crate::{self as ty, Interner, TypeFlags};
5858

5959
#[cfg(feature = "nightly")]
6060
type Never = !;
@@ -438,12 +438,12 @@ where
438438
pub fn fold_regions<I: Interner, T>(
439439
cx: I,
440440
value: T,
441-
mut f: impl FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
441+
f: impl FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
442442
) -> T
443443
where
444444
T: TypeFoldable<I>,
445445
{
446-
value.fold_with(&mut RegionFolder::new(cx, &mut f))
446+
value.fold_with(&mut RegionFolder::new(cx, f))
447447
}
448448

449449
/// Folds over the substructure of a type, visiting its component
@@ -453,7 +453,7 @@ where
453453
/// new bound regions which are not visited by this visitors as
454454
/// they are not free; only regions that occur free will be
455455
/// visited by `fld_r`.
456-
pub struct RegionFolder<'a, I: Interner> {
456+
pub struct RegionFolder<I, F> {
457457
cx: I,
458458

459459
/// Stores the index of a binder *just outside* the stuff we have
@@ -464,20 +464,21 @@ pub struct RegionFolder<'a, I: Interner> {
464464
/// Callback invokes for each free region. The `DebruijnIndex`
465465
/// points to the binder *just outside* the ones we have passed
466466
/// through.
467-
fold_region_fn: &'a mut (dyn FnMut(I::Region, ty::DebruijnIndex) -> I::Region + 'a),
467+
fold_region_fn: F,
468468
}
469469

470-
impl<'a, I: Interner> RegionFolder<'a, I> {
470+
impl<I, F> RegionFolder<I, F> {
471471
#[inline]
472-
pub fn new(
473-
cx: I,
474-
fold_region_fn: &'a mut dyn FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
475-
) -> RegionFolder<'a, I> {
472+
pub fn new(cx: I, fold_region_fn: F) -> RegionFolder<I, F> {
476473
RegionFolder { cx, current_index: ty::INNERMOST, fold_region_fn }
477474
}
478475
}
479476

480-
impl<'a, I: Interner> TypeFolder<I> for RegionFolder<'a, I> {
477+
impl<I, F> TypeFolder<I> for RegionFolder<I, F>
478+
where
479+
I: Interner,
480+
F: FnMut(I::Region, ty::DebruijnIndex) -> I::Region,
481+
{
481482
fn cx(&self) -> I {
482483
self.cx
483484
}
@@ -502,4 +503,34 @@ impl<'a, I: Interner> TypeFolder<I> for RegionFolder<'a, I> {
502503
}
503504
}
504505
}
506+
507+
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
508+
if t.has_type_flags(
509+
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
510+
) {
511+
t.super_fold_with(self)
512+
} else {
513+
t
514+
}
515+
}
516+
517+
fn fold_const(&mut self, ct: I::Const) -> I::Const {
518+
if ct.has_type_flags(
519+
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
520+
) {
521+
ct.super_fold_with(self)
522+
} else {
523+
ct
524+
}
525+
}
526+
527+
fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
528+
if p.has_type_flags(
529+
TypeFlags::HAS_FREE_REGIONS | TypeFlags::HAS_RE_BOUND | TypeFlags::HAS_RE_ERASED,
530+
) {
531+
p.super_fold_with(self)
532+
} else {
533+
p
534+
}
535+
}
505536
}

0 commit comments

Comments
 (0)