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.

479 thoughts on “JaegerMonkey – Fast JavaScript, Always!

  1. PM

    Sounds excellent… Out if interest: Do you actually cache any generated code? Or would that not be a significant performance win?

  2. Dan

    Is it known who uses what approach in terms of all the slow and fast engines? Is this the approach that Opera uses now for their speedy results?

  3. dvander Post author

    PM: We cache bytecode for the browser shell, but not webpages. It would definitely make sense to cache native code if we’re doing either, I think, but I don’t know if anyone’s tried it enough to measure.

    Dan: Most JavaScript engines use whole-method JIT compilation of some sort. Mozilla is the only one to use trace compilation at all.

  4. Frikky

    If you have a lousy slow rendering engine and you happen to boost it’s speed by 10% of it’s current speed, it still remains a lousy slow rendering engine.

    Mozilla, stop spewing garbage, you’ll loose terrain, the only thing that keeps Firefox in the race are it’s add-ons, the thing that other people created, Mozilla has a lousy bunch of worthless programmers.

    (I hope someday you realize how awful it is to speak like this, to people you don’t even know. -dvander)

  5. mark

    If you have a high quality rendering engine and you can boost it’s speed by the amount that you are expecting, then it will remain an excellent product.

    Mozilla, thanks for keeping us informed, you are continuing to push the performance of Firefox forward. One of the things that keeps Firefox in the race are it’s add-ons, the other is the dedicated team behind the browser itself. Mozilla has a great bunch of talented programmers.

  6. PM

    How do dumb people find your blog!? You’d think at least the captcha would stop them. Amazing

  7. joe

    You spend paragraphs talking about speed improvements, but BARELY mention that this is because of webkit
    give them more credit

  8. dvander Post author

    joe – I certainly don’t want to downplay that we’re using the macro assembler from Nitro, however this is not the underlying reason for speed improvements. It’s a small, lightweight library that abstracts emitting low-level assembly. For sure, we were able to get the compiler written so quickly because the Nitro developers made such a nice backend tool.

    However you still have to write a compiler around it, and the compiler is what I’d prefer talking about, since we’ll have our own unique approach there and that’s what actually performs optimizations.

  9. RyanVM

    Kudos to you and the entire team (seems to be growing larger every day!) for the great work you’re doing.

  10. Erik Harrison

    I’m curious what the interaction between inline JIT and trace JIT will be like. Can I have a method that is inlined but with a hot loop inside that is traced? Can a traced loop call out to an inline JITed method? I’d hate to live in a world where a single traceable loop consigned all of my code to run on the interpreter, or a fully inline JITed script “only” runs at V8/Nitro speeds because the compiler can’t “dial up” to tracing when possible.

  11. dvander Post author

    Erik: Yeah! Definitely, we want to spend as little time in the interpreter as possible. The current plan is in bug 549522. Method JIT’d code will have very thin instrumentation on loops. If a loop gets hot, it’ll jump into the interpreter to record a trace.

    But that’s it. Once the recorder has finished – no matter if it was successful or not – we’ll go back to the method JIT. Basically we should never be in the interpreter unless we are specifically recording a trace, which is always at most one iteration of a loop. (Though we may re-try after 30 iterations or so.)

    From there the method JIT can invoke the trace-JIT’d code directly, without any interpreter transitions. If all goes according to plan, we’ll get the best of both worlds.

    Eventually it would be awesome to have the tracing JIT be able to invoke the method JIT. Right now it inlines every function call. But inlining isn’t always good, some methods don’t inline well. To come up with good heuristics for that, we (or someone) will have to sit down and study what makes a method a good candidate for inlining or not.

    In general there’s tons of future work ahead for letting the tracer relax on optimizations that are potentially too optimistic. With the method JIT in place we’ll be better positioned to inform the tracer of what it should/shouldn’t optimize.

  12. Ruturaj Vartak

    Its really great to know, right now when working on Linux 64bit, @ times I can feel there is a lot of difference in performance in Google Chrome and Mozilla Firefox.

    Hope to see that difference as negligible.

  13. cic

    This is a very interesting approach. I would like to know if it would be easy to apply to other language´s interpreters (like Ruby or Python) or the languages are too complicated to do it easily.
    Also, how is the starting time compared to the pure interpreter?

  14. dvander Post author

    cic: I think these approaches would definitely apply to Python and Ruby. There are a few projects for Python already, that do whole-method JITing, and I think Psyco does type specialization. I don’t think anyone’s tried trace-based type specialization for any dynamic languages other than JavaScript though. It would be really interesting to see that.

    What do you mean by “starting time”? If you mean the overhead of compilation, so far it seems pretty cheap. Not exactly 0, but well under 1ms for most methods.

  15. James Gray

    This sounds like such a great approach! I just read about this on ars technica, and thought you would have written something interesting about it, and I was right.

    The potential here is absolutely incredible.

    -“sslice”

  16. Erik Harrison

    davander, I posted a (awaiting moderation) comment over on hacks.moz, but I figured I’d mention it here – I built Firefox from the jaegermonkey repo, and overall I’m seeing a 50% perf improvement in Sunspider with Methodjit+Tracing enabled, and no regressions. Great success!

  17. prom dresses gowns

    With a be sure you dress which costs that you small part using the expenditure you may have excess money to use decorative accents, comfortable shoes and a little more money when it comes to party event routines.

  18. Commercial Glass

    A person necessarily help to make seriously articles I would state.
    That is the first time I frequented your web page and so far?
    I surprised with the analysis you made to make ar publish amazing.
    Excellent activity!
    Excellent site. Lots of helpful info here. I am sending it to several pals ans also sharing in delicious.
    And naturally, thanks in your effort!

  19. zdrowy usmiech

    Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this website? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform. I would be awesome if you could point me in the direction of a good platform.

  20. automatyka przemysłowa

    I have observed that over the course of building a relationship with real estate owners, you’ll be able to come to understand that, in every single real estate purchase, a payment is paid. Eventually, FSBO sellers never “save” the fee. Rather, they struggle to win the commission by doing a good agent’s occupation. In the process, they devote their money in addition to time to carry out, as best they might, the tasks of an agent. Those tasks include displaying the home through marketing, offering the home to all buyers, constructing a sense of buyer desperation in order to trigger an offer, preparing home inspections, managing qualification assessments with the financial institution, supervising fixes, and assisting the closing of the deal.

  21. Hvac service

    I’ve discovered a fantastic deal of really excellent content material in your web web-site. I’ll undoubtedly be back once more for lots far more.

  22. Jarrod Molyneux

    I might personally favor Google. Both FB and Google would benefit from it, but we would in all probability benefit more from Google taking it over. In the case of Google+ it is just a matter of time. Facebook was lucky it didn’t definitely need to compete with something as “good” as facebook, the competition was just lacking a lot of features as well as a global approach.Google+ is exceptional to Facebook, but it has a bigger challenge to get popular.Just check out VHS and Betamax. Betamax was better but lost due to bad marketing/licensing.Danny recently posted..Black & Decker NPP2018 18-Volt Cordless Electric Pole Chain Saw

  23. Cierra Filipiak

    May I simply just say what a relief to discover somebody that really understands what they’re discussing online. You certainly understand how to bring a problem to light and make it important. A lot more people have to read this and understand this side of the story. It’s surprising you are not more popular because you definitely have the gift.

  24. custom sunglasses

    Please forgive my bad English.Simply wish to say your article is as surprising. The clearness in your post is just nice and i could assume you’re an expert on this subject. Fine with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the enjoyable work.

  25. reverse lookup phone cell

    Hmm it appears like your website ate my first comment (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your blog. I too am an aspiring blog blogger but I’m still new to the whole thing. Do you have any tips for inexperienced blog writers? I’d really appreciate it.

  26. Willard Paparelli

    Hi, I do think this is an excellent blog. I stumbledupon it ;) I am going to revisit yet again since i have saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide others.

  27. student loan

    hey there and thank you for your information and facts ¡V I¡¦ve unquestionably picked up something new from correct here. I did on the other hand expertise some technical points making use of this internet site, since I experienced to reload the web page a good deal of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high excellent score if advertising and marketing with Adwords. Anyway I am adding this RSS to my e-mail and could look out for much much more of your respective intriguing content. Ensure that you just update this yet again quite quickly..

  28. Honey Gustine

    Outstanding study, I only passed this particular onto the colleague who had been doing slightly analysis on that. And this individual really ordered me lunch because I stumbled upon it regarding him look So let me rephrase which: Thanks with regard to lunch!

  29. Illinois siding contractor

    Thank you for sharing excellent informations. Your site is so cool. I’m impressed by the details that you have on this site. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my pal, ROCK! I found just the information I already searched everywhere and just couldn’t come across. What a great web site.

  30. Siding Orland IL

    I’ll immediately seize your rss as I can not find your e-mail subscription hyperlink or newsletter service. Do you have any? Please allow me recognise in order that I may just subscribe. Thanks.

  31. remodeling Illinois

    I have to point out my love for your kindness supporting those people who really want help with in this field. Your real dedication to passing the message all over had been unbelievably effective and has usually helped women like me to arrive at their objectives. Your personal important help indicates a great deal a person like me and much more to my mates. Thanks a lot; from all of us.

  32. Hvac service Chicago

    Nice read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch! “Dreams are real while they last. Can we say more of life” by Henry Havelock Ellis.

  33. ALESHA

    i need to fully back up my hard drive and i want to know what is the best freeware for this task. But above all this i want this program to be able to synchronize the two backups(the files in the internal hard and the external) so i can keep updated my files. So what is the best freeware for this task?

Comments are closed.