4 ms·
Does Swift force GC (via AutoRC)? That might lower its utility as a systems language and should have a performance penalty that Rust does not.
by MichaelGG 10y ago
Does Swift force GC (via AutoRC)? That might lower its utility as a systems language and should have a performance penalty that Rust does not.
- pcwalton 10y agoThat's right. Swift is a garbage collected language.
- mikeash 10y agoYou can avoid it, by using things like UnsafePointer for your references, but it's unnatural and inconvenient.
- bjz_ 10y agoYes, unless you break out of it by using unsafe storage methods, which are purposefully made unergonomic to use. Due to its type system Rust has the advantage that you can safely choose by either thread-local or atomic reference counting - Swift has to default to the latter. And you actually don't really need RC all that much - most data in a program is short-lived and has a single owner. The flip-side is that because Swift's reference counting is implicit, it makes the type signatures simpler, and working with them more ergonomic.
- millstone 10y agoBoth Swift and Rust use reference counting in different ways. Swift uses reference counting for all reference types (classes), but idiomatic Swift encourages using value types over reference types when possible. The chief reason to use a reference type is when you need shared ownership. Value types are not reference counted, though they may contain RC'd fields. Rust has reference counting implemented in a library (Rc and Arc), which is what Rust programmers reach for when they need shared ownership. Rust also has a 1-bit reference count called the drop flag, which is used to dynamically determine when an object's destructor should run.
- pcwalton 10y agoThe drop flag is not a reference count. It is an implementation detail, and we could eliminate it if we wanted to. Swift's value types are less flexible than those of Rust because of the need to maintain soundness in the presence of aliasing and mutation.