6 ms·
Right. This is one of the things I don't like about automatic reference counting, that it's almost a complete solution, but cycles require more thought (but les
by giornogiovanna 8y ago
Right. This is one of the things I don't like about automatic reference counting, that it's almost a complete solution, but cycles require more thought (but less cases) to deal with than manual memory management itself.
- mamcx 8y agoBut how many times the problem truly happened? Is in my experience so rare that the experience is alike with a GC.
- giornogiovanna 8y ago- Doubly linked lists. - Cocoa delegates that keep references to some parent of the objects that they're delegates of. - Some event listeners (in the same way as delegates). I wouldn't say it's "rare". It popped up pretty often for me when I was writing Objective C.
- azinman2 8y agoWhat’s not solved with weak refs?
- giornogiovanna 8y agoIt can always be solved with weak references.
- fauigerzigerk 8y agoOnly if you know that you haven't solved it already: https://news.ycombinator.com/item?id=19093677 https://news.ycombinator.com/item?id=19093677
- ben-schaaf 8y agoWhat's not solved with manual memory management? The point is not that you can't solve it, the point is that reference counting isn't the whole solution for garbage collection unlike a tracing gc for instance.
- saagarjha 8y agoWeak reference counting is still reference counting.
- int_19h 8y agoIt depends on your domain. Any data structure that has links in both directions becomes a pain, and it's pretty common to have trees like that. It's usually solved by having parents own the children but not vice versa, but then you run into cases where you want the whole thing to live so long as someone holds an owning reference to a child.
- mamcx 8y agoAgree. Is similar on rust, where circular links are also hard to do. Easy graphs is a killer feature of a GC. But also, is a localized problem. I have more issues with obj-c in the past, some ergonomic of swift make it even rarer. However, certainly suck for the ones that hit this!. I'm building a relational lang in rust and damm, I miss a GC!
- zozbot123 8y agoMany people in the Rust community use some sort of ECS in cases where something like a GC is needed. The whole point of ECS is that they work a lot like an ordinary GC runtime or an in-memory database, the underlying representation ensures that the system knows about all the "entities" that it needs to, and can reliably trace linkages between them. It might be easier to just use Go in cases where tracing GC is needed, though.
- Buttons840 8y agoI've only heard of using ECS for games. I'll have to look into them more closely.
- azinman2 8y agoWhen would you want the graph to exist due to some leaf more being retained but you wouldn’t know about it enough to maintain a ref to the root mode?
- int_19h 8y agoIn a language with tracing GC, you'd use a strong reference in both directions, so anybody who holds the child can always walk their way to the root (if you expose those references). Without such GC, you basically have to pass the root around, because it's what's holding all those children alive.
- pjmlp 8y agoAlike a tracing GC..,.
- dfgdghdf 8y agoIt would be interesting to compare the cognitive load of the Swift way and the Rust way. Rust gives you better guarantees, but you must annotate your code.
- giornogiovanna 8y agoRust doesn't just give you better guarantees. It gives you no runtime checking, and potentially even better performance than naive C, because it can automatically tell LLVM when there is no memory aliasing going on.[0] Also, it gives you more confidence, because you can, for example, give out an array to someone and be sure that it won't be modified. And usually that kind of confidence is enough to write safe, race-free parallel code. I don't think Swift's runtime exclusivity checks give you any of that. [0]: https://github.com/rust-lang/rust/pull/50744 https://github.com/rust-lang/rust/pull/50744
- glhaynes 8y agoSwift arrays have pass-by-value semantics (under the cover, they use copy-on-write to keep performance good) and are thus safe to pass to functions without concern that they'll be modified. (Unless it's passed as an `inout` parameter, which requires a prepended "&" at the call site.)
- zozbot123 8y agoRust can use its own bovine superpowers here https://doc.rust-lang.org/std/borrow/enum.Cow.html https://doc.rust-lang.org/std/borrow/enum.Cow.html to enable developers to write code that's invariant over whether it's dealing with a fresh copy of something, or just a borrowed reference. So, it's not quite correct to say that Swift's use of copy-on-write gives it better performance. Rust can express the exact same pattern quite easily, and do it with far more generality.
- glhaynes 8y agoI don't mean to imply that Swift's array performance is better than anything other than what its own performance would be if it didn't implement COW. (My intention was to convey that the normal way of passing arrays comes with confidence that your copy won't be modified by the callee.) Rust sounds nice, I'll have to try it sometime.
- favorited 8y agoYou can add a cycle collector on top of a reference-counted language. It's been discussed for Swift, but there are downsides which might make it not worth adding.