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

Do not allocate for ZST ThinBox (attempt 2 using const_allocate) #123254

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

stepancheg
Copy link
Contributor

There's PR #123184 which avoids allocation for ZST ThinBox.

That PR has an issue with unsoundness with padding in MaybeUninit (see comments in that PR). Also that PR relies on Freeze trait.

This PR is much simpler implementation which does not have this problem, but it uses const_allocate feature.

@oli-obk suggested that const_allocate should not be used for that feature. But I like how easy it to do this feature with const_allocate. Maybe it's OK to use const_allocate while ThinBox is unstable? Or, well, we can abandon this PR.

r? @oli-obk

@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 Mar 31, 2024
@workingjubilee
Copy link
Member

I would strongly prefer that we not have an implementation that is only justifiable by the surface being unstable as well. It's one thing to have it be somewhat experimental.

@workingjubilee
Copy link
Member

...on second thought, I took a look at the other PR and it seems to be so radioactive its like I saw the Elephant's Foot. If you can sell oli on this one, then carry on.

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Mar 31, 2024

Maybe it's OK to use const_allocate while ThinBox is unstable? Or, well, we can abandon this PR.

Only if you make sure that ThinBox will not be stabilized in this form, and promise that you are okay with removing this optimization again without hesitation if we change const_allocate in ways that make it not work any more.

It is nice to see experimentation with const_allocate and that it can be useful even in its current form. But I want to be sure we are not locked-in to its current form either.

@stepancheg
Copy link
Contributor Author

@RalfJung

Only if you make sure that ThinBox will not be stabilized in this form, and promise that you are okay with removing this optimization again without hesitation if we change const_allocate in ways that make it not work any more.

If it comes to stabilization, I'd prefer to not stabilize ThinBox without this optimization: it should either work optimally, or not work at all.

But my opinion has little weight.

It is nice to see experimentation with const_allocate and that it can be useful even in its current form.

Yes, I think this is useful as

  • illustration of this feature
  • a test for the feature

library/alloc/src/boxed/thin.rs Outdated Show resolved Hide resolved
library/alloc/src/boxed/thin.rs Outdated Show resolved Hide resolved
library/alloc/src/boxed/thin.rs Outdated Show resolved Hide resolved
library/alloc/src/boxed/thin.rs Outdated Show resolved Hide resolved
@oli-obk
Copy link
Contributor

oli-obk commented Apr 2, 2024

yea, this seems fine to me.

Things to do in this PR

  • The logic should live in a WithHeader method though, as this breaks abstraction boundaries.
  • Add miri tests
  • add a blocker to the thinbox tracking issue to have made enough progress on const_allocate to justify its correct handling by the rest of the compiler and the fact that we'll always have it or an equivalent to it.

Things to do in follow-up PRs

  • move WithHeader into its own module to keep all the finicky logic in one place.

There's PR rust-lang#123184
which avoids allocation for ZST ThinBox.

That PR has an issue with unsoundness with misuse of `MaybeUninit`
(see comments in that PR).

This PR is much simpler implementation which does not have this
problem, but it uses `const_allocate` feature.
@stepancheg
Copy link
Contributor Author

The logic should live in a WithHeader method though, as this breaks abstraction boundaries.

Done.

Add miri tests

Give pointers where how to do that please?

add a blocker to the thinbox tracking issue

Is that something I can do?

move WithHeader into its own module to keep all the finicky logic in one place

Like submodule mod with_header? Or a separate file next to thin_box.rs?

@oli-obk
Copy link
Contributor

oli-obk commented Apr 9, 2024

Is that something I can do?

I added it

Like submodule mod with_header? Or a separate file next to thin_box.rs?

if a submodule works out nicely with visibility, that's prefereable imo

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 9, 2024

📌 Commit f539134 has been approved by oli-obk

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 Apr 9, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 9, 2024
…llaumeGomez

Rollup of 8 pull requests

Successful merges:

 - rust-lang#123254 (Do not allocate for ZST ThinBox (attempt 2 using const_allocate))
 - rust-lang#123626 (Add MC/DC support to coverage test tools)
 - rust-lang#123638 (rustdoc: synthetic auto: filter out clauses from the implementor's ParamEnv)
 - rust-lang#123653 (Split `non_local_definitions` lint tests in separate test files)
 - rust-lang#123658 (Stop making any assumption about the projections applied to the upvars in the `ByMoveBody` pass)
 - rust-lang#123662 (Don't rely on upvars being assigned just because coroutine-closure kind is assigned)
 - rust-lang#123665 (Fix typo in `Future::poll()` docs)
 - rust-lang#123672 (compiletest: unset `RUSTC_LOG_COLOR`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1c5db28 into rust-lang:master Apr 9, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 9, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 9, 2024
Rollup merge of rust-lang#123254 - stepancheg:thin-box-0-const-alloc, r=oli-obk

Do not allocate for ZST ThinBox (attempt 2 using const_allocate)

There's PR rust-lang#123184 which avoids allocation for ZST ThinBox.

That PR has an issue with unsoundness with padding in `MaybeUninit` (see comments in that PR). Also that PR relies on `Freeze` trait.

This PR is much simpler implementation which does not have this problem, but it uses `const_allocate` feature.

`@oli-obk` suggested that `const_allocate` should not be used for that feature. But I like how easy it to do this feature with `const_allocate`. Maybe it's OK to use `const_allocate` while `ThinBox` is unstable? Or, well, we can abandon this PR.

r? `@oli-obk`
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-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.

8 participants