JaegerMonkey – Fast JavaScript, Always!

February 26th, 2010 by dvander Leave a reply »

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.

62 comments

  1. perrgjx says:

    Football head protection decals like these from Pro Helmet Decals are the symbol of the team. They are their brand and a powerful manifestation of team colors regarding players and proponents alike.
    I have build this lens to share my collection of free things with you since the freebies offers i have come from different countries i have first send out them to my family and friends who came up with the idea to place them on a website.
    Baby Pittsburgh Penguins NHL Hockey Tops for Sale – Low cost Custom Penguins Sports ApparelPittsburgh Penguins NHL Jerseys for Infants and Toddlers Online

  2. ROBBIE says:

    Great to have the list, will work my way through the different links. Good to see a healthy amount of blogging. Just started blogging myself. Would love to get a debate going so any comments are welcome.

  3. I genuinely enjoy reading through on this website, it has great blog posts. “Beware lest in your anxiety to avoid war you obtain a master.” by Demosthenes.

  4. You have mentioned very interesting points ! ps decent site. “The length of a film should be directly related to the endurance of the human bladder.” by Alfred Hitchcock.

  5. This page definitely has all of the information and facts I wanted about this subject and didn’t know who to ask.

  6. I really love your website.. Very nice colors & theme. Did you make this website yourself? Please reply back as I’m attempting to create my own personal site and would love to learn where you got this from or what the theme is named. Thanks!

  7. Thanks for every other fantastic article. Where else could anyone get that type of info in such an ideal means of writing? I’ve a presentation subsequent week, and I’m on the search for such info.

  8. I was more than happy to discover this page. I need to to thank you for your time for this particularly wonderful read!! I definitely loved every bit of it and i also have you bookmarked to see new information on your site.

  9. It’s perfect time to make some plans for the longer term and it is time to be happy. I have read this post and if I may I want to suggest you some fascinating issues or suggestions. Maybe you could write subsequent articles regarding this article. I wish to learn more issues about it!

  10. loan says:

    A fascinating discussion is worth comment. There’s no doubt that that you should write more about this topic, it might not be a taboo matter but generally people don’t speak about these topics. To the next! All the best!!

  11. Upload says:

    Oh my goodness! Awesome article dude! Many thanks, However I am encountering difficulties with your RSS. I don’t understand the reason why I cannot join it. Is there anybody getting the same RSS issues? Anyone who knows the solution can you kindly respond? Thanx!!

  12. Helpful info. Fortunate me I discovered your site by accident, and I am stunned why this twist of fate did not came about in advance! I bookmarked it.

Leave a Reply