Mozilla orders Jäger shot for Firefox engine

Mozilla is brewing a new extension to the Firefox JavaScript engine, hoping to fix a flaw in its setup that occasionally sends the open source browser back to 2007.

Dubbed JaegerMonkey, the new extension will operate alongside the much-ballyhooed TraceMonkey - an extension that debuted with Firefox 3.5 in June of last year - interpreting JavaScript code unsuited to "tracing." With Mozilla's current setup, code that can't be optimized with TraceMonkey is kicked back to an aging interpreter that runs JavaScript at speeds reminiscent of the dark ages before Firefox 3.5 or Google Chrome.

The JaegerMonkey project is only about two months old - and it's a ways from testing in a Firefox beta build - but a blog post from Mozilla programmer David Anderson says it's already providing a 30 per cent performance boost over that old interpreter on x86 machines.

TraceMonkey attempts to speed performance by detecting code loops and converting them into assembly language, but there are cases where this sort of "tracing" just doesn't work. "Loops can throw curveballs that cause tracing to stop," Anderson says. "Especially with recursion, or lots of nesting, it can be very difficult to build good traces on complex code."

When this happens, Firefox falls back on its older interpreter, which isn't quite up to snuff, as Anderson admits. "When tracing fails, we have to fall back to our old-school interpreter. At that point your JavaScript runs about as fast as it would in 2007-2008 (i.e. before Firefox 3.5, Safari 4, Chrome, etc)," he continues.

"That’s not acceptable, and we need to fix that. Trace compilation is still an active area of research (one which we’ll continue to work on) - but in the interim, we need to make sure our 'slow path' is at least as good as the competition."

Other JavaScript engines forgo tracing, compiling entire methods - as opposed to just loops - into assembly code. The Nitro engine used by Apple Safari and its WebKit open source base, for instance, uses this whole-method compilation setup. This means it can handle the sort of "curveballs" Anderson speaks of, but according to Mozilla, it can't match Firefox speeds when TraceMonkey does kick in.

With JaegerMonkey - or JägerMonkey - Anderson and fellow programmers Dave Mandelin, Luke Wagner, and Julian Seward are working to marry these two methods. TraceMonkey will still look to convert loops, but when it can't, JaegerMonkey will use the Nitro approach to speed code on its own, converting entire methods into assembly. "The question we’ve been asked, and we’ve been asking of ourselves, is: Why couldn’t we trace and keep going SUPER AWESOME FAST, and when tracing fails, fall back to STILL REALLY FAST?" he says.

"We’re taking the tried-and-true approach of other vendors, and bolting trace compilation on top. Once the two are interacting seamlessly, you’ll have a much more consistent - and fast - JavaScript performance experience."

The new project uses the same assembly code compiler used by Nitro. As Dave Mandelin points out in a blog post of his own, TraceMonkey uses its own native code compiler, known as nanojit, but this was deemed unsuited to use with JaegerMonkey.

"Nanojit does a fair number of compiler backend optimizations, like dead store elimination and common subexpression elimination, which allows to generate faster code, but makes it take longer to generate that code," he writes. "We don’t expect those optimizations to help much in the Jäger domain, so we wanted something simpler and faster."

And that means lifting from Apple's open source Nitro. "We know it’s simple and fast from looking at it before," Mandelin continues, "it’s open-source, and it’s well-designed C++, so it was a great fit."

This was then paired with a method compiler designed by Anderson. Running on Anderson's machine, the new setup provides that 30 per cent boost on x86 and a 45 per cent boost on x64.

The Mozilla programmers have yet to actually integrate JaegerMonkey and TraceMonkey, but they plan to do so this week. Eventually, Anderson says, the method compiler will be able to identify loops and send them to the trace compiler. And then, if the trace compiler decides the loops are too complex, it will send them back to the method compiler.

You can browse the JaegerMonkey wiki here, and the source code is here. JaegerMonkey is an addition Firefox's core JavaScript engine, still known as SpiderMonkey.

The ultimate goal, Anderson says, is to provide whole-method compilation that's at least on par with Firefox competitors, and the implication is that TraceMonkey - the fast path - would take Firefox beyond its competitors. But in a third blog post, Mozilla director of community development Asa Dotzler argues that as the major browser makers continue to mimic each other's techniques, we'll reach a kind of equilibrium where JavaScript execution approaches the speed of native code execution - no matter whose browser you're using.

"We're probably not far from a world where all of the browser vendors are sharing these performance techniques," he says, "and when we get there... I think we'll see this particular performance 'war' wrapping up with everyone winning." ®