News for hackers. Stuff that matters.
Re:Apple is rewriting Foundation in Swift by KerrAvon on Sunday May 21, 2023 @10:28PM (#36026757)
They both compile slowly because they have similarly complicated type systems. What do you use in Rust that Swift doesn’t yet provide?
Re:Apple is rewriting Foundation in Swift by ozten on Sunday May 21, 2023 @10:34PM (#36026801)
There are a very large list of foundational capabilities so that it’s not practical to respond with a concise answer. One example is automatically closing resources on behalf of the caller thanks to lifetimes. Another example is the best compiler feedback of any mainstream language.
Re:Apple is rewriting Foundation in Swift by xedrac on Sunday May 21, 2023 @10:30PM (#36026771)
Catch data races at compiler time?
Re:Apple is rewriting Foundation in Swift by turdprincess on Sunday May 21, 2023 @10:34PM (#36026805)
I don’t know much about Rust, but Swift can prevent data races at compile time by using the Actor type.
Re:Apple is rewriting Foundation in Swift by astrange on Sunday May 21, 2023 @10:58PM (#36026956)
It could catch them before that using ownership, at the cost of lower performance.

Though Swift concurrency is mostly about "concurrency" not "parallelism", which is the one that has data races.

Re:Apple is rewriting Foundation in Swift by saagarjha on Monday May 22, 2023 @02:07AM (#36027983)
Ownership hasn’t shipped yet.
Re:Apple is rewriting Foundation in Swift by astrange on Monday May 22, 2023 @04:50AM (#36028877)
It has an ABI-level ownership thing already. What's missing is ways to control it.
Re:Apple is rewriting Foundation in Swift by tialaramex on Monday May 22, 2023 @01:58PM (#36034550)
If your concurrency strictly can't involve parallelism then sure, no data races, but also then your performance is miserable on modern hardware.

If your concurrency can invoke parallelism, then you need to explicitly protect against data races, or else you need to define what happens (Java does this, nobody else has) or else you shrug and say too bad now the program is nonsense, you lose (C, C++, Go, many modern languages do this, it is easier)

Re:Apple is rewriting Foundation in Swift by astrange on Tuesday May 23, 2023 @01:27AM (#36040219)
> If your concurrency strictly can't involve parallelism then sure, no data races, but also then your performance is miserable on modern hardware.

Often worth it when that hardware is battery-powered though. Using those extra cores isn't free.

Re:Apple is rewriting Foundation in Swift by wudangmonk on Sunday May 21, 2023 @10:52PM (#36026911)
You can have a good type system and have fast compile times. In every single language with 'complicated type systems' it always comes down to type inference on polymorphic types which causes these issues, this has been known for over 40 years yet the same issue seems to be rediscovered again and again like it was something new.

I sure hope the author learned the right lesson from the failure that is Swift and Mojo turns out to be better. I do not want a language with a complicated type system and long compile times. I want a 'good enough' type system and fast compile times.

Re:Apple is rewriting Foundation in Swift by aw1621107 on Monday May 22, 2023 @12:35AM (#36027506)
> it always comes down to type inference on polymorphic types which causes these issues

Unless I'm thinking of something different, doesn't OCaml have type inference on polymorphic types and fast(ish?) compile times?

I was under the impression that the breakdown of causes for Rust projects compiling slowly is very project-dependent as well. From what I can remember common culprits were monomorphization and/or LLVM, though I'm not sure if that has changed recently.

Re:Apple is rewriting Foundation in Swift by octachron on Monday May 22, 2023 @05:49AM (#36029246)
Fast (and principled) inference for not-too-complicated polymorphic types is an explicit design goal for OCaml and Haskell languages. And this design goal constraint quite a lot the type system.

Typically, there are many type system features that break this property and where OCaml and Haskell requires type annotations (for instance polymorphic functions that takes polymorphic functions as arguments and use them in a polymorphic way).

As a consequence, typechecking for OCaml programs tend to take from 10% to 60% of the whole compilation time for typical programs. However, for OCaml programs that make heavy use of GADTs typechecking can dominate the compilation time (probably due to the exhaustiveness check but I have yet to empirically check that).

Infamously, Swift made the choice to introduce a typechecking algorithm with an exponential complexity in the size of expression (to support function overloading and literals) even for simple types.

Re:Apple is rewriting Foundation in Swift by aw1621107 on Monday May 22, 2023 @07:22PM (#36037964)
Fascinating! Thanks for the insight!

Does the up to 60% figure include programs that make heavy use of GADTs, or are those even worse in terms of the proportion of compilation time spent type checking?

I had no idea Swift's type checking algorithm is exponential. How easy is it to run into those conditions?

Re:Apple is rewriting Foundation in Swift by octachron on Tuesday May 23, 2023 @03:53AM (#36041274)
For GADTs heavy programs, it depends a lot on how much work the exhaustiveness check for pattern matching is doing behind the scene: With GADTs, one can encode a Turing machine in the type system. In that pathological case, analyzing that a pattern match is exhaustive might require to check if the encoded Turing machine stops in n steps. Consequently, it is perfectly possible to write programs where the compiler spends 99% of its time on analyzing pattern matching. But it is also possible to write GADTs heavy programs that don't require such expensive analyses.

I have no first-hand experience with Swift but the previous discussion at https://news.ycombinator.com/item?id=12108876 and the description of the problematic algorithm seems to indicate that numerical or mathematical code can run quite easily into the issue.

However, I must add that type inference algorithms being exponential in the source code size is nothing new: OCaml and Haskell inference are also exponential in the source code size. But that is because the size of types can be exponential in the source code size. Moreover, type inference complexity in OCaml and Haskell is linear in the size of types. And since in practice, no one ever uses exponentially growing types, type inference stays well behaved.

The specific issue with Swift is really more that even for simple result types, the inference may still have to do an exponential amount of guess work.