Skip to content

Remove alignment-changing in-place collect - #120116

Merged
bors merged 2 commits into
rust-lang:masterfrom
the8472:only-same-alignments
Jan 20, 2024
Merged

Remove alignment-changing in-place collect#120116
bors merged 2 commits into
rust-lang:masterfrom
the8472:only-same-alignments

Conversation

@the8472

Copy link
Copy Markdown
Member

This removes the alignment-changing in-place collect optimization introduced in #110353
Currently stable users can't benefit from the optimization because GlobaAlloc doesn't support alignment-changing realloc and neither do most posix allocators. So in practice it has a negative impact on performance.

Explanation from #120091 (comment):

You mention that in case of alignment mismatch -- when the new alignment is less than the old -- the implementation calls mremap.

I was trying to note that this isn't really the case in practice, due to the semantics of Rust's allocator APIs. The only use of the allocator within the in_place_collect implementation itself is a call to Allocator::shrink(), which per its documentation allows decreasing the required alignment. However, in stable Rust, the only available Allocator is Global, which delegates to the registered GlobalAlloc. Since GlobalAlloc::realloc()cannot change the required alignment, the implementation of <Global as Allocator>::shrink() must fall back to creating a brand-new allocation, memcpying the data into it, and freeing the old allocation, whenever the alignment doesn't remain exactly the same.

Therefore, the underlying allocator, provided by libc or some other source, has no opportunity to internally mremap() the data when the alignment is changed, since it has no way of knowing that the allocation is the same.

Currently stable users can't benefit from this because GlobaAlloc doesn't support
alignment-changing realloc and neither do most posix allocators.
So in practice it always results in an extra memcpy.
@rustbot

Copy link
Copy Markdown
Collaborator

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 18, 2024
@cuviper

Copy link
Copy Markdown
Member

@bors r+
@rustbot label +beta-nominated (for #120091)

@rustbot

Copy link
Copy Markdown
Collaborator

Error: Parsing relabel command in comment failed: ...'-nominated' | error: a label delta at >| ' (for #120'...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@bors

bors commented Jan 19, 2024

Copy link
Copy Markdown
Collaborator

📌 Commit 85d1787 has been approved by cuviper

It is now in the queue for this repository.

@borsbors 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 Jan 19, 2024
@cuvipercuviper added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jan 19, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jan 20, 2024
…viper
Remove alignment-changing in-place collect
This removes the alignment-changing in-place collect optimization introduced in rust-lang#110353
Currently stable users can't benefit from the optimization because GlobaAlloc doesn't support alignment-changing realloc and neither do most posix allocators. So in practice it has a negative impact on performance.
Explanation from rust-lang#120091 (comment):
> > You mention that in case of alignment mismatch -- when the new alignment is less than the old -- the implementation calls `mremap`.
>
> I was trying to note that this isn't really the case in practice, due to the semantics of Rust's allocator APIs. The only use of the allocator within the `in_place_collect` implementation itself is [a call to `Allocator::shrink()`](https://github.com/rust-lang/rust/blob/db7125f008cfd72e8951c9a863178956e2cbacc3/library/alloc/src/vec/in_place_collect.rs#L299-L303), which per its documentation [allows decreasing the required alignment](https://doc.rust-lang.org/1.75.0/core/alloc/trait.Allocator.html). However, in stable Rust, the only available `Allocator` is [`Global`](https://doc.rust-lang.org/1.75.0/alloc/alloc/struct.Global.html), which delegates to the registered `GlobalAlloc`. Since `GlobalAlloc::realloc()` [cannot change the required alignment](https://doc.rust-lang.org/1.75.0/core/alloc/trait.GlobalAlloc.html#method.realloc), the implementation of [`<Global as Allocator>::shrink()`](https://github.com/rust-lang/rust/blob/db7125f008cfd72e8951c9a863178956e2cbacc3/library/alloc/src/alloc.rs#L280-L321) must fall back to creating a brand-new allocation, `memcpy`ing the data into it, and freeing the old allocation, whenever the alignment doesn't remain exactly the same.
>
> Therefore, the underlying allocator, provided by libc or some other source, has no opportunity to internally `mremap()` the data when the alignment is changed, since it has no way of knowing that the allocation is the same.
This was referenced Jan 20, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 20, 2024
…llaumeGomez
Rollup of 6 pull requests
Successful merges:
- rust-lang#119997 (Fix impl stripped in rustdoc HTML whereas it should not be in case the impl is implemented on a type alias)
- rust-lang#120000 (Ensure `callee_id`s are body owners)
- rust-lang#120063 (Remove special handling of `box` expressions from parser)
- rust-lang#120116 (Remove alignment-changing in-place collect)
- rust-lang#120138 (Increase vscode settings.json `git.detectSubmodulesLimit`)
- rust-lang#120169 (Spelling fix)
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit b917753 into rust-lang:masterJan 20, 2024
@rustbotrustbot added this to the 1.77.0 milestone Jan 20, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 20, 2024
Rollup merge of rust-lang#120116 - the8472:only-same-alignments, r=cuviper
Remove alignment-changing in-place collect
This removes the alignment-changing in-place collect optimization introduced in rust-lang#110353
Currently stable users can't benefit from the optimization because GlobaAlloc doesn't support alignment-changing realloc and neither do most posix allocators. So in practice it has a negative impact on performance.
Explanation from rust-lang#120091 (comment):
> > You mention that in case of alignment mismatch -- when the new alignment is less than the old -- the implementation calls `mremap`.
>
> I was trying to note that this isn't really the case in practice, due to the semantics of Rust's allocator APIs. The only use of the allocator within the `in_place_collect` implementation itself is [a call to `Allocator::shrink()`](https://github.com/rust-lang/rust/blob/db7125f008cfd72e8951c9a863178956e2cbacc3/library/alloc/src/vec/in_place_collect.rs#L299-L303), which per its documentation [allows decreasing the required alignment](https://doc.rust-lang.org/1.75.0/core/alloc/trait.Allocator.html). However, in stable Rust, the only available `Allocator` is [`Global`](https://doc.rust-lang.org/1.75.0/alloc/alloc/struct.Global.html), which delegates to the registered `GlobalAlloc`. Since `GlobalAlloc::realloc()` [cannot change the required alignment](https://doc.rust-lang.org/1.75.0/core/alloc/trait.GlobalAlloc.html#method.realloc), the implementation of [`<Global as Allocator>::shrink()`](https://github.com/rust-lang/rust/blob/db7125f008cfd72e8951c9a863178956e2cbacc3/library/alloc/src/alloc.rs#L280-L321) must fall back to creating a brand-new allocation, `memcpy`ing the data into it, and freeing the old allocation, whenever the alignment doesn't remain exactly the same.
>
> Therefore, the underlying allocator, provided by libc or some other source, has no opportunity to internally `mremap()` the data when the alignment is changed, since it has no way of knowing that the allocation is the same.
@cuviper

Copy link
Copy Markdown
Member

@rustbotrustbot added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jan 25, 2024
@cuvipercuviper mentioned this pull request Jan 25, 2024
@cuvipercuviper modified the milestones: 1.77.0, 1.76.0Jan 25, 2024
@cuvipercuviper removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jan 25, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 27, 2024
[beta] backports
- Remove alignment-changing in-place collect rust-lang#120116
- fix: Drop guard was deallocating with the incorrect size rust-lang#120145
r? ghost
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beta-acceptedAccepted for backporting to the compiler in the beta channel.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libsRelevant to the library 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

@the8472@rustbot@cuviper@bors@thomcc