Skip to content

Commit 5cd9d92

Browse files
committed
Avoid Vec allocation in TyCtxt::mk_place_elem
`mk_place_elem` appends a single `PlaceElem` to an existing (interned) projection. The current implementation copies the projection into a fresh `Vec`, pushes the new element, and re-interns the slice, which allocates on the heap on every call. Feed the elements through `mk_place_elems_from_iter` so that `CollectAndApply`'s hand-unrolled stack fast path (up to 9 elements, in `rustc_type_ir::interner`) kicks in for the common case of short projections and the `Vec` allocation is skipped entirely. The behavior is identical for longer projections (the fast path falls back to a `Vec` internally).
1 parent 3655153 commit 5cd9d92

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,10 +2399,10 @@ impl<'tcx> TyCtxt<'tcx> {
23992399
/// to build a full `Place` it's just a convenient way to grab a projection and modify it in
24002400
/// flight.
24012401
pub fn mk_place_elem(self, place: Place<'tcx>, elem: PlaceElem<'tcx>) -> Place<'tcx> {
2402-
let mut projection = place.projection.to_vec();
2403-
projection.push(elem);
2404-
2405-
Place { local: place.local, projection: self.mk_place_elems(&projection) }
2402+
Place {
2403+
local: place.local,
2404+
projection: self.mk_place_elems_from_iter(place.projection.iter().chain([elem])),
2405+
}
24062406
}
24072407

24082408
pub fn mk_poly_existential_predicates(

0 commit comments

Comments
 (0)