Skip to content

Avoid Vec allocation in TyCtxt::mk_place_elem#155778

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
kevinheavey:perf-mk-place-elem-avoid-vec-alloc
Apr 27, 2026
Merged

Avoid Vec allocation in TyCtxt::mk_place_elem#155778
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
kevinheavey:perf-mk-place-elem-avoid-vec-alloc

Conversation

@kevinheavey
Copy link
Copy Markdown
Contributor

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 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).

`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).
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 25, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 73 candidates
  • Random selection from 20 candidates

@cjgillot
Copy link
Copy Markdown
Contributor

Seems reasonable, let's see what perf says.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 26, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 26, 2026
…c, r=<try>

Avoid Vec allocation in TyCtxt::mk_place_elem
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 26, 2026

☀️ Try build successful (CI)
Build commit: 7a0916d (7a0916d081b78b4e94da1fd8af8f3a954ad92e9c, parent: c7fe5e9d1eb0ef8cc033961956d60f8e2e91e741)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (7a0916d): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

Results (primary 0.4%, secondary -1.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.5% [0.7%, 2.3%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.9% [-1.9%, -1.9%] 1
Improvements ✅
(secondary)
-1.8% [-1.8%, -1.8%] 1
All ❌✅ (primary) 0.4% [-1.9%, 2.3%] 3

Cycles

Results (secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.3% [-4.3%, -4.3%] 1
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.0%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.0% [-0.0%, -0.0%] 11
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 4
All ❌✅ (primary) -0.0% [-0.0%, -0.0%] 11

Bootstrap: 488.641s -> 488.25s (-0.08%)
Artifact size: 393.90 MiB -> 393.84 MiB (-0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 26, 2026
Copy link
Copy Markdown
Member

@JohnTitor JohnTitor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Waiting for @cjgillot checks in.

View changes since this review

@cjgillot
Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 26, 2026

📌 Commit 5cd9d92 has been approved by cjgillot

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 26, 2026
@JohnTitor
Copy link
Copy Markdown
Member

@bors r-

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 26, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 26, 2026

This pull request was unapproved.

View changes since this unapproval

@JohnTitor
Copy link
Copy Markdown
Member

@bors r=cjgillot,JohnTitor

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 26, 2026

📌 Commit 5cd9d92 has been approved by cjgillot,JohnTitor

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Rollup of 12 pull requests

Successful merges:

 - #149624 (Fix requires_lto targets needing lto set in cargo)
 - #155317 (`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.)
 - #155579 (Make Rcs and Arcs use pointer comparison for unsized types)
 - #155588 (Implement more traits for FRTs)
 - #155708 (Fix heap overflow in slice::join caused by misbehaving Borrow)
 - #155778 (Avoid Vec allocation in TyCtxt::mk_place_elem)
 - #151014 (std: sys: process: uefi: Add program searching)
 - #155682 (Add boxing suggestions for `impl Trait` return type mismatches)
 - #155770 (Avoid misleading closure return type note)
 - #155818 (Convert attribute `FinalizeFn` to fn pointer)
 - #155829 (rustc_attr_parsing: use a `try {}` in `or_malformed`)
 - #155835 (couple of `crate_name` cleanups)
@rust-bors rust-bors Bot merged commit c20a92e into rust-lang:main Apr 27, 2026
12 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants