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

Optimize Rc<T>::default #132031

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

slanterns
Copy link
Contributor

@slanterns slanterns commented Oct 22, 2024

The missing piece of #131460.

Also refactored Arc<T>::default by using a safe NonNull::from(Box::leak(_)) to replace the unnecessarily unsafe call to NonNull::new_unchecked(Box::into_raw(_)). The remaining unsafety is coming from [Rc|Arc]::from_inner, which is safe from the construction of [Rc|Arc]Inner.

@rustbot
Copy link
Collaborator

rustbot commented Oct 22, 2024

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

@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 Oct 22, 2024
@slanterns
Copy link
Contributor Author

@rustbot author

@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Oct 22, 2024
@rust-log-analyzer

This comment has been minimized.

@slanterns slanterns marked this pull request as ready for review October 22, 2024 08:53
@rust-log-analyzer

This comment has been minimized.

@slanterns
Copy link
Contributor Author

@rc_default_inplace = unnamed_addr alias ptr (), ptr @arc_default_inplace

Whoa

@slanterns
Copy link
Contributor Author

CI green. @rustbot ready
r? @ibraheemdev since you reviewed the previous PR :)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 22, 2024
@ibraheemdev
Copy link
Member

cc @rust-lang/opsem regarding NonNull::from(Box::leak(..)), I'm not sure we want to introduce this pattern into std (rust-lang/unsafe-code-guidelines#316)

I'm also happy to approve this without the Box::leak change if you wish.

@slanterns
Copy link
Contributor Author

slanterns commented Oct 22, 2024

The current implementation of Rc::new is already using such pattern:

pub fn new(value: T) -> Rc<T> {
// There is an implicit weak pointer owned by all the strong
// pointers, which ensures that the weak destructor never frees
// the allocation while the strong destructor is running, even
// if the weak pointer is stored inside the strong one.
unsafe {
Self::from_inner(
Box::leak(Box::new(RcInner { strong: Cell::new(1), weak: Cell::new(1), value }))
.into(),
)
}
}

And it's also what was suggested in #47336 (comment).

@ibraheemdev
Copy link
Member

Alright, this seems reasonable then. Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Oct 22, 2024

📌 Commit 0a963ab has been approved by ibraheemdev

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 Oct 22, 2024
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.

6 participants