News for hackers. Stuff that matters.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by theLiminator on Wednesday January 11, 2023 @02:19PM (#34343142)
Afaik Swift is pretty close to using Rc and Arc everywhere. Does swift do some more escape analysis than Rust?
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by janef0421 on Wednesday January 11, 2023 @02:54PM (#34343695)
Swift only uses reference counting when working with objects; Structs are optimised using copy-on-write. There's currently work on implementing move and ownership semantics, similar to Rust, but opt-in rather than by default.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by meindnoch on Wednesday January 11, 2023 @06:50PM (#34346841)
Ummm… No.

Swift structs are just like C structs (from a memory perspective). The copy-on-write thing is implemented manually by storing a private refcounted object in your struct. See the implementation of Array for example: https://github.com/apple/swift/blob/main/stdlib/public/core/...

There’s no magical copy-on-write mechanism at the language level.

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by jshier on Thursday January 12, 2023 @01:53PM (#34358570)
I don't know why this is downvoted, it's true. There is no automatic copy on write optimization in Swift. It's a manual optimization that expensive types like Array implement manually.
Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by meindnoch on Thursday January 12, 2023 @03:20PM (#34359739)
>I don't know why this is downvoted, it's true.

I know, right? Some people are pretty insecure…

Re:Apple Announces Full Swift Rewrite of the Foundation Fram... by manmal on Wednesday January 11, 2023 @02:58PM (#34343754)
I haven’t read that much on Rust, but that should be pretty much it.

Swift has an emphasis on value types, which often are stored on the stack, but only up to a certain size. Copy-on-write makes this feasible - often, those value types are passed by reference until a write actually occurs, but that’s opaque to the dev. Value types can have pointers to reference-counted types - if a value type is passed/copied, any pointer it owns is retained (weirdly enough, they claim that value types have no „lifetime“, but at some point those refs have to be released - we just have to trust the compiler in this).