X
Business

Servo: Inside Mozilla's mission to reinvent the web browser for the multi-core age

Mozilla on how its Servo engine will throw away the 20th-century baggage that holds back current browsers and harness the power of modern multi-core smartphones and tablets.
Written by Nick Heath, Contributor

Mozilla has opened up about its mission to reinvent the web browser for the multi-core, mobile age.

The foundation behind the Firefox browser is working with Samsung to create Servo, a new browser layout engine that breaks free of the legacy code and design decisions that shackle the performance of modern browsers.

"Basically all of the browser engines you are used to using were designed before the year 2000, and hardware at that time was very different. It usually only had one core, clock speeds were lower and you had much less memory available to you," said Mozilla platform engineer Josh Matthews at the FosDem conference earlier this month.

"Things like multi-thread programming weren't built in from the start and they've been bolted on after the fact. Existing engines are giant and there are various architectural decisions built into them that are very difficult to modify."

Servo will be designed to deliver parallel performance in the browser, to use the multiple processor cores inside modern mobiles and tablets to carry out tasks concurrently.

Major browser makers have made great strides in eeking more performance from Chrome, Internet Explorer, Firefox and Safari, through measures such as adding Just in Time compilation to their respective JavaScript engines. But performance and security gains can only go so far without rearchitecting the browser stack, said Matthews.

"There is a constant race to get a little bit faster than the other engines but often you're working against the architectural constraints imposed on you by the existing code," he said.

"We think that we can do better on both security and performance if we build it from the ground up with the understanding of the problems that we have from years of experience."

Servo is being designed to run on top of the Android OS on devices with Arm-based processors. There is plenty of scope to improve browser performance on mobiles, according Matthews, who said resource constraints can lead mobile browser taking "maybe ten times as long as a desktop machine" to lay out a web page.

Parallel tasks

The approach current browsers take to displaying content on a web page results in many operations being carried out sequentially that could be carried out in parallel by Servo, said Matthews.

In simple terms, a web browser parses HTML to compute the layout of the page, renders the content and then executes JavaScript, which in turn can modify the content being rendered.

"Modern engines will often be able to do the actual painting to the screen in parallel and then you have a loop between computing and executing. Even so there are a lot of serial and sequential operations that we believe we could do better," said Matthews.

Servo takes a different approach to current browsers. It splits the work to compute the layout, render content and execute scripts on a web page into three tasks, each of which it can carry out in parallel. 

The browser's ability to carry out these tasks at the same time stems from the nature of the Servo's underlying programming language, Rust, which has been developed by Mozilla for several years and is nearing version 1.0.

Each task in Rust is isolated, there is no shared memory between tasks and they communicate by message passing, which takes place asynchronously. The result is that tasks are independent from each other and rarely block each other's messages, making parallelism easier to accomplish.

"This means the renderer will be operating in parallel with the layout and script task. These are all isolated from each other and all able to run at their own speeds," said Matthews.

servo-tasks
Image: Mozilla

"There's still a little bit of serialisation because you need something laid out before you can render it. But after that if you do something like resize the window the layout can happen independently of painting your existing content and so you get a much smoother experience that way."

Servo also tries to deliver better performance when handling iFrames.

Servo's approach to iFrames means that a page will not always have to wait for the layout of the iFrame to be computed before the rest of the page. In addition an iFrame can have its own script task, which stops an iFrame from interfering with the execution of a script in the enclosing page. Finally Servo can also cope with a crash while loading in content inside an iFrame and allow the rest of the encapsulating page to continue to operate normally.

Servo can also carry out the type of "coarse parallelism" seen in other browsers, where the layout, computation and script execution is carried out in an isolated fashion for each tab.

Servo will use the Spidermonkey Javascript engine, currently used alongside the Gecko layout engine in Firefox.

Parallel layout

When browsers compute the layout of a web page they step through a tree-like model of elements that are to be laid out on the page to calculate how they should be positioned relative to one another.

Browser layout engines tend to step sequentially through this model, often having to reach fairly deep into the tree.

"Regular layout in any modern browser engine proceeds sequentially. You'll start at the top and you'll need to go to each child element and lay it out on the page and figure out where they exist, because other things at the same level will require that knowledge in order to lay out themselves," said Matthews.

While modern browsers can optimise this layout process, Servo is able to more aggressively streamline layout, he said, by identifying parts of the web page that don't affect each other's size or positioning and laying them out in parallel.

servo-layout
Image: Mozilla

"With Servo we think we can do better. We can break it down and for certain classes of web pages we can say 'We don't need to care about the other parts of the page'. We know that we can lay out certain parts of tree independently, so those can be farmed out to other processors and other isolated tasks.

"In particular we don't even have to do sub-trees, we can just split out individual children and use techniques like work stealing [where work from a heavily loaded processor queue is offloaded to another processor] to make sure that all of your processors are always fully engaged in laying out a page, without having to resort to sequential operations."

This parallel layout approach will make the biggest difference to performance of mobile browsers, he said, and may also extend battery life while browsing on mobiles.

"On mobile we're often a lot more resource constrained and there are much fewer processors so layout often takes maybe ten times as long as on a desktop machine," he said.

"So parallel layout in this case could be a very big advantage. We could also get power benefits because one processor working at full capacity will consume a lot more power than several processors working at reduced capacity over the same period of time.

"We think that we could get much better battery life improvements from Servo."

Security

The other advantage that Servo will have over current browsers is its approach to security, according to Matthews.

"Security is really big. Everyone uses a browser and therefore browser exploits are really good targets for security vulnerabilities.

"Our feeling is that, at least in Gecko [the layout engine used by Firefox], a lot of the security problems come from C++'s unsafe memory model."

Matthews said that C++ suffers from a class of memory management errors that lead to crashes and security vulnerabilities.

"Rust basically eliminates these whole classes of errors. In addition we have task isolation, which is shared nothing so you don't have data races," he said.

"It's also really simple to turn Rust into a process isolation model for using tasks, without having to modify the code for the browser itself. That gives us a whole different class of protection.

"Then we're thinking why not go even further? Why not take unsafe C code, which we need for certain performance reasons, and just run that inside a sandboxed NaCl process and you get multiple layers of protection."

What's next for Servo?

While a stable release of Servo is some way off Mozilla is hoping that Servo will be "relatively usable in a dog fooding kind of way" by the end of the year, according to Matthews. At present it is necessary to build Servo from the source code but binaries will be introduced in "the coming quarter".

Servo is being developed by a team of five Mozilla engineers with the help of community volunteers and a team of research engineers at Samsung.

"It's a fairly small team but we're getting a lot done and we're getting a lot of good results. We would love more people to help us out, it's a great way to learn a new and exciting language and it's a great way to push at the cutting edge of the web," Matthews said.

Volunteers can contribute to the project by writing Rust code, by writing JavaScript and CSS code (it has a test suite integrating W3C tests), or just by testing Servo's current build and filing bugs.

Current problems the Servo team are looking for help on include implementing missing DOM APIs and missing CSS features and making Servo run on Windows.

To keep track of development of Servo you can sign up to the Mozilla.dev.servo mailing list.

Editorial standards