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

Add alloc::rc::UniqueRc #111849

Merged
merged 1 commit into from
Jun 20, 2023
Merged

Add alloc::rc::UniqueRc #111849

merged 1 commit into from
Jun 20, 2023

Conversation

eholk
Copy link
Contributor

@eholk eholk commented May 22, 2023

This PR implements UniqueRc as described in rust-lang/libs-team#90.

I've tried to stick to the API proposed there, incorporating the feedback from the ACP review. For now I've just implemented UniqueRc, but we'll want UniqueArc as well. I wanted to get feedback on this implementation first since the UniqueArc version should be mostly a copy/paste/rename job.

@rustbot
Copy link
Collaborator

rustbot commented May 22, 2023

r? @Mark-Simulacrum

(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 May 22, 2023
@rust-log-analyzer

This comment has been minimized.

@eholk
Copy link
Contributor Author

eholk commented May 22, 2023

@rustbot author

There are a few issues that need fixed before this is ready for review.

@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 May 22, 2023
@rust-log-analyzer

This comment has been minimized.

@programmerjake
Copy link
Member

to me, something named UniqueRc<T> should contain a T and implement DerefMut<Target = T> and basically be Rc but with the restriction that strong_count == 1 and Weak::upgrade doesn't yet work...

what you have here seems more like a UninitalizedRc or EmptyRc where it allocated space for a RcBox but didn't fill it in yet.

@joboet
Copy link
Member

joboet commented May 23, 2023

to me, something named UniqueRc<T> should contain a T and implement DerefMut<Target = T> and basically be Rc but with the restriction that strong_count == 1 and Weak::upgrade doesn't yet work...

what you have here seems more like a UninitalizedRc or EmptyRc where it allocated space for a RcBox but didn't fill it in yet.

With the UniqueRc<T> you propose, EmptyRc could be easily emulated by using UniqueRc<MaybeUninit<T>> and adding a write method to initialize it.

@m-ou-se
Copy link
Member

m-ou-se commented May 23, 2023

to me, something named UniqueRc<T> should contain a T and implement DerefMut<Target = T> and basically be Rc but with the restriction that strong_count == 1 and Weak::upgrade doesn't yet work...

That's indeed what I meant in rust-lang/libs-team#90 (comment)

@eholk
Copy link
Contributor Author

eholk commented May 23, 2023

Ah, I see what you mean. I misinterpreted the original suggestion.

UniqueRc didn't seem like the right name for what I've implemented. While I was writing the documentation, something like PlaceholderRc<T> felt like a better name.

So with UniqueRc, creating a cyclic structure would look more like this, right?

struct Cyclic {
    this: Weak<Cyclic>,
}

let mut cycle = UniqueRc::new(Cyclic { this: Weak::new() };
cycle.this = UniqueRc::weak(cycle);

// convert to a normal `Rc`
let rc = UniqueRc::into_rc(cycle);

I'm curious why the Libs API team prefers this approach? I personally find something like PlaceholderRc or UninitializedRc more ergonomic, but I admit this is a matter of preference and I am likely overlooking other concerns around consistency with the rest of std, etc.

One nice property I see for PlaceholderRc is that it's pretty straightforward to implement Rc::new_cyclic in terms of PlaceholderRc. It's less obvious how to do this with UniqueRc, although @joboet's trick of using UniqueRc<MaybeUninit<T>> should work.

I appreciate the discussion here! One thing this has pointed out to me is that it's actually possible to implement this without depending on any internal details of Rc, so it could be done in an external crate (although it would require unsafe code, of course). In our usage of this, we had basically written our own RcBox and transmuted the contents of the Rc into that, which worked but was incredibly fragile. Given the realization we can do this without depending on internal details, I wonder if it makes more sense to try this as an external crate first in the interest of not growing std more than we need to?

@programmerjake
Copy link
Member

for UniqueRc at least, it does require modifying Rc's internals in order to prevent Weak::upgrade from working until the UniqueRc has been converted into an Rc, otherwise you could have both shared and mutable references to T which is UB.

@eholk
Copy link
Contributor Author

eholk commented May 24, 2023

for UniqueRc at least, it does require modifying Rc's internals in order to prevent Weak::upgrade from working until the UniqueRc has been converted into an Rc

Oh, good point. Thanks for pointing that out!

@eholk
Copy link
Contributor Author

eholk commented May 24, 2023

I updated the PR to implement UniqueRc<T> as a thing that always holds an initialized T and provides a DerefMut impl so you can freely mutate its contents before converting it to an Rc.

I also changed all the functions to not have a self, since the pattern for Rc and such is to avoid adding new methods that could conflict with methods on the underlying T.

Finally, I renamed UniqueRc::weak to UniqueRc::downgrade for better symmetry with Rc.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@safinaskar
Copy link
Contributor

I updated the PR to implement UniqueRc<T> as a thing that always holds an initialized T

I like your previous approach more. It is simpler to use

@programmerjake
Copy link
Member

I updated the PR to implement UniqueRc<T> as a thing that always holds an initialized T

I like your previous approach more. It is simpler to use

imho both APIs are useful, both the EmptyRc (like in Rc::new_cyclic) and UniqueRc that implements DerefMut<Target = T> -- they have different use-cases, so for each use-case one and/or the other or neither is best suited for that use-case.

@eholk
Copy link
Contributor Author

eholk commented Jun 6, 2023

imho both APIs are useful, both the EmptyRc (like in Rc::new_cyclic) and UniqueRc that implements DerefMut<Target = T> -- they have different use-cases, so for each use-case one and/or the other or neither is best suited for that use-case.

EmptyRc seems like a good name for it. The nice thing is the two modes are pretty similar (you can implement EmptyRc as UniqueRc<MaybeUninit<T>>, so it doesn't seem like a big deal to include both.

@eholk
Copy link
Contributor Author

eholk commented Jun 6, 2023

I think this is ready now. It's tempting to add EmptyRc too, but I think it's better to save that for a later PR. This PR focuses on implementing UniqueRc as was suggested by the libs API team.

@rustbot ready

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 6, 2023
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 6, 2023
Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

Implementation looks fine to me. I think the ACP looks like it was positively received, so I'm inclined to say we should file a tracking issue and I can approve this PR -- the naming of the type feels unfortunate as mentioned in my comment, but I wouldn't block this over that.

library/alloc/src/rc.rs Show resolved Hide resolved
@Mark-Simulacrum Mark-Simulacrum 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 Jun 10, 2023
@eholk
Copy link
Contributor Author

eholk commented Jun 12, 2023

I opened #112566 as the tracking issue for this feature.

@eholk
Copy link
Contributor Author

eholk commented Jun 13, 2023

@rustbot ready

@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 Jun 13, 2023
@riking
Copy link

riking commented Jun 14, 2023

Please check API consistency with the Rust-for-Linux UniqueArc https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/sync/arc.rs#L502-L578

@programmerjake
Copy link
Member

Please check API consistency with the Rust-for-Linux UniqueArc https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/sync/arc.rs#L502-L578

imho API consistency with Rc<T> should take priority over Linux's UniqueArc where there's a conflict.

@Mark-Simulacrum
Copy link
Member

@rustbot author

r=me with commits squashed

@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 Jun 16, 2023
@Mark-Simulacrum
Copy link
Member

I think we can revise API as needed in future PRs since this is unstable (e.g., for renaming or for kernel API consistencies).

@eholk
Copy link
Contributor Author

eholk commented Jun 19, 2023

Please check API consistency with the Rust-for-Linux UniqueArc https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/sync/arc.rs#L502-L578

I added this under open questions in the tracking issue (#112566). Thanks for pointing this out to me, it's good to know of another use case for something like this.

This is an `Rc` that is guaranteed to only have one strong reference.
Because it is uniquely owned, it can safely implement `DerefMut`, which
allows programs to have an initialization phase where structures inside
the `Rc` can be mutated.

The `UniqueRc` can then be converted to a regular `Rc`, allowing sharing
and but read-only access.

During the "initialization phase," weak references can be created, but
attempting to upgrade these will fail until the `UniqueRc` has been
converted to a regular `Rc`. This feature can be useful to create
cyclic data structures.

This API is an implementation based on the feedback provided to the ACP
at rust-lang/libs-team#90.
@eholk
Copy link
Contributor Author

eholk commented Jun 19, 2023

@bors r=@Mark-Simulacrum

@bors
Copy link
Contributor

bors commented Jun 19, 2023

📌 Commit 53003cd has been approved by Mark-Simulacrum

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 19, 2023
@bors
Copy link
Contributor

bors commented Jun 20, 2023

⌛ Testing commit 53003cd with merge 14803bd...

@bors
Copy link
Contributor

bors commented Jun 20, 2023

☀️ Test successful - checks-actions
Approved by: Mark-Simulacrum
Pushing 14803bd to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 20, 2023
@bors bors merged commit 14803bd into rust-lang:master Jun 20, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 20, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (14803bd): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.9% [-3.4%, -1.0%] 10
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.9% [-3.4%, -1.0%] 10

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 657.668s -> 655.501s (-0.33%)

/// me: Weak::new(),
/// });
/// rc.me = UniqueRc::downgrade(&rc);
/// Some(UniqueRc::into_rc(rc))
Copy link
Member

Choose a reason for hiding this comment

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

Why does create_gadget return an Option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The example evolved a bit from my first version of it. Originally I wanted to show that you could use this to initialize a circular data structure while the initialization phase is fallible, which is something that the existing new_cyclic function doesn't handle well. Unfortunately, there's no fallibility left in this example so the Option is probably more of a distraction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.