wingolog

generators in firefox now twenty-two times faster

14 November 2014 8:41 AM (spidermonkey | firefox | mozilla | javascript | es6 | ecmascript | v8 | jandem | bloomberg | igalia)

It's with great pleasure that I can announce that, thanks to Mozilla's Jan de Mooij, the new ES6 generator functions are twenty-two times faster in Firefox!

Some back-story, for the unawares. There's a new version of JavaScript coming, ECMAScript 6 (ES6). Among the new features that ES6 brings are generator functions: functions that can suspend. Firefox's JavaScript engine, SpiderMonkey, has had support for generators for many years, long before other engines. This support was upgraded to the new ES6 standard last year, thanks to sponsorship from Bloomberg, and was shipped out to users in Firefox 26.

The generators implementation in Firefox 26 was quite basic. As you probably know, modern JavaScript implementations have a number of tiered engines. In the case of SpiderMonkey there are three tiers: the interpreter, the baseline compiler, and the optimizing compiler. Code begins execution in the interpreter, which is the quickest engine to start. If a piece of code is hot -- meaning that lots of time is being spent there -- then it will "tier up" to the next level, where it is analyzed, possibly optimized, and then compiled to machine code.

Unfortunately, generators in SpiderMonkey have always been stuck at the lowest tier, the interpreter. This is because of SpiderMonkey's choice of implementation strategy for generators. Generators were implemented as "floating interpreter stack frames": heap-allocated objects whose shape was exactly the same as a stack frame in the interpreter. This had the advantage of being fairly cheap to implement in the beginning, but ultimately it made them unable to take advantage of JIT compilation, as JIT code runs on its own stack which has a different layout. The previous strategy also relied on trampolining through a helper written in C++ to resume generators, which killed optimization opportunities.

The solution was to represent suspended generator objects as snapshots of the state of a stack frame, instead of as stack frames themselves. In order for this to be efficient, last year we did a number of block scope optimizations to try and reduce the amount of state that a generator frame would have to restore. Finally, around March of this year we were at the point where we could refactor the interpreter to implement generators on the normal interpreter stack, with normal interpreter bytecodes, with the vision of being able to JIT-compile those bytecodes.

I ran out of time before I could land that patchset; although the patches were where we wanted to go, they actually caused generators to be even slower and so they languished in Bugzilla for a few more months. Sad monkey. It was with delight, then, that a month or so ago I saw that SpiderMonkey JIT maintainer Jan de Mooij was interested in picking up the patches. Since then he has been hacking off and on at getting my old patches into shape, and ended up applying them all.

He went further, optimizing stack frames to not reserve space for "aliased" locals (locals allocated on the scope chain), speeding up object literal creation in the baseline compiler and finally has implemented baseline JIT compilation for generators.

So, after all of that perf nargery, what's the upshot? Twenty-two times faster! In this microbenchmark:

function *g(n) {
    for (var i=0; i<n; i++)
        yield i;
}
function f() {
    var t = new Date();
    var it = g(1000000);
    for (var i=0; i<1000000; i++)
	it.next();
    print(new Date() - t);
}
f();

Before, it took SpiderMonkey 980 milliseconds to complete on Jan's machine. After? Only 43! It's actually marginally faster than V8 at this point, which has (temporarily, I think) regressed to 45 milliseconds on this test. Anyway. Competition is great and as a committer to both projects I find it very satisfactory to have good implementations on both sides.

As in V8, in SpiderMonkey generators cannot yet reach the highest tier of optimization. I'm somewhat skeptical that it's necessary, too, as you expect generators to suspend fairly frequently. That said, a yield point in a generator is, from the perspective of the optimizing compiler, not much different from a call site, in that it causes all locals to be saved. The difference is that locals may have unboxed representations, so we would have to box those values when saving the generator state, and unbox on restore.

Thanks to Bloomberg for funding the initial work, and big, big thanks to Mozilla's Jan de Mooij for picking up where we left off. Happy hacking with generators!

30 responses

  1. Matthias says:

    This count for which Version(s) please ?

  2. Tom Schuster says:

    Wingo, thank you for taking the time to write this blog post!
    @Mathias If you look at https://bugzilla.mozilla.org/show_bug.cgi?id=1093573 you will see a "Target Milestone" of mozilla36, which is Firefox 36, or at this time Firefox nightly.

  3. Arne Babenhauserheide says:

    Congratulations! This is awesome!

    And it’s great to see that your work paid off!

    For the speed, I got this link: http://arewefastyet.com/

  4. paul says:

    Nice! Will there be real coroutines at some point? That would allow getting rid of the horrid callback nonsense in node.js.

  5. db48x says:

    Paul: Yes, you can use yield to eliminate callbacks. Rather than explain, I'll just show an example: http://db48x.net/yield/yield.js

  6. ElectricPrism says:

    Had to say, kickass work bro.

  7. William ML Leslie says:

    Hey Andy,

    Just wanted to let you know that your less-than sign in the code block seems not to be escaped in the RSS feed.

  8. half girlfriend full movie says:

    Hey Andy,

    Just wanted to let you know that your less-than sign in the code block seems not to be escaped in the RSS feed.

  9. Apparao says:

    Hey Andy,

    Good Job Brother.Keep Going.

  10. Spicy Hunting says:

    Spicy Hunting is basic factor for hunting and you will learn how you can learn more and more about hunting and cooking.

  11. bigo beans says:

    Spicy Hunting is basic factor for hunting and you will learn how you can learn more and more about hunting and cooking.

  12. Golf Rangefinder says:

    Range finding is a art which is specially for those who love to see too far from them and analysis the beauty of the environment and have a great view about the nature.

  13. SB game hacker says:

    art which is specially for those who love to see too far from them and analysis the beauty of the environment and have a great view about th

  14. Bolo says:

    A bread making machine or bread maker is a home appliance for baking bread. You will love to buy them from us and get to know best bread making machine for your home.

  15. happy mothers day says:

    Before, it took SpiderMonkey 980 milliseconds to complete on Jan's machine. After? Only 43! It's actually marginally faster than V8 at this point, which has (temporarily, I think) regressed to 45 milliseconds on this test. Anyway. Competition is great and as a committer to both projects I find it very satisfactory to have good implementations on both sides.

  16. fang james says:

    Skin Novela has a promising future. Maybe I shouldn't put it that way. You can increase the time you spend once your Skin Care gets tuned up. To set yourself free from the crowd you want to hook up with Beauty. This is why you should absolutely get Wrinkles. I only do the basics.
    Read More: http://www.suxorfree.com/skin-novela/

  17. best cat litter for odor control says:

    Anyway. Competition is great and as a committer to both projects I find it very satisfactory to have good implementations on both sides.

  18. Call Girls in Jaipur says:

    Haute young ladies of Jaipur adopt a high class strategy to models escorts in Jaipur. We don't offer anything not as much as the most first class escorting administration inside Jaipur

  19. 9appscrack says:

    Flashlight instantly turns your device into a bright torch.A reliable source of emergency light. This app takes advantage of flash of your phone and turns your device into a torch as on $100 tactical torch comes with flashing strobe which attract the attention of other people to you.Great light at your fingertips. Ready and available for any situation or emergency. And its totally free.
    https://9appscrack.com/flashlight-fastest-led-light-torch-apk/

  20. happy raksha bandhan images says:

    Nice Flashlight instantly turns your device into a bright torch.A reliable source of emergency light. This app takes advantage of flash of your phone and turns your device into a torch as on $100 tactical torch comes with flashing strobe which attract the attention of other people to you.Great light at your fingertips. Ready and available for any situation or emergency. And its totally free.

  21. Score Hero Mod Apk says:

    That's really helpful
    It would be really great if you can keep posting such great content

  22. Happy Diwali 2017 SMS says:

    Awesome piece of content that is. It is really helpful and would really great if you keep posting such more stuff here.

  23. HP 6700 Drivers says:

    this is one of the best contents I have ever seen

  24. Click the Buy Coursework Link says:

    The outcomes of further research say there are indications of breaking point dissents in the skull. The contention was not a weapon, yet rather because of an amazing hail fell on his head. They couldn't escape being gotten in the midst of the valley. The pile of ice in the valley that over the long haul transformed into the lake that spared a few skulls.

  25. Hindimeresult says:

    The outcomes of further research say there are indications of breaking point dissents in the skull. The contention was not a weapon, yet rather because of an amazing hail fell on his head. They couldn't escape being gotten in the midst of the valley.

  26. Activate Bot Comments says:

    Awesome piece of content thisbis. Just love it. Awesome it is with great things.

  27. bhatu says:

    OK

  28. happy raksha bandhan images 2017 says:

    wow nice

  29. ejeta robinson says:

    Commbank Login – CommonWealth Bank | Online Banking Commonwealth bank also known as commbank or CBA is an Australian bank that offers financial services such as insurance, institutional bank, business banking, retail banking, management of funds etc.
    https://www.tecteem.com/index.php/2017/07/23/commbank-login-common-wealth-bank-online-banking/

  30. HP Wifi Drivers says:

    This is fantastico stupendouso websito

Leave a Reply