JaegerMonkey – Fast JavaScript, Always!

Mozilla’s JavaScript optimizer, TraceMonkey, is pretty powerful. It carefully observes loops and converts them to super-fast assembly. We call this “tracing”.

That’s great and all, but there’s a problem: sometimes tracing doesn’t work. Loops can throw curveballs that cause tracing to stop. Especially with recursion, or lots of nesting, it can be very difficult to build good traces on complex code.

Other JavaScript engines, such as Nitro (present in WebKit/Safari), take a simpler approach. Instead of compiling loops to assembly, they compile entire methods (functions) to assembly. The generated code is much more generic than tracing, so while it is not as fast, it can handle any curveball.

What we’ve found is that when tracing works, we’re faster than the generic approach. But 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).

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.

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?

Enter JaegerMonkey.

Our new project, JaegerMonkey (or JägerMonkey), has exactly this in mind. 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.

Dave Mandelin, Luke Wagner, Julian Seward and I have been sprinting the past few weeks to get something basic working. To emit actual machine code, we’re using some very pretty classes (“macro assembler”) from Nitro. That’s been a real treat; it’s well-abstracted and C++ish, and allowed us to get to work on the actual compiler very quickly.

Our compiler is simple so far. Before interpreting a method, we translate each bytecode into some pretty generic assembly. For example, an “ADD” opcode will emit assembly that can handle both fast cases (adding two numbers) and slow cases (adding, say, an object and a string).

Contrast this to tracing, where the types are known, and pinned, statically – it does not need to handle any extra cases that might come up. In the whole-method compiler, the generated code must handle all unexpected variations in control or type flow.

After the function is compiled we execute it right away – the interpreter is skipped entirely.

Early Progress.

We’ve barely started and the results are already really promising. Running SunSpider on my machine, the whole-method JIT is 30% faster than the interpreter on x86, and 45% faster on x64. This is with barely any optimization work! When we integrate tracing next week, we’ll already start to see the benefits of both working together.

For a more in-depth study, Dave Mandelin has blogged about our early performance gains, what’s done, up-and-coming, etc.

As we move forward, the two compilers will be tightly integrated. The method compiler will be able to identify loops and invoke the trace compiler. The trace compiler, if it decides a method is too complex to inline, may decide to invoke the method compiler.

The future of SpiderMonkey is bright and shiny, and we’ll be talking more about the project as it reaches major milestones.

In the meantime, if you are interested in learning more, I invite you to look at JaegerMonkey on the Mozilla wiki, and our makeshift source code repository. We also hang out in #jsapi on irc.mozilla.org.

420 thoughts on “JaegerMonkey – Fast JavaScript, Always!

  1. Virgilio

    in depth info you provide. It’s nice to come throughout a blog every once in a while that isn’t the very same out of date reworked info. Excellent read!

    Reply
  2. belgravia villas

    Excellent blog here! Additionally your website loads up fast! What host are you the use of? Can I get your associate link in your host? I want my website loaded up as fast as yours lol

    Reply
  3. p30306

    Thanks for some other fantastic article. Where else may just anybody get that type of info in such a perfect manner of writing?
    I’ve a presentation subsequent week, and I am on the search for such info.p30306

    Reply
  4. Livia

    Any health-related situation could eventually turn into high priced as well as that’s bound to quickly put a monetary impediment around the family finances. Putting in squeeze exceptional travel cover offer you before leaving will be definitely worth the time and effort. Thanks

    Reply
  5. 81

    Excellent blog! Do you have any tips and hints for aspiring writers?

    I’m planning to start my own blog soon but I’m a
    little lost on everything. Would you recommend starting with a free platform
    like WordPress or go for a paid option? There are so many choices out there that I’m totally confused ..
    Any tips? Thanks a lot!81

    Reply
  6. historia electrodomesticos chilectra enel

    Estas mismas fuentes preven que serán en torno a 4.000 personas de CCOO y 2.000 de UGT las que a lo largo de la jornada tratarán que la huelga sea un éxito, especialmente en las primeras horas coincidiendo con el inicio del funcionamiento de los transportes la apertura de los comercios. La sesión de control de este jueves se ha desarrollado sin la presencia del presidente del Gobierno, Mariano Rajoy, que está volviendo a España después del viaje oficial que ha realizado a Seúl. El segundo gran episodio que demostró la falta de firmeza fue el pensionazo: cuatro meses después de la huelga general del 29 de septiembre, dispusieron, junto al gobierno, que tendremos que trabajar dos años más para cobrar una pensión más exigua.

    Reply

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">