Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove alignment-changing in-place collect #120116

Merged
merged 2 commits into from
Jan 20, 2024

Conversation

the8472
Copy link
Member

@the8472 the8472 commented Jan 18, 2024

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
Collaborator

rustbot commented Jan 18, 2024

r? @thomcc

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

@rustbot rustbot 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
Member

cuviper commented Jan 19, 2024

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

@rustbot
Copy link
Collaborator

rustbot commented Jan 19, 2024

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
Copy link
Contributor

bors commented Jan 19, 2024

📌 Commit 85d1787 has been approved by cuviper

It is now in the queue for this repository.

@bors bors 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
@cuviper cuviper 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.
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:master Jan 20, 2024
11 checks passed
@rustbot rustbot 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
Member

cuviper commented Jan 25, 2024

@rustbot rustbot added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jan 25, 2024
@cuviper cuviper mentioned this pull request Jan 25, 2024
@cuviper cuviper modified the milestones: 1.77.0, 1.76.0 Jan 25, 2024
@cuviper cuviper 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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant 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