Last week in WebKit: simplifying everything

May 13th, 2013

Last week, everyone became crazy about refactoring. Some have taken “spring cleaning” very literally and many patches landed to make WebKit simpler and cleaner.

A nice change ties to the way Objective-C objects are adopted by RetainPtr. Previously, there were 3 ways to adopt a pointer:

RetainPtr<id> foo = adoptNS([...]);
RetainPtr<id> bar(AdoptNS, [...]);
RetainPtr<id> baz;

Darin Adler and Anders Carlsson wrote a few patches to unify all the code behind the adoptNS([…]) syntax. Some bugs were also discovered and fixed on the way.

Another great refactoring by Darin and Anders was switching from manual management of memory to smart pointers when possible. In particular, they hunted the use of deleteAllValues() on various data structure and updated their storage to use WTF’s OwnPtr. The new code is easier to reason about, and it becomes hard to accidentally leak memory on those structures.

There were also some nice cleaning of headers to rationalize where everything is defined in WTF.

New behaviors:

As usual, there were a lot of bug fixes. Some visible changes:

Talking about canvas, I spent a little time improving fillStyle and strokeStyle on Canvas2D’s context. A pattern I have seen is code recreating a CanvasPattern or CanvasGradient with every frame. For example:

function draw() {
    // Bad idea: don't do that for every frame.
    var gradient = context.createRadialGradient(...);
    context.strokeStyle = gradient;

When the Gradient/Pattern is constant from frame to frame, it’s much better to initialize the objects at the beginning and reuse them with every frame. Unfortunately, many tutorials fall into the trap of trashing the objects.

Engine engineering

The WebKitten of the week is Anders Carlsson for making this blog difficult with over 50 patches. Half of them refactoring and cleaning the code, the other half adding features to WebKit2.

Things to look forward to: