2012-02-02

Servo: a vision for the future of Firefox

Updates:
  • 2012-06-28: Restructured much of the content, to make it easier to digest.
  • 2012-06-27: New section “More information on Servo”.
Recently, Mozilla stopped the Electrolysis project [1] that was to give Firefox per-tab processes. Hence, I was relieved when David Bruant pointed out to me that Mozilla haven’t given up on a parallel browser. They are working on a project called Servo which is being implemented in the Rust programming language.

Servo – the next Firefox

The Servo project [2] is about building a parallel browser:
  • Use Rust instead of C++ which is safer and better equipped for parallelism.
  • Try to parallelize as many stages of the rendering process as possible (ongoing research).
  • Implement the DOM mostly in JavaScript (which makes it faster, because there are less context switches).
  • Browser innovation only: For the foreseeable future, Servo will use Firefox’s JavaScript engine, Spidermonkey.
Note that Servo is a research project, it may or may not become an actual product. Firefox is evolved in parallel and making great progress. In other words, Mozilla does not make the mistake of betting the farm on Servo. A complete rewrite of parts of the Navigator web browser (the flagship product) for version 4 brought Mozilla’s ancestor Netscape much trouble.

Why Servo isn’t written in C++

Firefox has grown to a very large code base, written in C++. C++’s main forte is speed. Writing a web browser in it poses two problems:
  • Safety: one of the key issues for a web browser is to keep users safe from attacks. C++ is an inherently unsafe language (especially when it comes to memory management).
  • Parallelism: Single-core speeds are improving at a decreasing rate, hence multi-core parallelism is the best strategy for making the web faster. C++ is not ideally suited for parallel programming.
With C++, safety and parallelism are antagonistic goals. Quoting Brendan Eich [2]:
Adding more threads to utilize multiple cores while fighting security bugs is like team-juggling chainsaws to music where the record player has been sped up!

The Rust programming language

Mozilla had to find a replacement for C++ and chose Rust:
The lead developer is Graydon Hoare, who began work on the system in 2006; Mozilla became involved in 2009, and officially unveiled the language for the first time at Mozilla Summit 2010.

Goals

Rust has been described as a “strongly-typed systems programming language with a focus on memory safety and concurrency”. Quoting the Rust website:
  • Syntax: Rust is a curly-brace, block-structured expression language. It visually resembles the C language family, but differs significantly in syntactic and semantic details.
  • Goals: Its design is oriented toward concerns of “programming in the large”, that is, of creating and maintaining boundaries – both abstract and operational – that preserve large-system integrity, availability and concurrency.
  • Features: It supports a mixture of imperative procedural, concurrent actor, object-oriented and pure functional styles. Rust also supports generic programming and metaprogramming, in both static and dynamic styles.
Servo helps with Rust’s evolution. Quoting Patrick Walton (one of Rust’s developers) on Hacker News:
We're using the bleeding edge master builds of Rust. Our work on Servo informs the direction we want to take Rust, and vice versa. Servo has been really valuable to help us determine whether the language features really work out in practice.
Rust is an interesting language. It looks like a mixture of C (syntax), Common Lisp (polymorphism via generic functions), Erlang (Actor-style concurrency), ML (immutable data, modules), and more. The following code from the language tutorial gives you a first impression:
    fn fac(n: int) -> int {
        let result = 1, i = 1;
        while i <= n {
            result *= i;
            i += 1;
        }
        ret result;
    }

Rust versus Go

As a systems language, Rust shares several goals with Google’s Go, but its inception predates that language. Mozilla plans to use it for desktop applications and server-side endeavors. Quoting Patrick Walton:
I think Rust's niche is software that has to be safe, scalable, and fast, in that order... with no room for compromise.
His answer to the question “isn’t that kind of similar to Go?” is:
No, Go is not designed for no-compromises speed and safety; it's designed for programmer productivity.

Releases

  • Release 0.2, 2012-03-29:
    Version 0.2 should still be considered an alpha release, suitable for early adopters and language enthusiasts.
  • Release 0.1, 2012-01-20:
    This is the initial release of the compiler after a multi-year development cycle focusing on self-hosting, implementation of major features, and solidifying the syntax. Version 0.1 should be considered an alpha release, suitable for early adopters and language enthusiasts. It's nifty, but it will still eat your laundry.

More information on Servo

References

  1. Firefox Electrolysis project put on hold
  2. Future Tense (slides from Brendan Eich’s talk during a Mozilla all-hands in April 2011)

No comments: