-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for Arc/Rc extras #28356
Comments
@alexcrichton I think there may be a good use for If you try and See the discussion here: viperscape/rust-promise#3 |
The more I think about this, I think the more your point about |
@timcharper yeah in general the methods on |
I rewrote my usecase for |
If this gets stabilized, these APIs should get added to the respective Weak as well I'm thinking |
What is planned for arc_counts? I would like to use it because I need to do ffi call to c library when the last Arc pointer is dropped but don't wanna start using it if it might disappear soon.. Maybe a different approach would be to do the call when the inner structure is being dropped, that would definitely be when the last pointer is dropped because is has only one owner.. |
May I ask: What is the point of |
@ticki I don't understand the question. Rc/Weak are dual types that have different interpretations of the same memory. Individual Rc/Weaks do not store a count. Rather, they all share RcBoxes to coordinate these counts. |
Yeah, that's right. They're just pointers to the actual data plus a weak and a strong reference count. What I ask is: why do they keep track of the weak reference count? For example, C++ and Obj-C do not count the weak references. |
If I were to venture a guess, strong reference counts are used to know when the ARC contained data should be freed, weak reference counts are used to know when the ARC container should be freed. I'm surprised to learn that C++ / Obj-C don't do this (at least, under the hood). What data structure does the weak reference query to learn if the memory has been freed? |
In fact, I was wrong about C++ shared_ptr:
(emphasis mine) |
Obj-C strategy is explained here: https://mikeash.com/pyblog/friday-qa-2010-07-16-zeroing-weak-references-in-objective-c.html Basically: every class instance has a set of "locations of weak pointers". When its strong count hits 0, it traverses the set, nulls out those pointers, and then frees all of its allocations. This can't be implemented in Rust, because it requires weak pointers to have significant locations in memory. Would necessitate overloading move (never gonna happen), or runtime support (unlikely to happen). That said, @pnkfelix has been working on the notion of stack introspection hooks for GC's, which could maybe accommodate this use-case..? Not something we would retrofit Arc/Rc to use, but something someone could do with their own custom types. |
Does it look like either |
Another use case for The |
If another reader sees this and like me thinks "won't the need to handle cycles provide a separate place to represent this information?" then you should read the linked issue. (Quick answer: maybe yes, but it sounds like it doesn't currently handle cycles well anyway, and the comment thread has some interesting alternative ideas beyond a separate map of seen addresses.) |
@pnkfelix indeed. One of the alternative idea is to return 0 if the strong reference count is more than 1, and that would also need stabilizing one of the methods covered in this issue. |
This crate has always given a lower bound of the actual heap memory usage: we use `#[ignore_heap_size_of]` when not doing it is not practical. This fix #37 with one of the ideas given in the discussion there: return an accurate result when the reference count is 1 (and ownership is in fact not shared), or zero otherwise. However, since APIs to access reference counts are not `#[stable]` yet (see rust-lang/rust#28356), so always return zero when unstable APIs are not enabled.
These may be useful for debugging. I don't have a concrete use case at the moment. Somehow I've managed to avoid shared ownership thus far. |
@camlorn Cycle detection in debug mode could be awesome actually. |
Agreed. I thought of it as well. But this didn't seem like the place to suggest it and in truth that sounds like it would probably be very complicated and RFC-ish anyway. |
Actually, cycle detection in could be useful for far more than just debugging: one might be able to use it to implement a cycle collector a la Python that not only detected cycles, but freed the ones that were no longer reachable. |
@DemiMarie You'd also need special compiler machinery for tracking the accessibility of stuff from the stack, and at that point we might as well just add a full GC. |
The remaining unstable methods covered here are:
They have been
To counter-balance that:
Given all this, I’d like to propose FCP to stabilize all four |
@rfcbot fcp merge Thanks for the summary @SimonSapin! Sounds reasonable to me to stabilize the count methods and deprecate the others. We've already committed to reference counting and weak pointers for both |
Let's at least strongly emphasize the concurrency hazards in the |
@glaebhoerl I’ve opened #37652, what do you think? |
LGTM :) |
…chton More proeminent warning in Arc::{strong,weak}_count docs. CC rust-lang#28356 (comment)
Team member @alexcrichton has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 psst @alexcrichton, I wasn't able to add the |
@alexcrichton I think you need to tell rfc bot that you applied the label? |
@BurntSushi hm I think rfcbot may be missing out on FCP endings... In any case we'll want to leave this until the near the end of the cycle regardless. |
The final comment period is now complete. |
Library stabilizations/deprecations for 1.15 release Stabilized: - `std::iter::Iterator::{min_by, max_by}` - `std::os::*::fs::FileExt` - `std::sync::atomic::Atomic*::{get_mut, into_inner}` - `std::vec::IntoIter::{as_slice, as_mut_slice}` - `std::sync::mpsc::Receiver::try_iter` - `std::os::unix::process::CommandExt::before_exec` - `std::rc::Rc::{strong_count, weak_count}` - `std::sync::Arc::{strong_count, weak_count}` - `std::char::{encode_utf8, encode_utf16}` - `std::cell::Ref::clone` - `std::io::Take::into_inner` Deprecated: - `std::rc::Rc::{would_unwrap, is_unique}` - `std::cell::RefCell::borrow_state` Closes #23755 Closes #27733 Closes #27746 Closes #27784 Closes #28356 Closes #31398 Closes #34931 Closes #35601 Closes #35603 Closes #35918 Closes #36105
This is a tracking issue for the
arc_counts
,rc_counts
, andrc_would_unwrap
features. The counting functions have been around for awhile butwould_unwrap
was added recently.cc #27718, more discussion there.
The text was updated successfully, but these errors were encountered: