6 ms·
Manifesto: Adding memory ownership to Swift
- telotortium 10y agoA summary (from the Rust subreddit https://www.reddit.com/r/rust/comments/5unav5/ https://www.reddit.com/r/rust/comments/5unav5/): https://gist.github.com/Gankro/1f79fbf2a9776302a9d4c8c0097cc40e https://gist.github.com/Gankro/1f79fbf2a9776302a9d4c8c0097cc....
- Gankro 10y agoHit me up with any questions y'all have.
- deleted 10y ago[deleted]
- solidsnack9000 10y agoThe Swift ownership system doesn't address concurrency because a mutable reference can be passed to a function, stored somewhere, and then passed to another function?
- Gankro 10y agoThe major holes are: class fields, globals, and captures of escaping (non-scoped) closures (which are basically class fields). Although this system proposes dynamic checking for these cases, it's explicitly not atomic checking. Atomic checking would be very expensive even though most code doesn't need it. Also there's a general sense that java, which does atomically guard all shared mutable state, has the problem that these accesses are defined but ultimately useless. Part of this is that coherent updates require coarser locks.
- solidsnack9000 10y agoMaybe for objects it makes more sense to be able to "own" the object as a whole, if I am understanding you right. Still reading the Swift proposal.
- jsolson 10y agoI'm at work and went diving into the doc looking for one specific thing. I found it, but it turns out to be a put unfortunate. https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md#deinit-for-non-copyable-types https://github.com/apple/swift/blob/master/docs/OwnershipMan... > Swift is permitted to destroy a value (and thus call deinit) at any time between its last use and its formal point of destruction. The exact definition of "use" for the purposes of this definition is not yet fully decided. For things like scoped lockables the last "use" must be well defined with respect to the termination of the scope. Early deinit means you're still unable to build something like C++'s std::lock_guard (http://en.cppreference.com/w/cpp/thread/lock_guard http://en.cppreference.com/w/cpp/thread/lock_guard). Consider: // dwizzles can be fizzled up to three times, but no more. class dwizzle { public: dwizzle() : count_(0) {} void fizzle() { std::lock_guard lock(&mutex_); if (count_ >= 3) return; ++count_; } private: std::mutex mutex_; int count_ = 0; }; Obviously a silly toy, but the point is that we acquire the lock, we must hold it until the end of the scope for correctness, and we want to ensure it's dropped on early returns. I hope any formal proposal coming out of this has a sufficiently strict definition of "use" (or provides opt-in tools for building structs that restrict the definition appropriately) to allow such constructions. It's much nicer than having to always type something like: mutex.lock() defer { mutex.unlock() } (apologies for any inconsistency in my C++ above -- the codebase I routinely work in has its own mutex type and an equivalent scoped lockable -- I tried to adopt the more standard version for universality's sake).
- Gankro 10y agoThe argument for Swift's behaviour is basically: very few things actually need this guarantee, and it's bad for performance (or memory usage) for all the other types to inherit it. Personally, as someone who has spent a lot of time trying to manage the safe/unsafe code boundary, I'm inclined to agree with you. Unsafe code in Swift has an even scarrier subtle hazard: if the last uses of a value are through an unmanaged handle, the compiler won't know and can free the managed value before those uses. You need to insert a marker saying "please keep this alive". Even worse: returning a value, after inlining, isn't sufficient to establish a use! That said, your lock handle example doesn't matter in swift today: you really want a lock handle to be a struct, and structs can't have destructors. You also want it to have move semantics, which don't exist yet. This proposal suggests moveonly values that could adopt destructors (like Rust), and if you're already adding an annotation for destructors, it's not out of the question to add another one asking for "true" lexical destructors.
- Tanegashima 10y agoSwift doesn't have memory ownership because your supposed to use DispatchQueues for that. The end. People asking for this are the people not familiar with Apple's SDK. And btw, DispatchQueues are also available in Linux, GCD has been ported to Linux since the beginning, but neckbeards have been trying to avoid it at all costs because it's Apple, and now they are complaining because Swift has been taking the spotlight from Rust.
- mememachine 10y agoNeckbeards?
- solidsnack9000 10y agoMany of us who like Rust also like Swift, and there are many Swift team members who have contributed to Rust. What I do wonder about it is Swift's plan for things like tiny binaries (no runtime), no standard library, zero-cost (or predictable cost) abstractions...
- Tanegashima 10y agoSwift is an Applications programming language first, digging its way down to the system. Rust on the other hand tries to be a system programming language, but with no success at that. And it's not a popular Applications programming language because there's no support from any major software platform vendor. Swift doesn't have a runtime, a linked library isn't a runtime, if you think it is, then you don't have the qualifications to discuss this. It is not a problem to store one 50meg file on my 32GB iPhone or 128GB server, it would be in a 32KB microcontroller, but nobody is trying to ship Swift as a language for those.
- solidsnack9000 10y ago> Swift doesn't have a runtime, a linked library isn't a runtime, if you think it is, then you don't have the qualifications to discuss this. My understanding is that it's called "the Objective-C runtime". I don't need any qualifications to call it that! I'm not sure what distinction you're trying to draw.