Comment by rvz

2 years ago

I would have thought that Ladybird would be using a language that specifically guarantees memory safety which is Rust. But Swift is a very surprising choice but actually makes sense for a browser.

The browser story is not great for Rust as we can only point to Servo which isn't ready to be used as a daily driver for millions of users. But Arc Browser is written with a combination of Swift and C++ to run on macOS and Windows.

Other than iOS apps, perhaps Swift found another use-case in large C++ code bases thanks to the C++ interop story.

> I would have thought that Ladybird would be using a language that specifically guarantees memory safety which is Rust.

Rust isnt the only compiled language that provides memory safety by default. Swift does as well.

Swift is memory safe.

The C++ interop thing I think is massive. But isn’t there also the problem that with Rust you can’t have the equivalent of shared pointers, so you can only have a single reference to one object (sorry I don’t really know Rust, just going from how this has been described to me) which apparently makes representing things like DOMs and abstract syntax trees an absolute nightmare?

  • RefCell is a shared pointer. Arc is a shared pointer with atomic reference counting (so it can be used between threads safely).

    Both these constructs use the "clone" trait to iterate the reference count.

    I make primarily concurrent and async applications these days so I use Arc a lot for accessing memory that is very large and shared many places, such as context.

    I think the real problem is Rust has a lot of rules (that make sense for safety) that take time to understand and most people aren't patient enough to make good software because of corporate conditioning.

  • And cannot have linked lists either. When you make a compiler in rust, basically you give everything the same lifetime and drop the memory at the very end. That allows cicular references. Or you use Rc, but that is against what purists want you to do. For longer lifetime programs with complex models the borrow checker becomes harder

    • You can express linked lists efficiently in Rust using the GhostCell/QCell pattern. Yes it's clunky, but this is after all an area of active research in PL theory. As it is, most ordinary "easy" implementations of linked lists do not actually prove memory safety - it's a genuinely non-trivial problem, not a case of mere incidental complexity.

> specifically guarantees memory safety which is Rust

I think you mean "one of which is Rust".

I'm not sure if the comparison to Servo holds up since that's a browser engine, and Arcs engine (Chromium) is 100% C++. Swift is only used for the frontend.